RSA 签名 / 验签工具

本贴最后更新于 2192 天前,其中的信息可能已经渤澥桑田

依赖 jar 包下载

zmxysdkjava20170605134301.jar
zmxysdkjava20170605134301source.jar

代码如下

import com.antgroup.zmxy.openplatform.api.ZhimaApiException; import com.antgroup.zmxy.openplatform.api.internal.util.Base64Util; import com.antgroup.zmxy.openplatform.api.internal.util.CoderUtil; import com.antgroup.zmxy.openplatform.api.internal.util.EncryptionModeEnum; import com.antgroup.zmxy.openplatform.api.internal.util.SignTypeEnum; import com.antgroup.zmxy.openplatform.api.internal.util.json.ExceptionErrorListener; import com.antgroup.zmxy.openplatform.api.internal.util.json.JSONValidatingReader; import java.io.ByteArrayOutputStream; import java.security.Key; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.RSAPrivateKeySpec; import java.security.spec.RSAPublicKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Iterator; import java.util.Map; import javax.crypto.Cipher; import org.apache.log4j.Logger; public class PKRSACoderUtil extends CoderUtil{ protected static Logger log = Logger.getLogger(PKRSACoderUtil.class); public static final String KEY_ALGORTHM = "RSA"; public static final String SPECIFIC_KEY_ALGORITHM = "RSA/ECB/PKCS1Padding"; public static final String SIGNATURE_ALGORITHM = "SHA256WITHRSA"; public static String encrypt(String paramsString, String charset, String publicKey) throws Exception { byte[] encryptedResult = encryptByPublicKey(paramsString.getBytes(charset), publicKey, null); return Base64Util.byteArrayToBase64(encryptedResult); } public static String encrypt(String paramsString, String charset, String publicKey, EncryptionModeEnum encryptionType) throws Exception { byte[] encryptedResult = encryptByPublicKey(paramsString.getBytes(charset), publicKey, encryptionType); return Base64Util.byteArrayToBase64(encryptedResult); } public static String sign(String data, String charset, String privateKey) throws Exception { byte[] dataInBytes = data.getBytes(charset); String signParams = sign(dataInBytes, privateKey); return signParams; } public static String sign(SignTypeEnum signType, String data, String charset, String privateKey) throws Exception { byte[] dataInBytes = data.getBytes(charset); String signParams = sign(signType, dataInBytes, privateKey); return signParams; } public static String decrypt(String data, String key, String charset) throws Exception { byte[] byte64 = Base64Util.base64ToByteArray(data); byte[] encryptedBytes = decryptByPrivateKey(byte64, key, null); return new String(encryptedBytes, charset); } public static String decrypt(String data, String key, String charset, EncryptionModeEnum encryptionType) throws Exception { byte[] byte64 = Base64Util.base64ToByteArray(data); byte[] encryptedBytes = decryptByPrivateKey(byte64, key, encryptionType); return new String(encryptedBytes, charset); } public static byte[] decryptByPrivateKey(byte[] data, String key, EncryptionModeEnum encryptionType) throws Exception { byte[] decryptedData = null; byte[] keyBytes = decryptBASE64(key); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(2, privateKey); int maxDecryptBlockSize; if (encryptionType != null) maxDecryptBlockSize = getMaxDecryptBlockSizeByEncryptionType(encryptionType); else { maxDecryptBlockSize = getMaxDecryptBlockSize(keyFactory, privateKey); } ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { int dataLength = data.length; for (int i = 0; i < dataLength; i += maxDecryptBlockSize) { int decryptLength = (dataLength - i < maxDecryptBlockSize) ? dataLength - i : maxDecryptBlockSize; byte[] doFinal = cipher.doFinal(data, i, decryptLength); bout.write(doFinal); } decryptedData = bout.toByteArray(); } finally { if (bout != null) { bout.close(); } } return decryptedData; } public static byte[] encryptByPublicKey(byte[] data, String key, EncryptionModeEnum encryptionType) throws Exception { byte[] encryptedData = null; byte[] keyBytes = decryptBASE64(key); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(1, publicKey); int maxEncryptBlockSize; if (encryptionType != null) maxEncryptBlockSize = getMaxEncryptBlockSizeByEncryptionType(encryptionType); else { maxEncryptBlockSize = getMaxEncryptBlockSize(keyFactory, publicKey); } ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { int dataLength = data.length; for (int i = 0; i < data.length; i += maxEncryptBlockSize) { int encryptLength = (dataLength - i < maxEncryptBlockSize) ? dataLength - i : maxEncryptBlockSize; byte[] doFinal = cipher.doFinal(data, i, encryptLength); bout.write(doFinal); } encryptedData = bout.toByteArray(); } finally { if (bout != null) { bout.close(); } } return encryptedData; } public static String sign(byte[] data, String privateKey) throws Exception { return sign(SignTypeEnum.SHA1WITHRSA, data, privateKey); } public static String sign(SignTypeEnum signType, byte[] data, String privateKey) throws Exception { byte[] keyBytes = decryptBASE64(privateKey); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey2 = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Signature signature = Signature.getInstance(signType.getDesc()); signature.initSign(privateKey2); signature.update(data); return encryptBASE64(signature.sign()); } public static boolean verify(byte[] data, String publicKey, String sign) throws Exception { return verify(SignTypeEnum.SHA1WITHRSA, data, publicKey, sign); } public static boolean verify(SignTypeEnum signType, byte[] data, String publicKey, String sign) throws Exception { byte[] keyBytes = decryptBASE64(publicKey); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey2 = keyFactory.generatePublic(x509EncodedKeySpec); Signature signature = Signature.getInstance(signType.getDesc()); signature.initVerify(publicKey2); signature.update(data); return signature.verify(decryptBASE64(sign)); } public static String decryptResponse(String fullResponse, String privateKey, String charset, EncryptionModeEnum encryptionType) throws Exception { String decryptedRsp = null; Map rootJson = parseResponseMap(fullResponse); for (Iterator it = rootJson.keySet().iterator(); it.hasNext(); ) { String key = (String)it.next(); if (key.endsWith("_response")) { String value = (String)rootJson.get(key); decryptedRsp = value; } } if (((Boolean)rootJson.get("encrypted")).booleanValue()) { decryptedRsp = decrypt(decryptedRsp, privateKey, charset, encryptionType); } return decryptedRsp; } public static void verifySign(String fullResponse, String decryptedBizResponse, String publicKey, String charset) throws Exception { verifySign(SignTypeEnum.SHA1WITHRSA, fullResponse, decryptedBizResponse, publicKey, charset); } public static void verifySign(SignTypeEnum signType, String fullResponse, String decryptedBizResponse, String publicKey, String charset) throws Exception { Map rootJson = parseResponseMap(fullResponse); String sign = (String)rootJson.get("biz_response_sign"); if ((sign != null) && (sign.length() > 0)) { boolean success = verify(signType, decryptedBizResponse.getBytes(charset), publicKey, sign); if (!(success)) throw new ZhimaApiException("验签失败: " + sign.toString()); } } public static Map parseResponseMap(String fullResponse) throws ZhimaApiException { JSONValidatingReader reader = new JSONValidatingReader(new ExceptionErrorListener()); Object rootObj = reader.read(fullResponse); if (rootObj instanceof Map) { Map rootJson = (Map)rootObj; return rootJson; } throw new ZhimaApiException("返回结果格式有误:" + fullResponse); } private static int getMaxEncryptBlockSize(KeyFactory keyFactory, Key key) throws Exception { int maxLength = 117; try { RSAPublicKeySpec publicKeySpec = (RSAPublicKeySpec)keyFactory.getKeySpec(key, RSAPublicKeySpec.class); int keyLength = publicKeySpec.getModulus().bitLength(); maxLength = keyLength / 8 - 11; } catch (Exception e) { } return maxLength; } private static int getMaxEncryptBlockSizeByEncryptionType(EncryptionModeEnum encryptionType) { if (encryptionType == EncryptionModeEnum.RSA1024) return 117; if (encryptionType == EncryptionModeEnum.RSA2048) { return 245; } return 117; } private static int getMaxDecryptBlockSize(KeyFactory keyFactory, Key key) throws Exception { int maxLength = 128; try { RSAPrivateKeySpec publicKeySpec = (RSAPrivateKeySpec)keyFactory.getKeySpec(key, RSAPrivateKeySpec.class); int keyLength = publicKeySpec.getModulus().bitLength(); maxLength = keyLength / 8; } catch (Exception e) { } return maxLength; } private static int getMaxDecryptBlockSizeByEncryptionType(EncryptionModeEnum encryptionType) { if (encryptionType == EncryptionModeEnum.RSA1024) return 128; if (encryptionType == EncryptionModeEnum.RSA2048) { return 256; } return 128; } }

验签代码

/** * 验证签名 * @return * @throws Exception */ public static boolean checksign(JSONObject jsonObject,String platpublickey) throws Exception{ //获取签名 String sign = jsonObject.getString("sign"); //json对象转换成map Map<String,Object> bizParams = getTextParams(jsonObject); String content = unurlgetSignCheckContentV2(bizParams).trim().replace("\\/", "/"); return PKRSACoderUtil.verify(SignTypeEnum.SHA256WITHRSA, content.getBytes(SysUtil.CHARSET),platpublickey, sign); }

签名代码

/** * 生成签名 * * * @return * */ public static String producesignByJson(JSONObject jsonObject ,String zzrsprivatekey){ String signstr=""; try{ String content = unurlgetSignCheckContentV2(getTextParams(jsonObject)).trim(); signstr = PKRSACoderUtil.sign(SignTypeEnum.SHA256WITHRSA, content, SysUtil.CHARSET, zzrsprivatekey); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return signstr; }
  • RSA
    8 引用 • 5 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • MongoDB

    MongoDB(来自于英文单词“Humongous”,中文含义为“庞大”)是一个基于分布式文件存储的数据库,由 C++ 语言编写。旨在为应用提供可扩展的高性能数据存储解决方案。MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似 JSON 的 BSON 格式,因此可以存储比较复杂的数据类型。

    90 引用 • 59 回帖 • 5 关注
  • ngrok

    ngrok 是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道。

    7 引用 • 63 回帖 • 644 关注
  • NGINX

    NGINX 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 NGINX 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

    315 引用 • 547 回帖
  • ReactiveX

    ReactiveX 是一个专注于异步编程与控制可观察数据(或者事件)流的 API。它组合了观察者模式,迭代器模式和函数式编程的优秀思想。

    1 引用 • 2 回帖 • 175 关注
  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    951 引用 • 943 回帖
  • Quicker

    Quicker 您的指尖工具箱!操作更少,收获更多!

    36 引用 • 155 回帖
  • SpaceVim

    SpaceVim 是一个社区驱动的模块化 vim/neovim 配置集合,以模块的方式组织管理插件以
    及相关配置,为不同的语言开发量身定制了相关的开发模块,该模块提供代码自动补全,
    语法检查、格式化、调试、REPL 等特性。用户仅需载入相关语言的模块即可得到一个开箱
    即用的 Vim-IDE。

    3 引用 • 31 回帖 • 116 关注
  • RemNote
    2 引用 • 16 回帖 • 9 关注
  • Latke

    Latke 是一款以 JSON 为主的 Java Web 框架。

    71 引用 • 535 回帖 • 820 关注
  • Sphinx

    Sphinx 是一个基于 SQL 的全文检索引擎,可以结合 MySQL、PostgreSQL 做全文搜索,它可以提供比数据库本身更专业的搜索功能,使得应用程序更容易实现专业化的全文检索。

    1 引用 • 214 关注
  • Mac

    Mac 是苹果公司自 1984 年起以“Macintosh”开始开发的个人消费型计算机,如:iMac、Mac mini、Macbook Air、Macbook Pro、Macbook、Mac Pro 等计算机。

    167 引用 • 595 回帖 • 1 关注
  • sts
    2 引用 • 2 回帖 • 226 关注
  • Elasticsearch

    Elasticsearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful 接口。Elasticsearch 是用 Java 开发的,并作为 Apache 许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

    117 引用 • 99 回帖 • 206 关注
  • Visio
    1 引用 • 2 回帖
  • Google

    Google(Google Inc.,NASDAQ:GOOG)是一家美国上市公司(公有股份公司),于 1998 年 9 月 7 日以私有股份公司的形式创立,设计并管理一个互联网搜索引擎。Google 公司的总部称作“Googleplex”,它位于加利福尼亚山景城。Google 目前被公认为是全球规模最大的搜索引擎,它提供了简单易用的免费服务。不作恶(Don't be evil)是谷歌公司的一项非正式的公司口号。

    49 引用 • 192 回帖
  • 服务

    提供一个服务绝不仅仅是简单的把硬件和软件累加在一起,它包括了服务的可靠性、服务的标准化、以及对服务的监控、维护、技术支持等。

    41 引用 • 24 回帖
  • 外包

    有空闲时间是接外包好呢还是学习好呢?

    26 引用 • 233 回帖 • 1 关注
  • Firefox

    Mozilla Firefox 中文俗称“火狐”(正式缩写为 Fx 或 fx,非正式缩写为 FF),是一个开源的网页浏览器,使用 Gecko 排版引擎,支持多种操作系统,如 Windows、OSX 及 Linux 等。

    7 引用 • 30 回帖 • 391 关注
  • Scala

    Scala 是一门多范式的编程语言,集成面向对象编程和函数式编程的各种特性。

    13 引用 • 11 回帖 • 157 关注
  • Maven

    Maven 是基于项目对象模型(POM)、通过一小段描述信息来管理项目的构建、报告和文档的软件项目管理工具。

    186 引用 • 318 回帖 • 260 关注
  • Openfire

    Openfire 是开源的、基于可拓展通讯和表示协议 (XMPP)、采用 Java 编程语言开发的实时协作服务器。Openfire 的效率很高,单台服务器可支持上万并发用户。

    6 引用 • 7 回帖 • 99 关注
  • danl
    164 关注
  • 心情

    心是产生任何想法的源泉,心本体会陷入到对自己本体不能理解的状态中,因为心能产生任何想法,不能分出对错,不能分出自己。

    59 引用 • 369 回帖
  • BND

    BND(Baidu Netdisk Downloader)是一款图形界面的百度网盘不限速下载器,支持 Windows、Linux 和 Mac,详细介绍请看这里

    107 引用 • 1281 回帖 • 31 关注
  • JWT

    JWT(JSON Web Token)是一种用于双方之间传递信息的简洁的、安全的表述性声明规范。JWT 作为一个开放的标准(RFC 7519),定义了一种简洁的,自包含的方法用于通信双方之间以 JSON 的形式安全的传递信息。

    20 引用 • 15 回帖 • 20 关注
  • 微信

    腾讯公司 2011 年 1 月 21 日推出的一款手机通讯软件。用户可以通过摇一摇、搜索号码、扫描二维码等添加好友和关注公众平台,同时可以将自己看到的精彩内容分享到微信朋友圈。

    132 引用 • 796 回帖 • 1 关注
  • Word
    13 引用 • 40 回帖