J'essaie de développer deux programmes java. Un pour crypter du texte en clair et un autre pour décrypter ce texte crypté.
Voici mon code :
Encryption.java
public class Encryption {
private static Cipher cipher = null;
public static void main(String args[]) throws Exception {
Scanner scan = new Scanner(System.in);
String keyText = "9ofAGtArndXw9Ffu3lRTGWy9svXuUBl8";
byte[] keyBytes = keyText.getBytes("UTF-8");
SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
cipher = Cipher.getInstance("AES");
System.out.println("Enter the plain text to be encrypted: ");
String plainText = scan.nextLine();
byte[] plainTextByte = plainText.getBytes("UTF-8");
byte[] encryptedBytes = encrypt(plainTextByte, secretKey);
String encryptedText = new String(encryptedBytes, "UTF-8");
System.out.println("Encrypted Text After Encryption: " + encryptedText);
}
static byte[] encrypt(byte[] plainTextByte, SecretKey secretKey) throws Exception {
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainTextByte);
return encryptedBytes;
}
}
INPUT FOR Encryption.java : nonu ET Ouput FROM Encryption.java : ??8????M?wFg(Ee
Mais lorsque j'entre la sortie de encryption.java dans decryption.java, il me donne une erreur au lieu de me rendre le texte en clair.
Voici le code de Decryption.java
public class Decryption {
private static Cipher cipher = null;
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
String keyText = "9ofAGtArndXw9Ffu3lRTGWy9svXuUBl8";
byte[] keyBytes = keyText.getBytes("UTF-8");
SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
cipher = Cipher.getInstance("AES");
System.out.println("Enter the encrypted text to be decrypted: ");
String encryptedText = scan.nextLine();
byte[] encryptedBytes = encryptedText.getBytes("UTF-8");
byte[] decryptedBytes = decrypt(encryptedBytes, secretKey);
String decryptedText = new String(decryptedBytes, "UTF-8");
System.out.println("Plain Text is: " + decryptedText);
}
static byte[] decrypt(byte[] encryptedBytes, SecretKey secretKey)
throws Exception {
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return decryptedBytes;
}
}
Il me donne cette erreur
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:936)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
at javax.crypto.Cipher.doFinal(Cipher.java:2164)
at encryption.Decryption.decrypt(Decryption.java:36)
at encryption.Decryption.main(Decryption.java:27)
Comment puis-je résoudre cette erreur ?