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 微信支付]支持付款码支付.二维码支付.订单查询.申请退款.退款查询.撤销订单.关闭订单. ...
随机推荐
- [原创]spring学习笔记:关于springsource-tool-suite插件的安装
1.首先我们得确定自己使用的eclipes的版本,具体方式:打开eclipes > help > About Eclipes > 点击eclipes的logo > 查看ecli ...
- Struts2.3+Spring+iBatis 初学之问题判断
小白接下来将会总结下我再学习Spring的学习过程中(ssi框架)中遇到的问题,以后会不断的进行更新. 最容易犯的问题,就是声明bean的时候,属性引用其他声明的bean的时候,name没有进行好对应 ...
- 封装mysqli类
类: <?phpheader('content-type:text/html;charset=utf-8');/*掌握满足单例模式的必要条件(1)私有的构造方法-为了防止在类外使用new关键字实 ...
- ASP.NET状态管理策略
如果要想把信息存储在客户端那可以选择视图状态.控件状态.隐藏字段.cookie.和查询字符串. 1.web窗体页提供viewstate属性作为内置结构,在同一页的多个请求间自动保留值.他作为页面的隐藏 ...
- ./fedora_install_oracle.sh bad interpreter
错误原因之一很有可能是你的脚本文件是DOS格式的, 即每一行的行尾以\r\n来标识, 其ASCII码分别是0x0D, 0x0A.可以有很多种办法看这个文件是DOS格式的还是UNIX格式的, 还是MAC ...
- WM (Constants)
Create page WM (Constants) Summary WM_* Constants and their definitions or descriptions and what c ...
- Java实现文件的读写,复制
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStr ...
- paper 56 :机器学习中的算法:决策树模型组合之随机森林(Random Forest)
周五的组会如约而至,讨论了一个比较感兴趣的话题,就是使用SVM和随机森林来训练图像,这样的目的就是 在图像特征之间建立内在的联系,这个model的训练,着实需要好好的研究一下,下面是我们需要准备的入门 ...
- springmvc+spring+mybatis分页查询实例版本2.0
先在改成纯利用js进行分页,首先查询出所有记录,初始化通过jquery控制只知显示首页内容,创建页面切换功能的函数,每次显示固定的内容行并把其他内容行隐藏,这样只需要一次提交就可以实现分页,但是仍有缺 ...
- AMAB interconnector PL301(二)
1)Frequency Conversion Components:包含三种component. AXI-AXI async bridge:拥有两种mode:bypass mode 和 async m ...