gpt4 book ai didi

java - 如何使用 java 解码用 openssl aes-128-cbc 编码的字符串?

转载 作者:行者123 更新时间:2023-11-29 03:06:32 25 4
gpt4 key购买 nike

我正在使用 openssl 使用以下命令对 string 进行编码:

openssl enc -aes-128-cbc -a -salt -pass pass:mypassword <<< "stackoverflow"

结果给我一个编码字符串:U2FsdGVkX187CGv6DbEpqh/L6XRKON7uBGluIU0nT3w=

到目前为止,我只需要使用 openssl 对其进行解码,因此以下命令会返回之前编码的字符串:

 openssl enc -aes-128-cbc -a -salt -pass pass:mypassword -d <<< "U2FsdGVkX187CGv6DbEpqh/L6XRKON7uBGluIU0nT3w="

结果:stackoverflow

现在,我需要在 java 应用程序 中解码编码字符串。

我的问题是:

有没有人能提供一个简单的 java 类来解码用先前给定的 openssl 命令编码的字符串?

非常感谢。

最佳答案

使用 Bouncy CaSTLe 库解决了它。

代码如下:

package example;
import java.util.Arrays;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.BlockCipherPadding;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;

public class OpenSSLAesDecrypter
{
private static final int AES_NIVBITS = 128; // CBC Initialization Vector (same as cipher block size) [16 bytes]

private final int keyLenBits;

public OpenSSLAesDecrypter(int nKeyBits)
{
this.keyLenBits = nKeyBits;
}

public byte[] decipher(byte[] pwd, byte[] src)
{
// openssl non-standard extension: salt embedded at start of encrypted file
byte[] salt = Arrays.copyOfRange(src, 8, 16); // 0..7 is "SALTED__", 8..15 is the salt

try
{
// Encryption algorithm. Note that the "strength" (bitsize) is controlled by the key object that is used.
// Note that PKCS5 padding and PKCS7 padding are identical.
BlockCipherPadding padding = new PKCS7Padding();
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), padding);

CipherParameters params = getCipherParameters(pwd, salt);
cipher.reset();
cipher.init(false, params);

int buflen = cipher.getOutputSize(src.length - 16);
byte[] workingBuffer = new byte[buflen];
int len = cipher.processBytes(src, 16, src.length - 16, workingBuffer, 0);
len += cipher.doFinal(workingBuffer, len);

// Note that getOutputSize returns a number which includes space for "padding" bytes to be stored in.
// However we don't want these padding bytes; the "len" variable contains the length of the *real* data
// (which is always less than the return value of getOutputSize.
byte[] bytesDec = new byte[len];
System.arraycopy(workingBuffer, 0, bytesDec, 0, len);
return bytesDec;
}
catch (InvalidCipherTextException e)
{
System.err.println("Error: Decryption failed");
return null;
}
catch (RuntimeException e)
{
System.err.println("Error: Decryption failed");
return null;
}
}

private CipherParameters getCipherParameters(byte[] pwd, byte[] salt)
{
// Use bouncycastle implementation of openssl non-standard (pwd,salt)->(key,iv) algorithm.
// Note that if a "CBC" cipher is selected, then an IV is required as well as a key. When using a password,
// Openssl
// *derives* the IV from the (pwd,salt) pair at the same time as it derives the key.
//
// * PBE = Password Based Encryption
// * CBC = Cipher Block Chaining (ie IV is needed)
//
// Note also that when the IV is derived from (pwd, salt) the salt **must** be different for each message; this is
// the default for openssl - just make sure to NOT explicitly provide a salt, or encryption security is badly
// affected.
OpenSSLPBEParametersGenerator gen = new OpenSSLPBEParametersGenerator();
gen.init(pwd, salt);
CipherParameters cp = gen.generateDerivedParameters(keyLenBits, AES_NIVBITS);
return cp;
}

public static void main(String[] args)
{
OpenSSLAesDecrypter d = new OpenSSLAesDecrypter(128);
String r = new String(d.decipher("mypassword".getBytes(),
Base64.decodeBase64("U2FsdGVkX187CGv6DbEpqh/L6XRKON7uBGluIU0nT3w=")));
System.out.println(r);
}
}

使用以下依赖项来编译/运行它:

关于java - 如何使用 java 解码用 openssl aes-128-cbc 编码的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31947256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com