通过OpenSSL生成公私钥文件(如果没有OpenSSL工具建议下载Cmder工具自带OpenSSL指令)

1、生成RSA密钥的方法

genrsa -out private-rsa.key 2048

2、获取客户端公钥文件

openssl  req -new -x509 -key private-rsa.key -days 750 -out public-rsa.cer

3、获取服务器私钥文件

openssl pkcs12 -export -name zhangsan -in public-rsa.cer -inkey private-rsa.key -out user-rsa.pfx

4、获取密钥文件的5元组数据

openssl rsa -in private-rsa.key -noout -text

Java实现私钥签名、公钥验签、私钥加密数据、公钥解密数据

 
import javax.crypto.Cipher;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory; public class Sha1withRSAUtil {
private static final String publicKeyFileName = System.getProperty("user.dir") + File.separator + "pubkey.cer";
private static final String privateKeyFileName = System.getProperty("user.dir") + File.separator + "private.pfx";
private static final String pfxPassword = "123";//私钥文件获取时设置的密钥
private static String aliasName = "003";//alias名称 /**
* 签名
*
* @return 签名后经过base64处理的字符串
* @throws Exception
*/
public static String sign(String str) {
String base64Sign = "";
InputStream fis = null;
try {
fis = new FileInputStream(privateKeyFileName);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
char[] pscs = pfxPassword.toCharArray();
keyStore.load(fis, pscs);
PrivateKey priKey = (PrivateKey) (keyStore.getKey(aliasName, pscs));
// 签名
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initSign(priKey);
byte[] bysData = str.getBytes("UTF-8");
sign.update(bysData);
byte[] signByte = sign.sign();
BASE64Encoder encoder = new BASE64Encoder();
base64Sign = encoder.encode(signByte);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64Sign;
} /**
* 数据验证
*
* @param signStr 加密后的数据
* @param verStr 原始字符
* @return
*/
public static boolean verify(String signStr, String verStr)
throws Exception {
boolean verfy = false;
InputStream fis = null;
try {
fis = new FileInputStream(publicKeyFileName);
CertificateFactory cf = CertificateFactory.getInstance("x509");
Certificate cerCert = cf.generateCertificate(fis);
PublicKey pubKey = cerCert.getPublicKey();
BASE64Decoder decoder = new BASE64Decoder();
byte[] signed = decoder.decodeBuffer(signStr);
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initVerify(pubKey);
sign.update(verStr.getBytes("UTF-8"));
verfy = sign.verify(signed);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return verfy;
} /**
* 通过公钥文件进行加密数据
*
* @return 加密后经过base64处理的字符串
*/
public static String encrypt(String source) throws Exception {
InputStream fis = null;
try {
fis = new FileInputStream(publicKeyFileName);
CertificateFactory cf = CertificateFactory.getInstance("x509");
Certificate cerCert = cf.generateCertificate(fis);
PublicKey pubKey = cerCert.getPublicKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] sbt = source.getBytes();
byte[] epByte = cipher.doFinal(sbt);
BASE64Encoder encoder = new BASE64Encoder();
String epStr = encoder.encode(epByte);
return epStr;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* 通过私钥文件进行解密数据
*
* @return 解密后的明文字符串
*/
public static String decode(String source) throws Exception {
BASE64Decoder b64d = new BASE64Decoder();
byte[] keyByte = b64d.decodeBuffer(source);
InputStream fis = null;
try {
fis = new FileInputStream(privateKeyFileName);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
char[] pscs = pfxPassword.toCharArray();
keyStore.load(fis, pscs);
PrivateKey priKey = (PrivateKey) (keyStore.getKey(aliasName, pscs));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
byte[] epByte = cipher.doFinal(keyByte);
return new String(epByte, "UTF-8");
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
 

使用方法

 
import com.util.rsa.Sha1withRSAUtil;

public class Main {

    public static void main(String[] args) {
String data = "name123456789";
String signData = Sha1withRSAUtil.sign(data);
System.out.println(signData); try {
boolean flag = Sha1withRSAUtil.verify(signData, data);
System.out.println(flag); String eData = Sha1withRSAUtil.encrypt(data);
System.out.println(eData);
String dData = Sha1withRSAUtil.decode(eData);
System.out.println(dData);
} catch (Exception e) {
e.printStackTrace();
}
}
}
 

【加密】RSA验签及加密的更多相关文章

  1. Java RSA加密以及验签

    签名加密以及验签工具类: 一般秘钥分为3个key 1.自己生成的私钥, 2.通过私钥生成的公钥1 3.通过提交公钥1给某宝,获取的公钥2. RSA公钥加密算法简介 非对称加密算法.只有短的RSA钥匙才 ...

  2. ios RSA 验签加密解密

    关于公钥和私钥的生成,网上有很多本地生产的方法,我遇到的问题是,按照网上生产的方式生成7个文件,本地使用没有问题,但是和后台交互就不行了. 发现生成公钥和私钥的没有那么麻烦,使用在线生产工具就能使用, ...

  3. RSA签名、验签、加密、解密

    最近在做一个项目,与一个支付渠道进行连接进行充值,为了安全,每个接口访问时,都要先登陆(调用登陆接口),拿到一个sessionKey,后续业务接口内容用它来进行3DES加密处理.而登陆需要用到RSA进 ...

  4. Java读取、创建Excel;验签,加密

    需要架包:poi相关jar,Md5.jar------------------------------------------------------------------------------- ...

  5. PHP 之用证书对数据进行签名、验签、加密、解密

    /** * 对数据进行签名 * $data = 'If you are still new to things, we’ve provided a few walkthroughs to get yo ...

  6. Jmeter接口测试-MD5加密-请求验签(完整流程)

    第一部分:先准备好Jmeter 1.在开始编写脚本之前,先要确保你的Jmeter能够正常运行.若你还没有安装Jmeter,可参考以下方法: A.Jmeter需要java运行环境,所以需要下载JDK,J ...

  7. Java & PHP RSA 互通密钥、签名、验签、加密、解密

    RSA加密算法是一种非对称加密算法.在公开密钥加密和电子商业中RSA被广泛使用.RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Le ...

  8. java RSA验签

    这几天在跟一个php的小哥哥联调接口,遇到了一些问题记录下来, 直接上代码吧,亲测有效 import org.slf4j.Logger; import org.slf4j.LoggerFactory; ...

  9. JAVA/PHP/C#版RSA验签--转

    本文是上一篇文章的兄弟篇,上篇文章介绍了客户端的sdk中如何基于JAVA/PHP/C#使用RSA私钥签名,然后服务端基于JAVA使用RSA公钥验签,客户端签名/服务端验签的模式只能帮助服务端检查客户端 ...

随机推荐

  1. php strtotime,mktime,DateTime函数处理时间累加问题

    时间戳(年月日时分秒)  使用strtotime函数,结合+1 month,-1 month,next month,last month的时候会出现一些问题. demo示例: //时间"20 ...

  2. codeforces 617 E. XOR and Favorite Number(莫队算法)

    题目链接:http://codeforces.com/problemset/problem/617/E 题目: 给你a1 a2 a3 ··· an 个数,m次询问:在[L, R] 里面又多少中 [l, ...

  3. grammar_action

    w ll = [] for i in range(0, 10, 1): ll.append(i) print(ll) for i in ll: if i < 6: print(i) index_ ...

  4. R 保存包含中文的 eps 图片--showtext

    来自统计之都,感谢 Ihavenothing(http://cos.name/cn/profile/81532) 详情参考:http://cos.name/cn/topic/151358?replie ...

  5. redis连接报错:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to...

    连接redis报错: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persis ...

  6. python实现读取excel

    实现代码如下: #读取excel,将每行数据放入一个列表,将所有列表放入一个列表形成二维列表,返回该二维列表 import xlrd class ReadExcel: def read_excel(s ...

  7. Python笔记(二十一)_内置函数、内置方法

    内置函数 issubclass(class1,class2) 判断class1类是否为class2类的子类,返回True和False 注意1:类会被认为是自身的子类 >>>issub ...

  8. SPOJ NICEBTRE - Nice Binary Trees(树 先序遍历)

    传送门 Description Binary trees can sometimes be very difficult to work with. Fortunately, there is a c ...

  9. 爬虫(十一)—— XPath总结

    目录 XPath总结 一.何为XPath 二.XPath语法 1.语法 2.实例 三.XPath轴 1.XPath轴语法 2.XPath轴实例 四.XPath运算符 XPath总结 一.何为XPath ...

  10. mysql中插入中文时显示乱码

    在插入mysql的时候参数是中文的时候显示的是???乱码,这个是由于没有配置字符编码引起的 只要在SqlMapconfig.xml文件中加上<property name="url&qu ...