RSA加密、解密、公钥私钥生成
有时项目中需要用到一些加密和解密工具,这里之前整理了一个demo,记录一下,方便查询
1 package com.test; 2 3 import java.security.KeyFactory; 4 import java.security.KeyPair; 5 import java.security.KeyPairGenerator; 6 import java.security.NoSuchAlgorithmException; 7 import java.security.PrivateKey; 8 import java.security.PublicKey; 9 import java.security.interfaces.RSAPrivateKey; 10 import java.security.interfaces.RSAPublicKey; 11 import java.security.spec.PKCS8EncodedKeySpec; 12 import java.security.spec.X509EncodedKeySpec; 13 import java.util.HashMap; 14 import java.util.Map; 15 import javax.crypto.Cipher; 16 import org.apache.commons.codec.binary.Base64; 17 18 public class RSAsecurity { 19 20 public static Map<String,String> getKey(int length){ 21 try { 22 //1.初始化秘钥 23 24 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); 25 26 //秘钥长度 27 keyPairGenerator.initialize(length); 28 //初始化秘钥对 29 KeyPair keyPair = keyPairGenerator.generateKeyPair(); 30 //公钥 31 RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); 32 //私钥 33 RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); 34 35 byte[] publicKeyByte = rsaPublicKey.getEncoded(); 36 37 byte[] privateKeyByte = rsaPrivateKey.getEncoded(); 38 39 String publicKey = Base64.encodeBase64String(publicKeyByte); 40 41 String privateKey = Base64.encodeBase64String(privateKeyByte); 42 43 Map<String,String> map = new HashMap<String,String>(); 44 45 map.put("publicKey", publicKey); 46 47 map.put("privateKey", privateKey); 48 49 return map; 50 51 } catch (NoSuchAlgorithmException e) { 52 // TODO Auto-generated catch block 53 e.printStackTrace(); 54 } 55 return new HashMap<String,String>(); 56 } 57 58 public static void main(String[] args) { 59 //Map<String,String> map = RSAsecurity.getKey(1024); 60 //System.out.println(map.get("publicKey")); 61 RSAsecurity.pubENpriDE(); 62 //System.out.println("---"); 63 } 64 65 /*RSA 工具类。提供加密,解密,生成密钥对等方法。 66 RSA加密原理概述 67 RSA的安全性依赖于大数的分解,公钥和私钥都是两个大素数(大于100的十进制位)的函数。 68 据猜测,从一个密钥和密文推断出明文的难度等同于分解两个大素数的积 69 密钥的产生: 70 1.选择两个大素数 p,q ,计算 n=p*q; 71 2.随机选择加密密钥 e ,要求 e 和 (p-1)*(q-1)互质 72 3.利用 Euclid 算法计算解密密钥 d , 使其满足 e*d = 1(mod(p-1)*(q-1)) (其中 n,d 也要互质) 73 4:至此得出公钥为 (n,e) 私钥为 (n,d) 74 RSA速度 75 * 由于进行的都是大数计算,使得RSA最快的情况也比DES慢上100倍,无论 是软件还是硬件实现。 76 * 速度一直是RSA的缺陷。一般来说只用于少量数据 加密。*/ 77 78 public static void priENpubDE() { 79 String src="RSA 加密字符串"; 80 try { 81 //1.初始化秘钥 82 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); 83 //秘钥长度 84 keyPairGenerator.initialize(1024); 85 //初始化秘钥对 86 KeyPair keyPair = keyPairGenerator.generateKeyPair(); 87 //公钥 88 RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); 89 String publicKeyStr = Base64.encodeBase64String(rsaPublicKey.getEncoded()); 90 //私钥 91 RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); 92 String privateKeyStr = Base64.encodeBase64String(rsaPrivateKey.getEncoded()); 93 94 //2.私钥加密,公钥解密----加密 95 //生成私钥 96 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyStr)); 97 KeyFactory keyFactory =KeyFactory.getInstance("RSA"); 98 PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); 99 //Cipher类为加密和解密提供密码功能,通过getinstance实例化对象 100 Cipher cipher =Cipher.getInstance("RSA"); 101 //初始化加密 102 cipher.init(Cipher.ENCRYPT_MODE,privateKey); 103 byte[] result = cipher.doFinal(src.getBytes()); 104 System.out.println("私钥加密,公钥解密----加密:"+Base64.encodeBase64String(result)); 105 106 //3.私钥加密,公钥解密----解密 107 //生成公钥 108 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyStr)); 109 keyFactory=KeyFactory.getInstance("RSA"); 110 PublicKey publicKey= keyFactory.generatePublic(x509EncodedKeySpec); 111 cipher = Cipher.getInstance("RSA"); 112 //初始化解密 113 cipher.init(Cipher.DECRYPT_MODE,publicKey); 114 result = cipher.doFinal(result); 115 System.out.println("私钥加密,公钥解密----解密:"+new String(result)); 116 } catch (Exception e) { 117 e.printStackTrace(); 118 } 119 } 120 121 public static void pubENpriDE() { 122 String src="RSA 加密字符串"; 123 try { 124 //1.初始化秘钥 125 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); 126 //秘钥长度 127 keyPairGenerator.initialize(512); 128 //初始化秘钥对 129 KeyPair keyPair = keyPairGenerator.generateKeyPair(); 130 //公钥 131 RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); 132 //私钥 133 RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); 134 135 //2.公钥加密,私钥解密----加密 136 X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(rsaPublicKey.getEncoded()); 137 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 138 PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); 139 //初始化加密 140 //Cipher类为加密和解密提供密码功能,通过getinstance实例化对象 141 Cipher cipher =Cipher.getInstance("RSA"); 142 cipher.init(Cipher.ENCRYPT_MODE,publicKey); 143 //加密字符串 144 byte[] result = cipher.doFinal(src.getBytes()); 145 System.out.println("公钥加密,私钥解密----加密:"+Base64.encodeBase64String(result)); 146 147 //3.公钥加密,私钥解密-----解密 148 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded()); 149 keyFactory=KeyFactory.getInstance("RSA"); 150 PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); 151 //初始化解密 152 cipher.init(Cipher.DECRYPT_MODE, privateKey); 153 //解密字符串 154 result = cipher.doFinal(result); 155 System.out.println("公钥加密,私钥解密-----解密:"+new String(result)); 156 } catch (Exception e) { 157 e.printStackTrace(); 158 } 159 } 160 }
总结:公钥和私钥是成对的,它们互相解密。
公钥加密,私钥解密。
私钥数字签名,公钥验证。