AES-256-CBC in Java(Java 中的 AES-256-CBC)
问题描述
我正在尝试编写一个简单的 Java 程序,该程序将使用 AES-256-CBC
加密纯文本.有课:
I'm trying to write a simple Java program that will encrypt plain text with AES-256-CBC
. There is class:
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AesCBC {
private byte[] key;
private byte[] iv;
private static final String ALGORITHM="AES";
public AesCBC(byte[] key, byte[] iv) {
this.key = key;
this.iv = iv;
}
public byte[] encrypt(byte[] plainText) throws Exception{
SecretKeySpec secretKey=new SecretKeySpec(key,ALGORITHM);
IvParameterSpec ivParameterSpec=new IvParameterSpec(iv);
Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,secretKey,ivParameterSpec);
return cipher.doFinal(plainText);
}
public byte[] getKey() {
return key;
}
public void setKey(byte[] key) {
this.key = key;
}
public byte[] getIv() {
return iv;
}
public void setIv(byte[] iv) {
this.iv = iv;
}
}
还有可能的用法:
byte[] test="a".getBytes();
byte[] key=DatatypeConverter.parseHexBinary("b38b730d4cc721156e3760d1d58546ce697adc939188e4c6a80f0e24e032b9b7");
byte[] iv=DatatypeConverter.parseHexBinary("064df9633d9f5dd0b5614843f6b4b059");
AesCBC aes=new AesCBC(key,iv);
try{
String result=DatatypeConverter.printBase64Binary(aes.encrypt(test));
System.out.println(result);
}catch(Exception e){
e.printStackTrace();
}
我的输出是VTUOJJp38Tk+P5ikR4YLfw==
,但是当我执行这个命令时:
My output is VTUOJJp38Tk+P5ikR4YLfw==
, but when I execute this command:
/usr/bin/openssl enc -A -aes-256-cbc -base64 -K "b38b730d4cc721156e3760d1d58546ce697adc939188e4c6a80f0e24e032b9b7" -iv "064df9633d9f5dd0b5614843f6b4b059" <<< "a"
我得到的东西与 Java 程序(Y65q9DFdR3k1XcWhA2AO2Q==
)不同.可悲的是,我不知道为什么结果不一样,因为我使用相同的算法和相同的密钥和 iv.这是否意味着我的 Java 程序不能正常工作?任何帮助将不胜感激.
I get something diffrent than in Java program( Y65q9DFdR3k1XcWhA2AO2Q==
). Sadly I have no idea why results aren't the same since I use the same algorithm with the same key and iv. Does it mean my Java program doesn't work properly? Any help would be appreciated.
推荐答案
两种方式都可以正常工作,但是你加密的是不同的东西.
Both ways are working correctly, however you are encrypting different things.
此处的字符串语法 (<<<
) 在字符串中添加换行符.所以Java输出是加密a"的结果,命令行输出是加密a
"的结果(即字符a
后跟换行符).
The here string syntax (<<<
) adds a newline to the string. So the Java output is the result of encrypting "a", and the command line output is the result of encrypting "a
" (i.e. the character a
followed by a newline).
从命令行试试这个:
printf "a" | /usr/bin/openssl enc -aes-256-cbc -base64 -K "b38b730d4cc721156e3760d1d58546ce697adc939188e4c6a80f0e24e032b9b7" -iv "064df9633d9f5dd0b5614843f6b4b059"
结果是 VTUOJJp38Tk+P5ikR4YLfw==
,与您的 Java 结果匹配.
the result is VTUOJJp38Tk+P5ikR4YLfw==
, matching your Java result.
这篇关于Java 中的 AES-256-CBC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 中的 AES-256-CBC
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01