java使用Cipher进行签名和验签
public static void main(String[] args) {
try {
String plainText = "duwenlei";
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("rsa");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
//签名
MessageDigest md = MessageDigest.getInstance("sha1");
byte[] encHash = md.digest(plainText.getBytes());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyPair.getPrivate().getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key privateKey =keyFactory.generatePrivate(keySpec);
Cipher cipherSign = Cipher.getInstance(keyFactory.getAlgorithm());
cipherSign.init(Cipher.ENCRYPT_MODE,privateKey);
// cipherSign.init(Cipher.ENCRYPT_MODE,keyPair.getPrivate());
byte[] encHashSign = cipherSign.doFinal(encHash);
//验签
X509EncodedKeySpec pkcs8EncodedKeySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded());
KeyFactory keyFactoryPub = KeyFactory.getInstance("RSA");
Key publicKey = keyFactoryPub.generatePublic(pkcs8EncodedKeySpec);
Cipher cipherVer = Cipher.getInstance(keyFactoryPub.getAlgorithm());
cipherVer.init(Cipher.DECRYPT_MODE, publicKey);
// cipherVer.init(Cipher.DECRYPT_MODE, keyPair.getPublic());
byte[] decHashVer = cipherVer.doFinal(encHashSign);
MessageDigest md2 = MessageDigest.getInstance("sha1");
byte[] decHash = md2.digest(plainText.getBytes());
// System.out.println("encHash = " + byte2hex(encHash));
// System.out.println("decHashVer = " + byte2hex(decHashVer));
// System.out.println("decHash = " + byte2hex(decHash));
System.out.println("签名是否正确:" + byte2hex(decHashVer).equals(byte2hex(decHash)));
PEMWriter privateKeyWriter = new PEMWriter(new FileWriter(new File("D://privateKey.key")));
privateKeyWriter.writeObject(keyPair.getPrivate());
privateKeyWriter.flush();
privateKeyWriter.close();
PEMWriter publicKeyWriter = new PEMWriter(new FileWriter(new File("D://publicKey.key")));
publicKeyWriter.writeObject(keyPair.getPublic());
publicKeyWriter.flush();
publicKeyWriter.close();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] hex2byte(String strhex) {
if (strhex == null) {
return null;
}
int l = strhex.length();
if (l % 2 == 1) {
return null;
}
byte[] b = new byte[l / 2];
for (int i = 0; i != l / 2; i++) {
b[i] = (byte) Integer.parseInt(strhex.substring(i * 2, i * 2 + 2),16);
}
return b;
}
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1) {
hs = hs + "0" + stmp;
} else {
hs = hs + stmp ;
}
}
return hs.toUpperCase();
}
java使用Cipher进行签名和验签的更多相关文章
- RSA后台签名前台验签的应用(前台采用jsrsasign库)
写在前面 安全测试需要, 为防止后台响应数据返给前台过程中被篡改前台再拿被篡改后的数据进行接下来的操作影响正常业务, 决定采用RSA对响应数据进行签名和验签, 于是有了这篇<RSA后台签名前台验 ...
- 密码基础知识(2)以RSA为例说明加密、解密、签名、验签
密码基础知识(1)https://www.cnblogs.com/xdyixia/p/11528572.html 一.RSA加密简介 RSA加密是一种非对称加密.是由一对密钥来进行加解密的过程,分别称 ...
- .NET RSA解密、签名、验签
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Sec ...
- PHP SHA1withRSA加密生成签名及验签
最近公司对接XX第三方支付平台的代付业务,由于对方公司只有JAVA的demo,所以只能根据文档自己整合PHP的签名加密,网上找过几个方法,踩到各种各样的坑,还好最后算是搞定了,话不多说,代码分享出来. ...
- erlang的RSA签名与验签
1.RSA介绍 RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对 其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥,即公钥,而 ...
- RSA/RSA2 进行签名和验签
package com.byttersoft.hibernate.erp.szmy.util; import java.io.ByteArrayInputStream; import java.io. ...
- 中行P1签名及验签
分享中国银行快捷.NET P1签名和验签方法代码中ReturnValue为自定义类型请无视 #region 验证签名 /// <summary> /// 验证签名 /// </sum ...
- 几个例子理解对称加密与非对称加密、公钥与私钥、签名与验签、数字证书、HTTPS加密方式
# 原创,转载请留言联系 为什么会出现这么多加密啊,公钥私钥啊,签名啊这些东西呢?说到底还是保证双方通信的安全性与完整性.例如小明发一封表白邮件给小红,他总不希望给别人看见吧.而各种各样的技术就是为了 ...
- Delphi微信支付【支持MD5和HMAC-SHA256签名与验签】
作者QQ:(648437169) 点击下载➨微信支付 微信支付api文档 [Delphi 微信支付]支持付款码支付.二维码支付.订单查询.申请退款.退款查询.撤销订单.关闭订单. ...
随机推荐
- Windows 服务入门指南
有很多时候,我们需要创建Windows Service. 这篇文章可以算是一个入门指南吧,希望对初学者有帮助. 要创建Windows Service, 首先选择Windows服务项目,如下图: 这里我 ...
- paper 24 :matlab的cat函数
cat:用来联结数组 用法:C = cat(dim, A, B) 按dim来联结A和B两个数组. C = cat(dim, A1, A2, A3, ...) 按dim联结所有输入的数 ...
- 12---Net基础加强
使用ShowDialog窗体之间的回传值: using System; using System.Collections.Generic; using System.ComponentModel; u ...
- mac ruby rails安装(使用rvm)
mac的场合: which ruby -> /usr/bin/ruby -> 这是mac自带的ruby,我们希望能用管理ruby的版本. 安装rvm curl -L https://get ...
- android显示当前时间
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss "); Date c ...
- 【python cookbook】【数据结构与算法】18.将名称映射到序列的元素中
问题:希望通过名称来访问元素,减少结构中对位置的依赖性 解决方案:使用命名元组collections.namedtuple().它是一个工厂方法,返回的是python中标准元组类型的子类,提供给它一个 ...
- 161123、ssh scp 复制文件和文件夹
复制文件或目录命令: 复制文件: (1)将本地文件拷贝到远程 scp 文件名用户名@计算机IP或者计算机名称:远程路径 本地192.168.80.100客户端 scp /root/instal ...
- 对比其它软件方法评估敏捷和Scrum
一般来说,选择一种软件开发方法,更像是加入一个邪教组织,而不像是做出了一个技术决策.许多公司甚至从未试图去评估这些方法,而仅仅是盲目采用最流行的方法,这就造成了如今五花八门的各种敏捷方法.因此本文将使 ...
- history and its relevant variables in Linux/GNU and Mac OS history命令以及相关环境变量
对于Terminalor们,history命令并不陌生,什么!n, !!更是很常用的,而且您在命令行敲的cmds是默认保存在/home/$USER/.bash_history(linux) /User ...
- Linux终端乱码的解决办法
用SSH连接Linux时经常会遇到乱码的情况,痛苦了好久,在网上找到一个解决办法,编辑~/.bash_profile文件,加入下面两行: LANG="zh_CN.GB18030" ...