Hi I'm trying to create an encrypt/decrypt app in java for a project and I managed to do the encrypt decrypt in the same class however I need two separate classes one for encrypt and one for decrypt, but I've been getting this error (Instead of printing the message I initially encrypted) and I'm not sure what I need to do to make it work.
Without separating the code into classes:-
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Encrypt1
{
public static void main(String[] argv) {
try{
//KeyGenerator keygenerator = KeyGenerator.getInstance("AES");
//SecretKey myAesKey = keygenerator.generateKey(key);
Scanner input = new Scanner(System.in);
System.out.println("Please enter a message you'd like to encrypt");
String plaintext = input.nextLine();
System.out.println("Please enter a 16 digit password: ");
String pass = input.nextLine();
byte[] key = pass.getBytes();
SecretKeySpec aesKey = new SecretKeySpec(key, "AES");
Cipher aesCipher;
// Create the cipher
aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// Initialize the cipher for encryption
aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);
//sensitive information
byte[] text = plaintext.getBytes();
System.out.println("Text [Byte Format] : " + text);
System.out.println("Text : " + new String(text));
// Encrypt the text
byte[] textEncrypted = aesCipher.doFinal(text);
System.out.println("Text Encryted : " + textEncrypted);
// Initialize the same cipher for decryption
aesCipher.init(Cipher.DECRYPT_MODE, aesKey);
// Decrypt the text
byte[] textDecrypted = aesCipher.doFinal(textEncrypted);
System.out.println("Text Decryted : " + new String(textDecrypted));
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(NoSuchPaddingException e){
e.printStackTrace();
}catch(InvalidKeyException e){
e.printStackTrace();
}catch(IllegalBlockSizeException e){
e.printStackTrace();
}catch(BadPaddingException e){
e.printStackTrace();
}
}
}
Attempt to separate it into classes:-
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class Encrypt2 {
public static void main(String[] args) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
// TODO Auto-generated method stub
Encrypt();
Decrypt();
}
public static void Encrypt() throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
Scanner input = new Scanner(System.in);
System.out.println("Please enter a message you'd like to encrypt");
String plaintext = input.nextLine();
System.out.println("Please enter a 16 digit password: ");
String pass = input.nextLine();
byte[] key = pass.getBytes();
SecretKeySpec aesKey = new SecretKeySpec(key, "AES");
Cipher aesCipher;
// Create the cipher
aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// Initialize the cipher for encryption
aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);
//sensitive information
byte[] text = plaintext.getBytes();
System.out.println("Text [Byte Format] : " + text);
System.out.println("Text : " + new String(text));
// Encrypt the text
byte[] textEncrypted = aesCipher.doFinal(text);
System.out.println("Text Encryted : " + textEncrypted);
}
public static void Decrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Scanner input = new Scanner(System.in);
System.out.println("Please enter a message you'd like to decrypt");
String ciphertext = input.nextLine();
byte[] textEncrypted = ciphertext.getBytes();
System.out.println("Please enter a 16 digit password: ");
String passw = input.nextLine();
byte[] key = passw.getBytes();
SecretKeySpec aesKey = new SecretKeySpec(key, "AES");
Cipher aesCipher;
// Create the cipher
aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
String pass = input.nextLine(); // Initialize the same cipher for decryption
aesCipher.init(Cipher.DECRYPT_MODE, aesKey);
// Decrypt the text
byte[] textDecrypted = aesCipher.doFinal(textEncrypted);
System.out.println("Text Decryted : " + new String(textDecrypted));
}
}
Btw I have type a 16 bit character and everything as well, here is the result I got from running the code:-
Please enter a message you'd like to encrypt hi Please enter a 16 digit password: 1234567890987654 Text [Byte Format] : [B@382ce255 Text : hi Text Encryted : [B@1b523bd4 Please enter a message you'd like to decrypt [B@1b523bd4 Please enter a 16 digit password: 1234567890987654
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at Encrypt2.Decrypt(Encrypt2.java:80)
at Encrypt2.main(Encrypt2.java:18)
here is a screenshot of the result if needed: http://puu.sh/dnITY/eac9faa84f.png