大佬教程收集整理的这篇文章主要介绍了Java AES 256加密,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
是的,它将是64个字符,即32个字节和256位,并且256位的任何序列都可以用作AES-256密钥。
我建议您使用DatatypeConverter.parseHexBinary(或您选择的库中的类似实用程序)将十六进制字符串转换为字节数组。
我有下面的Java代码来加密使用64个字符的密钥的字符串。我的问题是这将是AES-256加密吗?
String keyString = "C0BAE23DF8B51807B3E17D21925FADF273A70181E1D81B8EDE6C76A5C1F1716E";
byte[] keyValue = hexStringToByte(keyString);
Key key = new SecretKeySpec(keyValue,"AES");
Cipher c1 = Cipher.geTinstance("AES");
c1.init(Cipher.ENCRYPT_MODE,key);
String data = "Some data to encrypt";
byte[] encVal = c1.doFinal(data.getBytes());
String encryptedValue = Base64.encodeBase64String(encVal);
/* Copied the below code from another post in stackexchange */
public static byte[] hexStringToByte(String hexstr)
{
byte[] retVal = new BigInteger(hexstr,16).toByteArray();
if (retVal[0] == 0)
{
byte[] newArray = new byte[retVal.length - 1];
System.arraycopy(retVal,1,newArray,newArray.length);
return newArray;
}
return retVal;
}
以下是结合了divanov和laz建议的代码。
String keyString = "C0BAE23DF8B51807B3E17D21925FADF273A70181E1D81B8EDE6C76A5C1F1716E";
byte[] keyValue = DatatypeConverter.parseHexBinary(keyString);
Key key = new SecretKeySpec(keyValue,key);
String data = "Some data to encrypt";
byte[] encVal = c1.doFinal(data.getBytes());
String encryptedValue = Base64.encodeBase64String(encVal);
以上是大佬教程为你收集整理的Java AES 256加密全部内容,希望文章能够帮你解决Java AES 256加密所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。