Eu estou tentando desenvolver dois programas Java. Uma para criptografar texto simples e outra para descriptografar o texto cifrado.
Abaixo está o meu código:
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;
}
}
ENTRADA PARA Encryption.java: Nonu E Ouput DE Encryption.java:? ?? 8 ??? M WFG (Ee
Mas quando eu entro a saída do encryption.java na decryption.java, ele está me dando algum erro, em vez de me dar de volta o texto simples.
Aqui está o código 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;
}
}
Ele está me dando esse erro
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)
Como posso resolver esse erro?
Há pelo menos um problema, ou seja, com esta linha:
String encryptedText = new String(encryptedBytes, "UTF-8");
Os dados binários criptografados, em geral, não ser um válido codificação UTF-8. Isto significa que você perder informações ao converter os bytes para uma cadeia.
Use base64 ou alguma outra codificação para dados binários para converter os bytes binários para uma string.