在这里,您需要了解的是密文可能包含不可打印的字符。因此,当您使用readLine()时,它可能不会为您提供文件中的所有字节。
同样,
byteCipherText.toString()它并没有给您您认为会得到的。在Java中,该
toString()方法不提供数组内容的字符串表示形式。
无需在加密的文本中添加填充。它已经被填充。
import java.nio.file.Files;import java.nio.file.Paths;import javax.crypto.*;public class Main { public static void main(String[] args) throws Exception { String fileName = "encryptedtext.txt"; String fileName2 = "decryptedtext.txt"; KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey secKey = keyGen.generateKey(); Cipher aesCipher = Cipher.getInstance("AES"); byte[] byteText = "Your Plain Text Here".getBytes(); aesCipher.init(Cipher.ENCRYPT_MODE, secKey); byte[] byteCipherText = aesCipher.doFinal(byteText); Files.write(Paths.get(fileName), byteCipherText); byte[] cipherText = Files.readAllBytes(Paths.get(fileName)); aesCipher.init(Cipher.DECRYPT_MODE, secKey); byte[] bytePlainText = aesCipher.doFinal(cipherText); Files.write(Paths.get(fileName2), bytePlainText); }}


