【问题标题】:How to decrypt String from AES Encryption?如何从 AES 加密中解密字符串?
【发布时间】:2016-10-27 01:38:23
【问题描述】:

所以我在使用 java AES 加密解密文件或字符串时遇到问题。所以我有一个包含二进制数的 text 变量。我创建了一个随机密钥。然后我加密文本并返回加密的 byte[] 并将其写入 .txt 文件。并且加密过程有效。

然后我从 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)

【问题讨论】:

标签: java encryption cryptography


【解决方案1】:
System.out.println("Text : " + new String(text));
System.out.println("Text Encrypted: " + textEncrypted);

您上面的代码对加密数据毫无意义,因为加密数据是二进制的。您应该循环遍历字节数组并通过将它们转换为 HEX 来打印每个字节:

for (byte b : textEncrypted) {
  if ((b & 0xFF) < 0x10) {
    System.out.print("0");
  }
  System.out.print(Integer.toHexString(b & 0xFF).toUpperCase() + " ");
}
System.out.println();

您的错误是因为 FileWriter。 FileWriter 用于写入字符数据,您应该使用 FileOutputStream 代替。

比较字节数组 textEncrypted 和 encryptedBytes 你会看到。

【讨论】:

  • HEX 将是什么数据类型?
  • 我在新文件中写入十六进制字符串值后,是否将该文件转换为字节并运行解密方法?
  • HEX 表示十六进制,即基数 16。您的加密数据始终是一个字节 []。写入文件时不需要转换任何内容,FileOutputStream.write() 具有重载版本来写入单个字节或整个字节[]。我上面提供的 for 循环仅用于以人类可读的格式打印 byte[]。
猜你喜欢
  • 1970-01-01
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
  • 2016-01-04
相关资源
最近更新 更多