自动化立体仓库 - WMS系统
1
ZY
21 小时以前 1b5c0044b4178c7314781ac36f897b781de72064
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.zy.nc.util;
 
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
 
/**
 * Key������
 *
 * @author liyang
 */
public class KeysFactory {
 
    /**
     * buildAsymKey ����һ��ǶԳ���Կ
     *
     * @return KeyPair key��PublicKey��PrivateKey
     * @throws NoSuchAlgorithmException
     */
    public static KeyPairs buildAsymKey() throws Exception {
 
        /* ��ʼ����Կ������ */
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(CipherConstant.RSA);
        keyPairGenerator.initialize(2048, SecureRandomProxy.getRandomInstance());
 
        /* ������Կ */
        return new KeyPairs(keyPairGenerator.generateKeyPair());
    }
 
    /**
     * buildAsymKey ����һ���Գ���Կ
     *
     * @return �Գ���Կ
     * @throws NoSuchAlgorithmException
     * @throws Exception
     */
    public static String buildSymKey() throws Exception {
        // ����Key
        KeyGenerator keyGenerator = KeyGenerator.getInstance(CipherConstant.AES);
 
        keyGenerator.init(256, SecureRandomProxy.getRandomInstance());
        // ʹ���������ֳ�ʼ�����������ض�������������Կ���������ܺ��������Ψһ�̶��ġ�
        SecretKey secretKey = keyGenerator.generateKey();
 
        return Base64Util.encryptBASE64(secretKey.getEncoded());
 
    }
 
    public static Key getPublicKey(String pubKey) throws Exception {
        Key key = null;
 
        try {
            byte[] keyBytes = Base64Util.decryptBASE64(pubKey);
            KeyFactory keyFactory = KeyFactory.getInstance(CipherConstant.RSA);
 
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
            key = keyFactory.generatePublic(x509KeySpec);
 
        } catch (Exception e) {
            throw new Exception("��Ч����Կ  " + e.getMessage());
        }
 
        return key;
    }
 
    public static Key getPrivateKey(String priKey) throws Exception {
        Key key = null;
 
        try {
            byte[] keyBytes = Base64Util.decryptBASE64(priKey);
 
            KeyFactory keyFactory = KeyFactory.getInstance(CipherConstant.RSA);
 
            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
            key = keyFactory.generatePrivate(pkcs8KeySpec);
 
        } catch (Exception e) {
            throw new Exception("��Ч��Կ " + e.getMessage());
        }
 
        return key;
    }
 
    public static Key getSymKey(String symKey) throws Exception {
        Key key = null;
 
        try {
            byte[] keyBytes = Base64Util.decryptBASE64(symKey);
            // Keyת��
            key = new SecretKeySpec(keyBytes, CipherConstant.AES);
        } catch (Exception e) {
            throw new Exception("��Ч��Կ " + e.getMessage());
        }
 
        return key;
    }
}