本文实例讲述了java实现的AES秘钥生成算法。分享给大家供大家参考,具体如下:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; public class Test { public static void main(String[] args) { getKey(); getKeyByPass(); } /** * 随机生成秘钥 */ public static void getKey() { try { KeyGenerator kg = KeyGenerator.getInstance( "AES" ); kg.init( 128 ); //要生成多少位,只需要修改这里即可128, 192或256 SecretKey sk = kg.generateKey(); byte [] b = sk.getEncoded(); String s = byteToHexString(b); System.out.println(s); System.out.println( "十六进制密钥长度为" +s.length()); System.out.println( "二进制密钥的长度为" +s.length()* 4 ); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); System.out.println( "没有此算法。" ); } } /** * 使用指定的字符串生成秘钥 */ public static void getKeyByPass() { //生成秘钥 String password= "testkey" ; try { KeyGenerator kg = KeyGenerator.getInstance( "AES" ); // kg.init(128);//要生成多少位,只需要修改这里即可128, 192或256 //SecureRandom是生成安全随机数序列,password.getBytes()是种子,只要种子相同,序列就一样,所以生成的秘钥就一样。 kg.init( 128 , new SecureRandom(password.getBytes())); SecretKey sk = kg.generateKey(); byte [] b = sk.getEncoded(); String s = byteToHexString(b); System.out.println(s); System.out.println( "十六进制密钥长度为" +s.length()); System.out.println( "二进制密钥的长度为" +s.length()* 4 ); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); System.out.println( "没有此算法。" ); } } /** * byte数组转化为16进制字符串 * @param bytes * @return */ 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(); } } |
希望本文所述对大家java程序设计有所帮助。