简体   繁体   English

如何使用openssl解密由Java使用AES加密的数据

[英]How to use openssl to decrypt data encrypted by Java using AES

I am generating an AES public key and IV (Initialization Vector) in Java to encrypt data. 我正在用Java生成AES公钥和IV(初始化向量)来加密数据。 I need to decrypt this using openssl command. 我需要使用openssl命令对此进行解密。 Is this possible given that I store the AES Key and the IV on the disk? 如果我将AES密钥和IV存储在磁盘上,是否可以这样做?

Following is the encryption logic 以下是加密逻辑

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, iv);
    byte[] stringBytes = pass.getBytes();
    byte[] raw = cipher.doFinal(stringBytes);
    return Base64.encodeBase64String(raw);

Generating the AES Key 生成AES密钥

   SecretKey secret_key = KeyGenerator.getInstance("AES").generateKey();

Generating the IV 产生IV

   SecureRandom random = new SecureRandom();
   IvParameterSpec iv = new IvParameterSpec(random.generateSeed(16));

I am saving the IV and the AES Key in a file in a disk unencrypted. 我将IV和AES密钥保存在未加密的磁盘文件中。 How do I now decrypt data using these two parameters using openssl ? 现在如何使用openssl使用这两个参数解密数据?

Save them as hexadecimals and use eg 将它们另存为十六进制并使用例如

-K `cat key.hex` -iv `cat iv.hex`

so that's using the backticks instead of single quotation marks. 因此使用的是反引号而不是单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM