I'm writing a code which will encrypt and decrypt the message(using DES algorithm), and both messages will be displayed in Dialog box. But when i run the code i got nullPointerException. And after debugging the code i realize the null data is getting stored 'iv'. Here is my code:-
import javax.crypto.spec.*;
import javax.crypto.*;
import javax.swing.*;
public class des
{
public static void main(String ar[]) throws Exception
{
KeyGenerator keygen=KeyGenerator.getInstance("DES");
SecretKey secretkey=keygen.generateKey();
Cipher cip=Cipher.getInstance("DES");
String inputText=JOptionPane.showInputDialog("Give input:");
byte[] iv=cip.getIV();
IvParameterSpec ps=new IvParameterSpec(iv);
cip.init(Cipher.ENCRYPT_MODE,secretkey);
byte[] encrypted=cip.doFinal(inputText.getBytes());
cip.init(Cipher.DECRYPT_MODE,secretkey,ps);
byte[] decrypted=cip.doFinal(encrypted);
JOptionPane.showMessageDialog(null,"Encrypted :"+new String(encrypted)+"\n Decrypted :"+new String(decrypted));
System.exit(0);
}
}