Java中进行RSA加密解密

1、公私钥生成

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
     * 创建RSA密钥对
     *
     * @return List:0 公钥;1 私钥
     */    public static List<String> createRsaKey() {
        List<String> keyList = new ArrayList<String>();
 
        try {
            /**
             * 实例化密钥生成器
             */            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_RSA);
            /**
             * 初始化密钥生成器
             */            keyPairGenerator.initialize(SIZE_RSA);
            /**
             * 生成密钥对
             */            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            /**
             * 公钥
             */            RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
            /**
             * 私钥
             */            RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
            keyList.add(SignatureUtil.base64Encode(publicKey.getEncoded()));
            keyList.add(SignatureUtil.base64Encode(privateKey.getEncoded()));
        } catch (Exception e) {
            // Do nothing
        }
 
        return keyList;
}

2、公钥加密

01
02
03
04
05
06
07
08
09
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
/**
     * RSA公钥加密
     *
     * @param data
     *            待加密数据
     * @param key
     *            密钥
     * @return String 加密数据
     */    public static String rsaEncryptByPublicKey(String data, String key) {
        return base64Encode(rsaEncryptByPublicKey(data.getBytes(), base64DecodeBytes(key)));
    }
 
/**
     * RSA公钥加密
     *
     * @param data
     *            待加密数据
     * @param key
     *            密钥
     * @return byte[] 加密数据
     */    public static byte[] rsaEncryptByPublicKey(byte[] data, byte[] key) {
        byte[] result = null;
 
        try {
            /**
             * 实例化密钥工厂
             */            KeyFactory keyFactory = KeyFactory.getInstance(KEY_RSA);
            //
            /**
             * 初始化公钥, 密钥材料转换
             */            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
            /**
             * 产生公钥
             */            PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
            /**
             * 数据加密
             */            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            result = cipher.doFinal(data);
        } catch (Exception e) {
            // Do nothing
        }
 
        return result;
}

3、私钥加密

01
02
03
04
05
06
07
08
09
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
/**
     * RSA私钥加密
     *
     * @param data
     *            待加密数据
     * @param key
     *            密钥
     * @return String 加密数据
     */    public static String rsaEncryptByPrivateKey(String data, String key) {
        return base64Encode(rsaEncryptByPrivateKey(data.getBytes(), base64DecodeBytes(key)));
    }
 
/**
     * RSA私钥加密
     *
     * @param data
     *            待加密数据
     * @param key
     *            密钥
     * @return byte[] 加密数据
     */    public static byte[] rsaEncryptByPrivateKey(byte[] data, byte[] key) {
        byte[] result = null;
 
        try {
            /**
             * 取得私钥
             */            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_RSA);
            /**
             * 生成私钥
             */            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            /**
             * 数据加密
             */            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            result = cipher.doFinal(data);
        } catch (Exception e) {
            // Do nothing
        }
 
        return result;
}

4、公钥解密

01
02
03
04
05
06
07
08
09
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
/**
     * RSA公钥解密
     *
     * @param data
     *            待解密数据
     * @param key
     *            密钥
     * @return String 解密数据
     */    public static String rsaDecryptByPublicKey(String data, String key) {
        return new String(rsaDecryptByPublicKey(base64DecodeBytes(data), base64DecodeBytes(key)));
    }
 
/**
     * RSA公钥解密
     *
     * @param data
     *            待解密数据
     * @param key
     *            密钥
     * @return byte[] 解密数据
     */    public static byte[] rsaDecryptByPublicKey(byte[] data, byte[] key) {
        byte[] result = null;
 
        try {
            /**
             * 实例化密钥工厂
             */            KeyFactory keyFactory = KeyFactory.getInstance(KEY_RSA);
            //
            /**
             * 初始化公钥,密钥材料转换
             */            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
            /**
             * 产生公钥
             */            PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
            /**
             * 数据解密
             */            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, pubKey);
            result = cipher.doFinal(data);
        } catch (Exception e) {
            // Do nothing
        }
 
        return result;
}

5、私钥解密

01
02
03
04
05
06
07
08
09
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
/**
     * RSA私钥解密
     *
     * @param data
     *            待解密数据
     * @param key
     *            密钥
     * @return String 解密数据
     */    public static String rsaDecryptByPrivateKey(String data, String key) {
        return new String(rsaDecryptByPrivateKey(base64DecodeBytes(data), base64DecodeBytes(key)));
    }
 
/**
     * RSA私钥解密
     *
     * @param data
     *            待解密数据
     * @param key
     *            密钥
     * @return byte[] 解密数据
     */    public static byte[] rsaDecryptByPrivateKey(byte[] data, byte[] key) {
        byte[] result = null;
 
        try {
            /**
             * 取得私钥
             */            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_RSA);
            /**
             * 生成私钥
             */            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
            /**
             * 数据解密
             */            Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            result = cipher.doFinal(data);
        } catch (Exception e) {
            // Do nothing
        }
 
        return result;
    }

6、工具方法与常量

01
02
03
04
05
06
07
08
09
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
public static final String KEY_RSA = "RSA";
 
private static final int SIZE_RSA = 512;
 
/**
     * base64编码
     *
     * @param data
     * @return
     */    public static String base64Encode(byte[] data) {
        String result = null;
 
        if (data != null) {
            try {
                result = Base64.encodeBase64String(data);
            } catch (Exception e) {
                // Do nothing
            }
        }
 
        return result;
    }
 
/**
     * base64解码
     *
     * @param data
     * @return
     */    public static byte[] base64DecodeBytes(String data) {
        byte[] result = null;
 
        if (data != null) {
            result = Base64.decodeBase64(data);
        }
 
        return result;
}

发表评论

欢迎阅读『Java中进行RSA加密解密|Java、加密解密、算法|Nick Tan-梓潼Blog』