Java 如何生成 128 位密钥以用于 AES 算法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23361224/
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
How to generate a 128 bit key to use in AES algorithm
提问by brig
I am using the code in below mentioned post to encrypt and decrypt values between Java and Java script module of my application.
我正在使用下面提到的帖子中的代码来加密和解密我的应用程序的 Java 和 Java 脚本模块之间的值。
Compatible AES algorithm for Java and Javascript
适用于 Java 和 Javascript 的兼容 AES 算法
In a above post they are using 128 bit key value. I want to use my own key instead of hard coding the 128 bit key value.
在上面的帖子中,他们使用 128 位密钥值。我想使用我自己的密钥而不是硬编码 128 位密钥值。
My question is that can I convert any random string into 128 bit key value.
我的问题是我可以将任何随机字符串转换为 128 位密钥值。
Please post some examples if it is possible to convert any string into 128 bit value.
如果可以将任何字符串转换为 128 位值,请发布一些示例。
回答by Anirban Basak
Characters are represented with 8 bits. hence to form 128 bit key, create a string having 16 chars (16*8=128), e.g. "abcdefgh12345678".
字符用 8 位表示。因此要形成 128 位密钥,请创建一个具有 16 个字符 (16*8=128) 的字符串,例如“abcdefgh12345678”。
to mask this key as base64, you may use Apache commons-codec Base64.encodeBase64... @see http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html
将此密钥屏蔽为base64,您可以使用Apache commons-codec Base64.encodeBase64 ... @see http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/ 64.html
回答by Mike bovquier
Something I fount by google, and use in my project:
我通过谷歌找到的东西,并在我的项目中使用:
private final static String algorithm = "PBKDF2WithHmacSHA1";
private final static String HEX = "0123456789ABCDEF";
private static final String CP_ALGORITH = "AES";
private static final String CP_KEY = "PUTsomeKEYinHere";
public static String cipher(String cipherKey, String data) throws NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
KeySpec spec = new PBEKeySpec(cipherKey.toCharArray(), cipherKey.getBytes(), 128, 256);
SecretKey tmp = skf.generateSecret(spec);
SecretKey key = new SecretKeySpec(tmp.getEncoded(), CP_ALGORITH);
Cipher cipher = Cipher.getInstance(CP_ALGORITH);
cipher.init(Cipher.ENCRYPT_MODE, key);
return toHex(cipher.doFinal(data.getBytes()));
}
public static String decipher(String cipherKey, String data) throws NoSuchAlgorithmException,
InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
KeySpec spec = new PBEKeySpec(cipherKey.toCharArray(), cipherKey.getBytes(), 128, 256);
SecretKey tmp = skf.generateSecret(spec);
SecretKey key = new SecretKeySpec(tmp.getEncoded(), CP_ALGORITH);
Cipher cipher = Cipher.getInstance(CP_ALGORITH);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(toByte(data)));
}
private static byte[] toByte(String data) throws NullPointerException{
int len = data.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(data.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
private static String toHex(byte[] doFinal) {
StringBuffer result = new StringBuffer(2*doFinal.length);
for (int i = 0; i < doFinal.length; i++) {
result.append(HEX.charAt((doFinal[i]>>4)&0x0f)).append(HEX.charAt(doFinal[i]&0x0f));
}
return result.toString();
}
usage:
用法:
cipher(CP_KEY, STRINGtoCIPHER);
decipher(CP_KEY, YOURcipheredSTRING)
I put that all in a shared class with static fields and methods, so i can use it everywhere in my app. I use it to store SessionID i shared preferences, and it works nice.
我把所有这些都放在一个带有静态字段和方法的共享类中,所以我可以在我的应用程序中的任何地方使用它。我用它来存储我共享的首选项的 SessionID,它运行良好。