5
\$\begingroup\$

I have been trying to do some encryption-decryption using bouncy castle and with the available docs and samples on the internet I have managed to prepare the below. Though this works without throwing any exception, i feel something fishy. Can somebody tell me if i have done it right or is there any thing that i should do :)

 private static void Test()
  {
  try
  {  
    String keyString = "Olmy9iqs1LwWblwe";   
    String input = "teststring";
    byte[] inputBytes = input.getBytes();
    String xiv = "1234567891234567";
    byte[] iv = xiv.getBytes("UTF-8");
    int length;
    //Set up
    AESEngine engine = new AESEngine();
    CBCBlockCipher blockCipher = new CBCBlockCipher(engine); 
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher); 
    KeyParameter keyParam = new KeyParameter(keyString.getBytes());
    ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 16);

    // Encrypt
    cipher.init(true, keyParamWithIV);
    byte[] outputBytes = new byte[cipher.getOutputSize(inputBytes.length)];
     length = cipher.processBytes(inputBytes,0,inputBytes.length, outputBytes, 0);
    cipher.doFinal(outputBytes, length); 
    String encryptedInput = new String(Base64.encode(outputBytes));
    System.out.println("Encrypted String:" +encryptedInput);

    //Decrypt            
    cipher.init(false, keyParamWithIV);
    byte[] out2 = Base64.decode(encryptedInput);
    byte[] comparisonBytes = new byte[cipher.getOutputSize(out2.length)];
    length = cipher.processBytes(out2, 0, out2.length, comparisonBytes, 0);
    cipher.doFinal(comparisonBytes, length); //Do the final block
    String s2 = new String(comparisonBytes);
    System.out.println("Decrypted String:"+s2);
}
  catch (Exception e) 
  {
    e.printStackTrace();
  }
}
\$\endgroup\$
4
\$\begingroup\$

Some observations:

  1. It is best to always specify the encoding when converting from a string to a byte array:
    input.getBytes() -> input.getBytes("UTF-8")
    keyString.getBytes() -> keyString.getBytes("UTF-8")

  2. The IV should be an array of random bytes unique for each encryption, usually created from a CSPRNG. Then prefix the encrypted data with the IV for use during decryption, the IV does not need to be secret.

  3. When using a password it is necessary to derive the key from it with a function such as PBKDF2.

\$\endgroup\$
4
  • \$\begingroup\$ Thank you for that. Also do you see any other flaw in the algorithm implementation? \$\endgroup\$
    – akn
    Jun 29 '17 at 19:24
  • 1
    \$\begingroup\$ Past this it depends on you usage and needs. You may need encryption authentication. If you use password derivation there is the issue of passing the salt and iteration, they too can be prefixed to the encrypted data. Finally it is a good idea to also prepend a version indicator. An example of all of this is RNCryptor-Spec. \$\endgroup\$
    – zaph
    Jun 29 '17 at 20:56
  • \$\begingroup\$ The situation i need to tackle is, another vendor of my client passes me the encrypted data and i need to decrypt it and process it. So they have generated the key at their end shared it to us. Another query that i have is how do i make use of a key in this program which is in hex format.? What do I do to incorporate that into my program? @zaph \$\endgroup\$
    – akn
    Jun 30 '17 at 9:07
  • \$\begingroup\$ Details of specific coding are best handled on your SO question and more details will be required. \$\endgroup\$
    – zaph
    Jun 30 '17 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.