3 Replies Latest reply on Apr 15, 2013 6:58 AM by EJP

    Cipher - encrypt large String variable

    1002972
      Hello,

      I have a jUnit test for a simple Cipher encryption/decryption.
      I want to write a test to encrypt large data.

      Do I have to change the encryption variable type from String to a Stream class?

      Anyone have suggestion?

      package no.ssb.cipher;

      import org.junit.*;
      import static org.junit.Assert.*;


      /**
      * Unit test for simple App.
      */
      public class SimpleStringCipherTest {

      private SimpleStringCipher simpleStringCipher;

      /**
      * Create the test case
      *
      * @param testName name of the test case
      */
      @Before
      public void setUp()
      {
      simpleStringCipher = new SimpleStringCipher("tvnw63ufg9gh5392");
      }

      @Test
      public void testBasicEncyption() throws Exception {         
      String plainText = "2010 starts new decade.";
      String encrypted = simpleStringCipher.encrypt(plainText);
      System.out.println("Encrypted: " + encrypted);
      assertTrue(!plainText.equals(encrypted));
      String decrypted = simpleStringCipher.decrypt(encrypted);
      assertEquals(plainText, decrypted);
      }

      @Test
      public void testEncryptionIsNotURLSafe() throws Exception{
      String plainText = "2010 starts new decade.";
      String expectedValue = "abe6vPUFQ4xSMezuFF2HBNC3dW98iifMeM027PKKnNw=";

      String encrypted = simpleStringCipher.encrypt(plainText);
      assertTrue(!plainText.equals(encrypted));
      assertTrue(expectedValue.equals(encrypted));

      String decrypted = simpleStringCipher.decrypt(encrypted);
      assertEquals(plainText, decrypted);

      }

      @Test(expected=IllegalArgumentException.class)
      public void testInvalidSecretKey() throws Exception{
      SimpleStringCipher invalidCipher = new SimpleStringCipher("tull");
      }

      @Test(expected=Exception.class)
      public void testDecryptInvalidEncryptedString() throws Exception {
      simpleStringCipher.decrypt("abe6vPUFQ4xSMezuFF2HBNC3dW98jjfMeM027PKKnNw");

      }

      }
        • 1. Re: Cipher - encrypt large String variable
          EJP
          I have a jUnit test for a simple Cipher encryption/decryption.
          I want to write a test to encrypt large data.
          Do I have to change the encryption variable type from String to a Stream class?
          The question doesn't make sense. You're asking us about the interface to the unknown class you are unit testing. We can't answer that. You have to test the API that you are unit-testing. Only you know what that is.
          • 2. Re: Cipher - encrypt large String variable
            1002972
            Sorry, I am beginner and new in Cipher.
            Hope my following question make more sense.

            In production we have an error when decrypting
            large data.

            My existing unit test is shown and class I am testing
            How can I write a new test, simulating the production error we have?

            1.@Test
            2.public void testBasicEncyption() throws Exception {
            3.String plainText = "text to be encrypted."
            4.String encrypted = simpleStringCipher.encrypt(plainText);
            5.System.out.println("Encrypted: " + encrypted);
            6.assertTrue(!plainText.equals(encrypted));
            7.String decrypted = simpleStringCipher.decrypt(encrypted);
            8.assertEquals(plainText, decrypted);
            }

            * Class to test *
            package no.ssb.cipher;

            import javax.crypto.Cipher;
            import javax.crypto.SecretKey;
            import javax.crypto.spec.SecretKeySpec;
            import org.apache.commons.codec.binary.Base64;

            public class SimpleStringCipher {
            private static byte[] linebreak = {}; // Remove Base64 encoder default
            // linebreak
            private String secret; // secret key length must
            // be 16
            private SecretKey key;
            private Cipher cipher;
            private Base64 coder;

            public SimpleStringCipher(String secretKey) {
            if(secretKey.length()!=16){
            throw new IllegalArgumentException("secretKey must be 16 digits");
            }

            this.secret = secretKey;
            try {
            key = new SecretKeySpec(secretKey.getBytes(), "AES");
            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
            coder = new Base64(32, linebreak, false);
            } catch (Throwable t) {
            t.printStackTrace();
            }

            }

            public synchronized String encrypt(String plainText)
            throws Exception {
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] cipherText = cipher.doFinal(plainText.getBytes());
            return new String(coder.encode(cipherText));
            }

            public synchronized String decrypt(String codedText)
            throws Exception {
            byte[] encypted = coder.decode(codedText.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decrypted = cipher.doFinal(encypted);
            return new String(decrypted);
            }
            }

            Edited by: 999969 on Apr 14, 2013 10:38 PM
            • 3. Re: Cipher - encrypt large String variable
              EJP
              That class has no methods that take streams as parameters or return them as results. So why are you asking about streams?

              Still doesn't make sense.