| 
					
				 | 
			
			
				@ -3,12 +3,11 @@ package com.yihu.jw.security.utils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import org.apache.commons.codec.binary.Base64; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import javax.crypto.Cipher; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.security.KeyPair; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.security.KeyPairGenerator; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.security.SecureRandom; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.security.Security; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.security.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.security.interfaces.RSAPrivateKey; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.security.interfaces.RSAPublicKey; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.security.spec.PKCS8EncodedKeySpec; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.security.spec.X509EncodedKeySpec; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				public class RSAUtils { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -45,6 +44,53 @@ public class RSAUtils { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return new String(decrypt(Base64.decodeBase64(string),keyPair)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * RSA公钥加密 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param str 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     *            加密字符串 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param publicKey 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     *            公钥 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 密文 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @throws Exception 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     *             加密过程中的异常信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static String encrypt( String str, String publicKey ) throws Exception{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //base64编码的公钥 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        byte[] decoded = Base64.decodeBase64(publicKey); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //RSA加密 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Cipher cipher = Cipher.getInstance("RSA"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        cipher.init(Cipher.ENCRYPT_MODE, pubKey); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8"))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return outStr; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * RSA私钥解密 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param str 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     *            加密字符串 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param privateKey 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     *            私钥 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 铭文 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @throws Exception 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     *             解密过程中的异常信息 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static String decrypt(String str, String privateKey) throws Exception{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //64位解码加密后的字符串 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //base64编码的私钥 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        byte[] decoded = Base64.decodeBase64(privateKey); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        //RSA解密 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Cipher cipher = Cipher.getInstance("RSA"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        cipher.init(Cipher.DECRYPT_MODE, priKey); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String outStr = new String(cipher.doFinal(inputByte)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return outStr; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private static byte[] decrypt(byte[] string,KeyPair keyPair) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -60,14 +106,19 @@ public class RSAUtils { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//    public static void main(String[] args) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				// 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        KeyPair keyPair =  initKey(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        // 生成public key 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        System.out.println(generateBase64PublicKey(keyPair)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				// 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        // 解密 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//        System.out.println(decryptBase64("wAfY9JkoKay9SxcPIs1FcG+t6sR+wYwAs/mh9DpfcBraxzqoZdb9LyaAigzFQ0EKck9OyHL0dhv+Uxuw5hHw6CPT0B2Z0i1gwrjDUNaL1gWvqt1pDJVGrIYPLJSjs9xktFhY1jbxQgXGjyCt06Rwid5sJknw90AUO0CyQulfipg=",keyPair)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				//    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public static void main(String[] args) throws Exception{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        KeyPair keyPair =  getKey(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        // 生成public key 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String privateKeyString = new String(Base64.encodeBase64((keyPair.getPrivate().getEncoded()))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String publicKeyString = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        System.out.println("publicKeyString="+publicKeyString); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        System.out.println("privateKeyString="+privateKeyString); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String str = "123ysj*#%"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String encryptStr = encrypt(str,publicKeyString); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        // 字符串解密 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        System.out.println(decrypt(encryptStr,privateKeyString)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				} 
			 |