У меня есть два класса с именем encryption.java и decryption.java. Я хочу зашифровать текстовый файл, используя класс encrypt.java, и расшифровать его из класса decrypt.java. Я могу зашифровать текстовый файл успешно, но не смог его расшифровать таким же образом. Может ли кто-нибудь сказать мне, почему нет?Ошибка Java в расшифровке
Это encrypt.java:
public class Encrypt{
public static void main(String[] args) throws Exception {
String FileName = "D:/ashok/normal.txt";
String FileName1 = "D:/ashok/encrypted.txt";
KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
KeyGen.init(128);
SecretKey SecKey = KeyGen.generateKey();
Cipher AesCipher = Cipher.getInstance("AES");
byte[] cipherText = Files.readAllBytes(Paths.get(FileName));
AesCipher.init(Cipher.ENCRYPT_MODE, SecKey);
byte[] byteCipherText = AesCipher.doFinal(cipherText);
Files.write(Paths.get(FileName1), byteCipherText);
}
Это мой decrypt.java класс:
class decrypt{
public static void main(String[] args) {
try {
String FileName1 = "D:/ashok/encrypted.txt";
String FileName2 = "D:/ashok/decrypted.txt";
KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
KeyGen.init(128);
SecretKey SecKey = KeyGen.generateKey();
Cipher AesCipher = Cipher.getInstance("AES");
byte[] cipherrText = Files.readAllBytes(Paths.get(FileName1));
AesCipher.init(Cipher.DECRYPT_MODE, SecKey);
byte[] bytePlainText = AesCipher.doFinal(cipherrText);
Files.write(Paths.get(FileName2), bytePlainText); }}
и получать ошибки при выполнении класса расшифровывать, как этот
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
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:2086)
at decryption.main(decryption.java:79)
Пробуйте эту реализацию AES-256 с помощью 'keystore' https://github.com/sufiyanghori/aes-256-java-bks –