- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找任何示例java代码,只要 key 已知,该代码将解密使用“openssl enc -aes-256-cbc) -a -salt”命令加密的消息。
到目前为止,我能够获得以下加密和解密消息的 java 代码。但我无法使用 openssl 命令解密加密的消息。出现“错误的魔数(Magic Number)”错误。有什么想法吗?
Encrypt the message using the code >
Encrypt("sample text", "test$password") = "i+5zkPPgnDdV7fr/w8uHkw=="
Decrypt("i+5zkPPgnDdV7fr/w8uHkw==", "test$password") = "sample text"
Decrypt the message using openssl >
F:\cipher>echo i+5zkPPgnDdV7fr/w8uHkw== | openssl aes-256-cbc -a -salt -d
enter aes-256-cbc decryption password:
bad magic number
import java.security.spec.KeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class AES {
private static final byte[] SALT = {
(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
(byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03
};
private static final int ITERATION_COUNT = 65536;
private static final int KEY_LENGTH = 256;
private Cipher ecipher;
private Cipher dcipher;
AES(String passPhrase) throws Exception {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
ecipher.init(Cipher.ENCRYPT_MODE, secret);
dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = ecipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
dcipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
}
public String encrypt(String encrypt) throws Exception {
byte[] bytes = encrypt.getBytes("UTF8");
byte[] encrypted = encrypt(bytes);
return Base64.getEncoder().encodeToString(encrypted);
}
public byte[] encrypt(byte[] plain) throws Exception {
return ecipher.doFinal(plain);
}
public String decrypt(String encrypt) throws Exception {
byte[] bytes = Base64.getDecoder().decode(encrypt);
byte[] decrypted = decrypt(bytes);
return new String(decrypted, "UTF8");
}
public byte[] decrypt(byte[] encrypt) throws Exception {
return dcipher.doFinal(encrypt);
}
public static void main(String[] args) throws Exception {
String message = "sample text";
String password = "test$password";
AES encrypter = new AES(password);
String encrypted = encrypter.encrypt(message);
String decrypted = encrypter.decrypt(encrypted);
System.out.println("Encrypt(\"" + message + "\", \"" + password + "\") = \"" + encrypted + "\"");
System.out.println("Decrypt(\"" + encrypted + "\", \"" + password + "\") = \"" + decrypted + "\"");
}
}
最佳答案
您可以在 stackoverflow 中搜索许多类似的问题。
您的代码中有多个问题:
在 Java 中,您使用 PBKDF2 从提供的密码生成加密 key 。 Openssl 使用其 EVP_BytesToKey 。在互联网上搜索 Java 实现。请注意,EVP_BytesToKey 中使用的哈希值随某些 openssl 版本的变化(从 MD5 更改为 SHA-1 SHA-256),如果有人有更多详细信息,请评论
并且您使用随机 IV。你没有沿着密文传递IV,所以你也许能够使用相同的密码实例解密密文(保持相同的iv),但是让我们尝试你的Java代码在其他时间或使用其他实例解密你的密文,它行不通的。您需要沿着密文传递 IV(通常是前置的)
Openssl 需要以下格式:
<删除> Salted_<8 byte salt>ciphertext
Salted__<8 byte salt>ciphertext
8 字节 salt 是一个随机字节数组,用于根据提供的密码生成加密 key 和 IV。尝试使用 openssl 进行加密 -p
参数,它会打印生成的salt、IV和Key,以便您检查和比较
建议:
-K/-iv
参数提供加密 key 和 IV(十六进制格式)而不是密码,然后 openssl 需要纯密文(输入中没有 Salted_ 或 salt)关于java - 正在寻找用于解密使用 openssl -aes-256-cbc -a -salt 命令加密的消息的 Java 实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55175864/
我是一名优秀的程序员,十分优秀!