Hi,
I'm attempting to create two simple method for encrypting/decrying plain text.

I have the following code:

public String encrypt(String plainText) {

      byte[] plaintTextInBytes = plainText.getBytes();

      Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding", 
"Mozilla-JSS");

      AlgorithmParameters ap = null;

      cipher.init(Cipher.ENCRYPT_MODE, mozKey);

      ap = cipher.getParameters();
      if (ap != null) {                
           encodedAlgParams = ap.getEncoded();
      }

     byte[] ciphertext = cipher.doFinal(plaintTextInBytes);

    return new String(Hex.encodeHex(ciphertext));
} 


 public String decrypt(String cihperText) {

            byte[] chiperTextAsBytes = Hex.decodeHex(cihperText.toCharArray());
           
            Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding", 
"Mozilla-JSS");

            cipher.init(Cipher.DECRYPT_MODE, mozKey);


            byte[] recovered = cipher.doFinal(chiperTextAsBytes);

            return new String(recovered);
}

The thing is when I decrypt the cipher.init(Cipher.DECRYPT_MODE, mozKey) method 
throws the following exception:
 
java.security.InvalidKeyException: DESede/CBC/PKCS5Padding cannot use a null 
parameter

only when I add the ap (AlgorithmParameters) from the encrypting method as 
such: cipher.init(Cipher.DECRYPT_MODE, mozKey,ap) it works.

Am I missing something? How can I workaround this? since I have scenarios where 
I only decrypt or cannot save the ap value.

Thanks in advance!
-- 
dev-tech-crypto mailing list
dev-tech-crypto@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-tech-crypto

Reply via email to