Im new to cryptography in Java and I am trying to write a program to encrypt and decrypt a phrase using DES symmetric cipher, based on CBC mode of operation.
Can anyone tell me how to go about creating an 8-byte initialization vector and how to cast the new IV into AlgorithmParameterSpec class?
Also, which packages should I import?
Edit: Right now I have these lines:
SecureRandom sr = new SecureRandom(); //create new secure random
byte [] iv = new byte[8]; //create an array of 8 bytes
sr.nextBytes(iv); //create random bytes to be used for the IV (?) Not too sure.
IvParameterSpec IV = new IvParameterSpec(iv); //creating the IV
Is my above approach correct?
Thanks.
Yes. Till Now You are right.
the
Class IvParameterSpecis used to pass Initial Vector toClass CipherAfter this create a
Cipheras belowhere
DES/CBC/NoPaddingis used because you are useing DES encryption in CBC mode.Next is initializing it.
Parameters are:
1st is mode of encryption either
Cipher.DECRYPT_MODEorCipher.ENCRYPT_MODE2nd is Secret Key You have generated using
Class SecretKey3rd is generated ivParameterSpec
and last line is
if mode is Decrypt it will return decrypted data and if mode is Encrypt it will return encrypted data.
Last but important catch all the Exceptions properly.
And you are done