非对称加密 java 实现_RSA非对称加密Java实现

1 importjava.security.Key;2 importjava.security.KeyFactory;3 importjava.security.KeyPair;4 importjava.security.KeyPairGenerator;5 importjava.security.PrivateKey;6 importjava.security.PublicKey;7 importjava.security.Signature;8 importjava.security.interfaces.RSAPrivateKey;9 importjava.security.interfaces.RSAPublicKey;10 importjava.security.spec.PKCS8EncodedKeySpec;11 importjava.security.spec.X509EncodedKeySpec;12 importjava.util.HashMap;13 importjava.util.Map;14 importjavax.crypto.Cipher;15 public class RSASecurity extendsSecurityBase {16 public static final String KEY_ALGORTHM="RSA";17 public static final String SIGNATURE_ALGORITHM="MD5withRSA";18 public static final String PUBLIC_KEY = "RSAPublicKey";//公钥

19 public static final String PRIVATE_KEY = "RSAPrivateKey";//私钥

20 /**

21 * 初始化密钥22 *@return

23 *@throwsException24 */

25 public static Map createKeys()throwsException{26 KeyPairGenerator keyPairGenerator =KeyPairGenerator.getInstance(KEY_ALGORTHM);27 keyPairGenerator.initialize(1024);28 KeyPair keyPair =keyPairGenerator.generateKeyPair();29 //公钥

30 RSAPublicKey publicKey =(RSAPublicKey) keyPair.getPublic();31 //私钥

32 RSAPrivateKey privateKey =(RSAPrivateKey) keyPair.getPrivate();33 Map keyMap = new HashMap(2);34 keyMap.put(PUBLIC_KEY, publicKey);35 keyMap.put(PRIVATE_KEY, privateKey);36 returnkeyMap;37 }38 /**

39 * 取得公钥,并转化为String类型40 *@paramkeyMap41 *@return

42 *@throwsException43 */

44 public static String getPublicKey(Map keyMap)throwsException{45 Key key =keyMap.get(PUBLIC_KEY);46 returnencryptBASE64(key.getEncoded());47 }48 /**

49 * 取得私钥,并转化为String类型50 *@paramkeyMap51 *@return

52 *@throwsException53 */

54 public static String getPrivateKey(Map keyMap) throwsException{55 Key key =keyMap.get(PRIVATE_KEY);56 returnencryptBASE64(key.getEncoded());57 }58 /**

59 * 用私钥加密60 *@paramdata 加密数据61 *@paramkey 密钥62 *@return

63 *@throwsException64 */

65 public static byte[] encryptByPrivateKey(byte[] data,String key)throwsException{66 //解密密钥

67 byte[] keyBytes =decryptBASE64(key);68 //取私钥

69 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = newPKCS8EncodedKeySpec(keyBytes);70 KeyFactory keyFactory =KeyFactory.getInstance(KEY_ALGORTHM);71 Key privateKey =keyFactory.generatePrivate(pkcs8EncodedKeySpec);72 //对数据加密

73 Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());74 cipher.init(Cipher.ENCRYPT_MODE, privateKey);75 returncipher.doFinal(data);76 }77 /**

78 * 用私钥解密79 *@paramdata 加密数据80 *@paramkey 密钥81 *@return

82 *@throwsException83 */

84 public static byte[] decryptByPrivateKey(byte[] data,String key)throwsException{85 //对私钥解密

86 byte[] keyBytes =decryptBASE64(key);87 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = newPKCS8EncodedKeySpec(keyBytes);88 KeyFactory keyFactory =KeyFactory.getInstance(KEY_ALGORTHM);89 Key privateKey =keyFactory.generatePrivate(pkcs8EncodedKeySpec);90 //对数据解密

91 Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());92 cipher.init(Cipher.DECRYPT_MODE, privateKey);93 returncipher.doFinal(data);94 }95 /**

96 * 用公钥加密97 *@paramdata 加密数据98 *@paramkey 密钥99 *@return

100 *@throwsException101 */

102 public static byte[] encryptByPublicKey(byte[] data,String key)throwsException{103 //对公钥解密

104 byte[] keyBytes =decryptBASE64(key);105 //取公钥

106 X509EncodedKeySpec x509EncodedKeySpec = newX509EncodedKeySpec(keyBytes);107 KeyFactory keyFactory =KeyFactory.getInstance(KEY_ALGORTHM);108 Key publicKey =keyFactory.generatePublic(x509EncodedKeySpec);109 //对数据解密

110 Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());111 cipher.init(Cipher.ENCRYPT_MODE, publicKey);112 returncipher.doFinal(data);113 }114 /**

115 * 用公钥解密116 *@paramdata 加密数据117 *@paramkey 密钥118 *@return

119 *@throwsException120 */

121 public static byte[] decryptByPublicKey(byte[] data,String key)throwsException{122 //对私钥解密

123 byte[] keyBytes =decryptBASE64(key);124 X509EncodedKeySpec x509EncodedKeySpec = newX509EncodedKeySpec(keyBytes);125 KeyFactory keyFactory =KeyFactory.getInstance(KEY_ALGORTHM);126 Key publicKey =keyFactory.generatePublic(x509EncodedKeySpec);127 //对数据解密

128 Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());129 cipher.init(Cipher.DECRYPT_MODE, publicKey);130 returncipher.doFinal(data);131 }132 /**

133 * 用私钥对信息生成数字签名134 *@paramdata //加密数据135 *@paramprivateKey //私钥136 *@return

137 *@throwsException138 */

139 public static String sign(byte[] data,String privateKey)throwsException{140 //解密私钥

141 byte[] keyBytes =decryptBASE64(privateKey);142 //构造PKCS8EncodedKeySpec对象

143 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = newPKCS8EncodedKeySpec(keyBytes);144 //指定加密算法

145 KeyFactory keyFactory =KeyFactory.getInstance(KEY_ALGORTHM);146 //取私钥匙对象

147 PrivateKey privateKey2 =keyFactory.generatePrivate(pkcs8EncodedKeySpec);148 //用私钥对信息生成数字签名

149 Signature signature =Signature.getInstance(SIGNATURE_ALGORITHM);150 signature.initSign(privateKey2);151 signature.update(data);152 returnencryptBASE64(signature.sign());153 }154 /**

155 * 校验数字签名156 *@paramdata 加密数据157 *@parampublicKey 公钥158 *@paramsign 数字签名159 *@return

160 *@throwsException161 */

162 public static boolean verify(byte[] data,String publicKey,String sign)throwsException{163 //解密公钥

164 byte[] keyBytes =decryptBASE64(publicKey);165 //构造X509EncodedKeySpec对象

166 X509EncodedKeySpec x509EncodedKeySpec = newX509EncodedKeySpec(keyBytes);167 //指定加密算法

168 KeyFactory keyFactory =KeyFactory.getInstance(KEY_ALGORTHM);169 //取公钥匙对象

170 PublicKey publicKey2 =keyFactory.generatePublic(x509EncodedKeySpec);171 Signature signature =Signature.getInstance(SIGNATURE_ALGORITHM);172 signature.initVerify(publicKey2);173 signature.update(data);174 //验证签名是否正常

175 returnsignature.verify(decryptBASE64(sign));176 }177 /**

178 *179 * @Title: main180 * @Description: 测试181 *@param @paramargs182 *@returnvoid183 *@throws

184 */

185 public static voidmain(String[] args) {186 try{187 Map data =createKeys();188 String content = "JavaRsaEncoder";189 System.out.println("加密前:"+content);190 String encode =encryptBASE64(encryptByPrivateKey(content.getBytes(), getPrivateKey(data)));191 System.out.println("加密后:"+encode);192 String decode = newString(decryptByPublicKey(decryptBASE64(encode), getPublicKey(data)));193 System.out.println("解密后:"+decode);194 } catch(Exception e) {195 e.printStackTrace();196 }197 }198 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值