我有下面的 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, 0, newArray.length);
return newArray;
}
return retVal;
}
以下是结合 divanov 和 laz 的建议后的代码。
String keyString = "C0BAE23DF8B51807B3E17D21925FADF273A70181E1D81B8EDE6C76A5C1F1716E";
byte[] keyValue = DatatypeConverter.parseHexBinary(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);
是的,它会,因为 64 个字符是 32 字节和 256 位,任何 256 位序列都可以用作 AES-256 密钥。
我建议你使用数据类型转换器.parseHexBinary http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/DatatypeConverter.html#parseHexBinary%28java.lang.String%29(或您选择的库中的类似实用程序)将十六进制字符串转换为字节数组。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)