JAVA RSA非對稱加密詳解
阿新 • • 發佈:2019-02-05
原文:原文地址
package com.rsa; import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import java.security.*; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; /** * 非對稱加密演算法RSA演算法元件 * 非對稱演算法一般是用來傳送對稱加密演算法的金鑰來使用的,相對於DH演算法,RSA演算法只需要一方構造金鑰,不需要 * 大費周章的構造各自本地的金鑰對了。DH演算法只能演算法非對稱演算法的底層實現。而RSA演算法演算法實現起來較為簡單 * * @author kongqz */ public class RSACoder { //非對稱金鑰演算法 public static final String KEY_ALGORITHM = "RSA"; /** * 金鑰長度,DH演算法的預設金鑰長度是1024 * 金鑰長度必須是64的倍數,在512到65536位之間 */ private static final int KEY_SIZE = 512; //公鑰 private static final String PUBLIC_KEY = "RSAPublicKey"; //私鑰 private static final String PRIVATE_KEY = "RSAPrivateKey"; /** * 初始化金鑰對 * * @return Map 甲方金鑰的Map */ public static Map<String, Object> initKey() throws Exception { //例項化金鑰生成器 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM); //初始化金鑰生成器 keyPairGenerator.initialize(KEY_SIZE); //生成金鑰對 KeyPair keyPair = keyPairGenerator.generateKeyPair(); //甲方公鑰 RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); //甲方私鑰 RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); //將金鑰儲存在map中 Map<String, Object> keyMap = new HashMap<String, Object>(); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; } /** * 私鑰加密 * * @param data 待加密資料 * @param key 金鑰 * @return byte[] 加密資料 */ public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception { //取得私鑰 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); //生成私鑰 PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); //資料加密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(data); } /** * 公鑰加密 * * @param data 待加密資料 * @param key 金鑰 * @return byte[] 加密資料 */ public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception { //例項化金鑰工廠 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); //初始化公鑰 //金鑰材料轉換 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); //產生公鑰 PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); //資料加密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, pubKey); return cipher.doFinal(data); } /** * 私鑰解密 * * @param data 待解密資料 * @param key 金鑰 * @return byte[] 解密資料 */ public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception { //取得私鑰 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); //生成私鑰 PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); //資料解密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } /** * 公鑰解密 * * @param data 待解密資料 * @param key 金鑰 * @return byte[] 解密資料 */ public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception { //例項化金鑰工廠 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); //初始化公鑰 //金鑰材料轉換 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); //產生公鑰 PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); //資料解密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, pubKey); return cipher.doFinal(data); } /** * 取得私鑰 * * @param keyMap 金鑰map * @return byte[] 私鑰 */ public static byte[] getPrivateKey(Map<String, Object> keyMap) { Key key = (Key) keyMap.get(PRIVATE_KEY); return key.getEncoded(); } /** * 取得公鑰 * * @param keyMap 金鑰map * @return byte[] 公鑰 */ public static byte[] getPublicKey(Map<String, Object> keyMap) throws Exception { Key key = (Key) keyMap.get(PUBLIC_KEY); return key.getEncoded(); } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //初始化金鑰 //生成金鑰對 Map<String, Object> keyMap = RSACoder.initKey(); //公鑰 byte[] publicKey = RSACoder.getPublicKey(keyMap); //私鑰 byte[] privateKey = RSACoder.getPrivateKey(keyMap); System.out.println("公鑰:/n" + Base64.encodeBase64String(publicKey)); System.out.println("私鑰:/n" + Base64.encodeBase64String(privateKey)); System.out.println("================金鑰對構造完畢,甲方將公鑰公佈給乙方,開始進行加密資料的傳輸============="); String str = "RSA密碼交換演算法"; System.out.println("/n===========甲方向乙方傳送加密資料=============="); System.out.println("原文:" + str); //甲方進行資料的加密 byte[] code1 = RSACoder.encryptByPrivateKey(str.getBytes(), privateKey); System.out.println("加密後的資料:" + Base64.encodeBase64String(code1)); System.out.println("===========乙方使用甲方提供的公鑰對資料進行解密=============="); //乙方進行資料的解密 byte[] decode1 = RSACoder.decryptByPublicKey(code1, publicKey); System.out.println("乙方解密後的資料:" + new String(decode1) + "/n/n"); System.out.println("===========反向進行操作,乙方向甲方傳送資料==============/n/n"); str = "乙方向甲方傳送資料RSA演算法"; System.out.println("原文:" + str); //乙方使用公鑰對資料進行加密 byte[] code2 = RSACoder.encryptByPublicKey(str.getBytes(), publicKey); System.out.println("===========乙方使用公鑰對資料進行加密=============="); System.out.println("加密後的資料:" + Base64.encodeBase64String(code2)); System.out.println("=============乙方將資料傳送給甲方======================"); System.out.println("===========甲方使用私鑰對資料進行解密=============="); //甲方使用私鑰對資料進行解密 byte[] decode2 = RSACoder.decryptByPrivateKey(code2, privateKey); System.out.println("甲方解密後的資料:" + new String(decode2)); } }