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进行签名和验签的更多相关文章

  1. RSA后台签名前台验签的应用(前台采用jsrsasign库)

    写在前面 安全测试需要, 为防止后台响应数据返给前台过程中被篡改前台再拿被篡改后的数据进行接下来的操作影响正常业务, 决定采用RSA对响应数据进行签名和验签, 于是有了这篇<RSA后台签名前台验 ...

  2. 密码基础知识(2)以RSA为例说明加密、解密、签名、验签

    密码基础知识(1)https://www.cnblogs.com/xdyixia/p/11528572.html 一.RSA加密简介 RSA加密是一种非对称加密.是由一对密钥来进行加解密的过程,分别称 ...

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

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Sec ...

  4. PHP SHA1withRSA加密生成签名及验签

    最近公司对接XX第三方支付平台的代付业务,由于对方公司只有JAVA的demo,所以只能根据文档自己整合PHP的签名加密,网上找过几个方法,踩到各种各样的坑,还好最后算是搞定了,话不多说,代码分享出来. ...

  5. erlang的RSA签名与验签

    1.RSA介绍 RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对 其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥,即公钥,而 ...

  6. RSA/RSA2 进行签名和验签

    package com.byttersoft.hibernate.erp.szmy.util; import java.io.ByteArrayInputStream; import java.io. ...

  7. 中行P1签名及验签

    分享中国银行快捷.NET P1签名和验签方法代码中ReturnValue为自定义类型请无视 #region 验证签名 /// <summary> /// 验证签名 /// </sum ...

  8. 几个例子理解对称加密与非对称加密、公钥与私钥、签名与验签、数字证书、HTTPS加密方式

    # 原创,转载请留言联系 为什么会出现这么多加密啊,公钥私钥啊,签名啊这些东西呢?说到底还是保证双方通信的安全性与完整性.例如小明发一封表白邮件给小红,他总不希望给别人看见吧.而各种各样的技术就是为了 ...

  9. Delphi微信支付【支持MD5和HMAC-SHA256签名与验签】

    作者QQ:(648437169) 点击下载➨微信支付            微信支付api文档 [Delphi 微信支付]支持付款码支付.二维码支付.订单查询.申请退款.退款查询.撤销订单.关闭订单. ...

随机推荐

  1. C++之路进阶——bzoj1455(罗马游戏)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  gryz2016 Logout 捐赠本站 Notice:由于本OJ ...

  2. 类名.class, class.forName(), getClass()区别

    1:Class cl=A.class; JVM将使用类A的类装载器, 将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作.返回类A的Class的对象. 2:Class cl=对象引 ...

  3. PAT乙级 1004. 成绩排名 (20)

    1004. 成绩排名 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入n名学生的姓名.学号.成绩,分 ...

  4. 我使用的vim配置文件

    各种搜,拼凑出了这么一个配置文件,以下是文件的内容 syntax onfiletype onset linespace=0set rulerset nocompatibleset confirmset ...

  5. yii2.0分页

    本实例是对商品列表进行分页 1.Controller中,商品列表的方法actionList 引用分页类 actionList中: $goods_info=Goods::find()->joinW ...

  6. SqlServer:此数据库处于单用户模式,导致数据库无法删除的处理

    此数据库处理单用户模式,尚在连接当中,无法删除(既使将SQLServer停止后再启动也是如此) USE [master] GO /****** Object: StoredProcedure [dbo ...

  7. Spark实战2:Zeppelin的安装和SparkSQL使用总结

    zeppelin是spark的web版本notebook编辑器,相当于ipython的notebook编辑器. 一Zeppelin安装 (前提是spark已经安装好) 1 下载https://zepp ...

  8. tp框架支付宝手机网页支付

    开发环境:linux+php+mysql 密钥生成: 1.genrsa -out rsa_private_key.pem 1024 生成商户私钥,因在php环境,一定要保持原始状态,不得修改.rsa_ ...

  9. NXP QN9020

    NXP的这个BLE蓝牙方案也很有趣, 一起研究. 这个函数在app_gpa_task.c里面 ***************************************************** ...

  10. laravel事务小例子

    发生异常则自动回滚,正常则自动提交,示例如下: DB::connection('vshare')->transaction(function() use($id,$reason,$refuser ...