-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAESKey.java
More file actions
45 lines (39 loc) · 1.33 KB
/
AESKey.java
File metadata and controls
45 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.lzk.gdut.audio.sm;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import org.apache.commons.codec.binary.Base64;
public class AESKey {
public static byte[] initKey() throws Exception{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey skey = kgen.generateKey();
return skey.getEncoded();
}
public static String getkey() {
String str=null;
try {
byte[] key = AESKey.initKey();
str =byteToHexString(key);
} catch (Exception e) {
e.printStackTrace();
}
String key=str.substring(0, 16).replaceAll("a", "A").replace("2", "G").replaceAll("d", "Q").replaceAll("3", "E");
return key;
}
public static String byteToHexString(byte[] bytes){
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String strHex=Integer.toHexString(bytes[i]);
if(strHex.length() > 3){
sb.append(strHex.substring(6));
} else {
if(strHex.length() < 2){
sb.append("0" + strHex);
} else {
sb.append(strHex);
}
}
}
return sb.toString();
}
}