Так что у меня возникают проблемы с расшифровкой файла или строки с использованием java AES-шифрования. Поэтому у меня есть текст переменная, содержащая двоичные числа. Я создал случайный ключ. Затем я зашифровал текст и вернул зашифрованный байт [] и написал его в файл .txt. И процесс шифрования работает.Как расшифровать строку из AES Encryption?
Затем я схватил все байты от exampleOrig.txt и выполнил процесс дешифрования и возвратил ошибку ниже. Я не уверен, что не так, и делаю некоторые исследования, но это не помогло. Любая помощь будет оценена по достоинству. Благодаря!
public static void main(String[] args) throws Exception {
// Generate AES Key
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecretKey myAesKey = keyGenerator.generateKey();
Cipher aesCipher = Cipher.getInstance("AES");
String text = "11111110001100110011011111111011011111111101000111000101111111111111111001011110110001011111110111111001110110011100110111011111101111100111101";
// ENCRYPT the text
aesCipher.init(Cipher.ENCRYPT_MODE, myAesKey);
byte[] textEncrypted = aesCipher.doFinal(text.getBytes());
// Output results
System.out.println("Text [Byte Format]: " + text);
System.out.println("Text : " + new String(text));
System.out.println("Text Encrypted: " + textEncrypted);
// Write the 'text' to a file
File encryptFileResult = new File("TestFiles/exampleOrig.txt");
if (!encryptFileResult.exists()) {
encryptFileResult.createNewFile();
} else {
encryptFileResult.delete();
encryptFileResult.createNewFile();
}
FileWriter encryptFileWriter = new FileWriter(encryptFileResult.getAbsoluteFile());
BufferedWriter bufferedWriter = new BufferedWriter(encryptFileWriter);
bufferedWriter.write(new String(textEncrypted));
bufferedWriter.close();
// Grab all bytes from the 'exampleOrig.txt' file
byte[] encryptedBytes = Files.readAllBytes(encryptFileResult.toPath());
// DECRYPT the text
aesCipher.init(Cipher.DECRYPT_MODE, myAesKey);
byte[] textDecrypted = aesCipher.doFinal(encryptedBytes);
System.out.println("Text Decrypted: " + new String(textDecrypted));
}
ОШИБКА СООБЩЕНИЕ:
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:913)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at main.AESEncryption.main(AESEncryption.java:50)
Соответствует ли это http://stackoverflow.com/questions/17234359/javax-crypto-illegalblocksizeexception-input-length-must-be-multiple-of-16-whe? – tenCupMaximum
Позвольте мне попробовать – user3851283
Нет, это не сработало. Я получаю то же сообщение об ошибке, что и выше. – user3851283