package com.test;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;
/**
* Rsa加密解密工具类,需加入sun.misc.BASE64Decoder.jar
* 加入sun.misc.BASE64Decoder.jar 是为了让加密结果更好传输,也可以不加
*
*
* @author 枫jk
*
*/
public class RsaKey {
String modulus;
Cipher cipher;
public RsaKey(String modulus) {
this.modulus = modulus;
try {
//若用Cipher.getInstance("RSA");在android端加密,在服务端解密会报错。
//原因网友解释为:android系统的RSA实现是“RSA/ECB/NoPadding”,而标准JDK实现是“RSA/None/PKCS1Padding”,这造成了在android机上加密后无法在服务器上解密的原因
//所以指定一种形式的RSA就可以了。
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取加密公匙
* @param modulus
* @param publicExponent
* @return
* @throws Exception
*/
public PublicKey getPublicKey(String modulus, String publicExponent)
throws Exception {
BigInteger m = new BigInteger(modulus);
BigInteger e = new BigInteger(publicExponent);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* 获取加密私匙
* @param modulus
* @param privateExponent
* @return
* @throws Exception
*/
public PrivateKey getPrivateKey(String modulus, String privateExponent)
throws Exception {
BigInteger m = new BigInteger(modulus);
BigInteger e = new BigInteger(privateExponent);
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 得到加密串
* @param text
* @param publicExponent
* @return
*/
public String getEnRsa(String text,String publicExponent) {
String str = null;
try {
PublicKey publicKey = getPublicKey(modulus, publicExponent);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] enBytes = cipher.doFinal(text.getBytes());
str = (new BASE64Encoder()).encode(enBytes);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
/**
* 解密得到原文
* @param text
* @param privateExponet
* @return
*/
public String getDeRsa(String text,String privateExponet) {
String str = null;
try {
PrivateKey privateKey = getPrivateKey(modulus, privateExponet);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] enBytes = (new BASE64Decoder()).decodeBuffer(text);
byte[] deBytes = cipher.doFinal(enBytes);
str = new String(deBytes);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
}
RSA加密JAVA实现+sun.misc.BASE64Decoder.jar
5星 · 超过95%的资源 需积分: 10 149 浏览量
2013-12-09
20:17:42
上传
评论 2
收藏 8KB ZIP 举报
枫jk
- 粉丝: 2
- 资源: 7
最新资源
- mybatis动态sql及其JAVA示例
- 微软常用运行库 游戏运行库 VC++各个版本
- 微信小程序开发教程.pptx
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- 锐捷网络认证中心网络管理.pdf
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
- SD8233LF是一款用于单按键触摸及接近感应开关,其用途是替代传统的机械型开关芯片IC
- 基于YOLOv5的烟雾火焰检测算法研究
- 基于STM32的联合调试侦听设备解决方案原理图PCB源文件调试工具视频(大赛作品)
- MyBatis动态SQL是一种强大的特性,它允许我们在SQL语句中根据条件动态地添加或删除某些部分,从而实现更加灵活和高效的数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈