【问题标题】:AES - simple encrypt in Java, decrypt with opensslAES - 用 Ja​​va 简单加密,用 openssl 解密
【发布时间】:2014-03-24 13:25:52
【问题描述】:

我正在尝试使用 Java Cryto 在 Java 中进行简单的 AES 加密,然后可以使用 OpenSSL 在 ObjectiveC 中对其进行解密。

由于我不是在做 ObjectiveC 方面,我想确保它可以使用 openSSL 命令行,但我总是得到“坏幻数”

这是我的 Java 代码

public class EncryptionUtils {

private static final String AES_CIPHER_METHOD = "AES";
private static final int AES_KEY_SIZE = 128;

public static byte[] generateAesKey() throws NoSuchAlgorithmException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_CIPHER_METHOD);
    keyGenerator.init(AES_KEY_SIZE);
    SecretKey key = keyGenerator.generateKey();
    return key.getEncoded();
}

public static SecretKeySpec createAesKeySpec(byte[] aesKey) {
    return new SecretKeySpec(aesKey, AES_CIPHER_METHOD);
}

public static void aesEncryptFile(File in, File out, SecretKeySpec aesKeySpec) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IOException {
    Cipher aesCipher = Cipher.getInstance(AES_CIPHER_METHOD);
    aesCipher.init(Cipher.ENCRYPT_MODE, aesKeySpec);
    InputStream inputStream = new FileInputStream(in);
    try {
        OutputStream outputStream = new CipherOutputStream(new FileOutputStream(out), aesCipher);
        try {
            IOUtils.copy(inputStream , outputStream);
        } finally {
            outputStream.close();
        }
    } finally {
        inputStream.close();
    }
}
}


//testcode
@Test
public void testAesEncryptFile() throws IOException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
    byte[] aesKey = EncryptionUtils.generateAesKey();
    SecretKeySpec aesKeySpec = EncryptionUtils.createAesKeySpec(aesKey);
    EncryptionUtils.aesEncryptFile(new File("C:\\test\\test.txt"), new File("C:\\test\\test-encrypted.txt"), aesKeySpec);

    FileOutputStream outputStream = new FileOutputStream("C:\\test\\aes.key");
    outputStream.write(aesKey);
    outputStream.close();
}

@Test
public void testAesDecryptFile() throws IOException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
    FileInputStream keyFis = new FileInputStream("C:\\test\\aes.key");
    ByteArrayOutputStream keyBaos = new ByteArrayOutputStream();
    IOUtils.copy(keyFis, keyBaos);

    SecretKeySpec keySpec = new SecretKeySpec(keyBaos.toByteArray(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, keySpec);

    FileInputStream fileInputStream = new FileInputStream("C:\\test\\test-encrypted.txt");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(fileInputStream, baos);

    byte[] decrypted = cipher.doFinal(baos.toByteArray());
    FileOutputStream outputStream = new FileOutputStream("C:\\test\\test-decrypted.txt");
    outputStream.write(decrypted);
    outputStream.close();

}

现在按预期运行,文件“test-encrypted.txt”确实已加密,并且“test-decrypted.txt”==“test.txt”

然后我尝试使用 OpenSSL 在命令行上运行解密

openssl enc -d -aes-128-ecb -in test-encrypted.txt -k aes.key

然而,这总是给我

bad magic number

据我所知,Java 中使用的算法“AES”默认使用“ECB”模式,所以上面应该可以工作。我做错了什么。

【问题讨论】:

  • 您的文件中可能有一些无法打印的 ascii 字符?尝试使用-base64 选项。
  • 旁注:如果您想要 ECB 模式,请指定它。将"AES" 更改为"AES/ECB/<padding>" 以避免歧义。 (其中<padding> 是您首选的填充方案)。依赖默认值是不明智的做法。

标签: java encryption cryptography openssl


【解决方案1】:

问题确实是由 OpenSSL 根据密码计算出来的密钥造成的。

最可能的原因是 OpenSSL 有自己的算法从密码中派生密钥 EVP_BytesToKey,这与 Java 的不同。

我找到的唯一解决方案是使用该算法的 Java 重新实现:

private static final int KEY_LENGTH = 32;    

private byte[] deriveKey(String encryptionPassword, byte[] salt) throws NoSuchAlgorithmException {
    final byte[] passAndSalt = ArrayUtils.addAll(encryptionPassword.getBytes(), salt);
    byte[] hash = new byte[0];
    byte[] keyAndIv = new byte[0];
    for (int i = 0; i < 3 && keyAndIv.length < KEY_LENGTH; i++) {
        final byte[] dataToHash = ArrayUtils.addAll(hash, passAndSalt);
        final MessageDigest md = MessageDigest.getInstance("SHA-256");
        hash = md.digest(dataToHash);
        keyAndIv = ArrayUtils.addAll(keyAndIv, hash);
    }
    return Arrays.copyOfRange(keyAndIv, 0, KEY_LENGTH);
}

ArrayUtils 是 Apache Commons 库的一部分。

完全使用:

IvParameterSpec initializationVectorSpec = new IvParameterSpec(
                Hex.decodeHex(encryptionInitializationVector.toCharArray()));

cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] salt = new SecureRandom().generateSeed(8);
byte[] key = deriveKey(encryptionPassword, salt);
Key keySpec = new SecretKeySpec(key, "AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec, initializationVectorSpec);

byte[] rawEncryptedInput = cipher.doFinal(input.getBytes());
byte[] encryptedInputWithPrependedSalt = ArrayUtils.addAll(ArrayUtils.addAll(
                "Salted__".getBytes(), salt), rawEncryptedInput);
return Base64.getEncoder()
                .encodeToString(encryptedInputWithPrependedSalt);

感谢this answer 为我指路。

【讨论】:

  • 我无法让它按原样工作,也无法让 openssl 解密消息。但是当我切换到“AES/ECB/PKCS5Padding”时,它起作用了。
【解决方案2】:

问题出在钥匙上。 -k 参数需要密码,而不是文件。反过来,当 openssl 加密例程使用密码短语时,会在加密结果前面放置一个魔法和盐。这就是找不到的魔法。

要使用 openssl 命令行,以十六进制打印出密钥并使用 -K 选项而不是小写的 -k 选项。

你也可以使用:

`cat aes.key`

作为-K 之后的参数,假设 aes.key 包含十六进制的密钥。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 2013-11-22
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 2014-06-28
    相关资源
    最近更新 更多