是否有任何示例 Java 代码可以像本网站一样进行 AES 加密?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13102788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any sample Java code that does AES encryption exactly like this website?
提问by user1068636
http://www.hanewin.net/encrypt/aes/aes-test.htm
http://www.hanewin.net/encrypt/aes/aes-test.htm
If you go to this website and enter the following:
如果您访问此网站并输入以下内容:
"Key In Hex": 00000000000000000000000000123456
"Plain Text in Hex": 00000000000000000000000000000000
And click on "Encrypt" button you will see the ciphertext in hex is:
然后点击“加密”按钮,你会看到十六进制的密文是:
3fa9f2a6e4c2b440fb6f676076a8ba04
Is there a Java program out there that I can do this (I.e. Is there an AES library that will input the "Key In Hex" above with the "Plain Text In Hex" above and generate the Ciphertext in Hex above? )?
有没有我可以执行此操作的 Java 程序(即是否有一个 AES 库可以输入上面的“十六进制密钥”和上面的“十六进制纯文本”并生成上面的十六进制密文?)?
I would appreciate any advice or links to Java sample code that does this.
如果您有任何建议或指向执行此操作的 Java 示例代码的链接,我将不胜感激。
回答by Duncan Jones
See the code below for the standard way to do this with JCE classes.
有关使用 JCE 类执行此操作的标准方法,请参阅下面的代码。
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
public class EncryptionExample {
public static void main(String[] args) throws Exception {
final String keyHex = "00000000000000000000000000123456";
final String plaintextHex = "00000000000000000000000000000000";
SecretKey key = new SecretKeySpec(DatatypeConverter
.parseHexBinary(keyHex), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(DatatypeConverter
.parseHexBinary(plaintextHex));
System.out.println(DatatypeConverter.printHexBinary(result));
}
}
Prints:
印刷:
3FA9F2A6E4C2B440FB6F676076A8BA04
3FA9F2A6E4C2B440FB6F676076A8BA04
回答by Chand Priyankara
instead of converting bytes to HEX, you may convert to Base64 as well. I like using Apache Commons for this. Here is an example.
除了将字节转换为 HEX,您也可以转换为 Base64。我喜欢为此使用 Apache Commons。这是一个例子。
To compile you need additional Apache Commons Codec jar, which is available here:
要编译,您需要额外的 Apache Commons Codec jar,可在此处获得:
http://commons.apache.org/proper/commons-codec/download_codec.cgi
http://commons.apache.org/proper/commons-codec/download_codec.cgi
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class Encryptor {
public static String encrypt(String key1, String key2, String value) {
try {
IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),
"AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
System.out.println("encrypted string:"
+ Base64.encodeBase64String(encrypted));
return Base64.encodeBase64String(encrypted);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static String decrypt(String key1, String key2, String encrypted) {
try {
IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),
"AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
return new String(original);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String key1 = "Bar12345Bar12345"; // 128 bit key
String key2 = "ThisIsASecretKet";
System.out.println(decrypt(key1, key2,
encrypt(key1, key2, "Hello World")));
}
}