RSA 签名 / 验签工具

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

依赖 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 关注

相关帖子

欢迎来到这里!

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

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

推荐标签 标签

  • VirtualBox

    VirtualBox 是一款开源虚拟机软件,最早由德国 Innotek 公司开发,由 Sun Microsystems 公司出品的软件,使用 Qt 编写,在 Sun 被 Oracle 收购后正式更名成 Oracle VM VirtualBox。

    10 引用 • 2 回帖 • 12 关注
  • Quicker

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

    38 引用 • 158 回帖 • 1 关注
  • 宕机

    宕机,多指一些网站、游戏、网络应用等服务器一种区别于正常运行的状态,也叫“Down 机”、“当机”或“死机”。宕机状态不仅仅是指服务器“挂掉了”、“死机了”状态,也包括服务器假死、停用、关闭等一些原因而导致出现的不能够正常运行的状态。

    13 引用 • 82 回帖 • 74 关注
  • CSDN

    CSDN (Chinese Software Developer Network) 创立于 1999 年,是中国的 IT 社区和服务平台,为中国的软件开发者和 IT 从业者提供知识传播、职业发展、软件开发等全生命周期服务,满足他们在职业发展中学习及共享知识和信息、建立职业发展社交圈、通过软件开发实现技术商业化等刚性需求。

    14 引用 • 155 回帖 • 1 关注
  • jQuery

    jQuery 是一套跨浏览器的 JavaScript 库,强化 HTML 与 JavaScript 之间的操作。由 John Resig 在 2006 年 1 月的 BarCamp NYC 上释出第一个版本。全球约有 28% 的网站使用 jQuery,是非常受欢迎的 JavaScript 库。

    63 引用 • 134 回帖 • 743 关注
  • 知乎

    知乎是网络问答社区,连接各行各业的用户。用户分享着彼此的知识、经验和见解,为中文互联网源源不断地提供多种多样的信息。

    10 引用 • 66 回帖 • 1 关注
  • GitBook

    GitBook 使您的团队可以轻松编写和维护高质量的文档。 分享知识,提高团队的工作效率,让用户满意。

    3 引用 • 8 回帖
  • ZooKeeper

    ZooKeeper 是一个分布式的,开放源码的分布式应用程序协调服务,是 Google 的 Chubby 一个开源的实现,是 Hadoop 和 HBase 的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

    61 引用 • 29 回帖 • 6 关注
  • flomo

    flomo 是新一代 「卡片笔记」 ,专注在碎片化时代,促进你的记录,帮你积累更多知识资产。

    6 引用 • 143 回帖 • 1 关注
  • Angular

    AngularAngularJS 的新版本。

    26 引用 • 66 回帖 • 563 关注
  • BND

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

    107 引用 • 1281 回帖 • 41 关注
  • 安装

    你若安好,便是晴天。

    132 引用 • 1184 回帖
  • 职场

    找到自己的位置,萌新烦恼少。

    127 引用 • 1708 回帖 • 1 关注
  • 叶归
    14 引用 • 62 回帖 • 25 关注
  • jsoup

    jsoup 是一款 Java 的 HTML 解析器,可直接解析某个 URL 地址、HTML 文本内容。它提供了一套非常省力的 API,可通过 DOM,CSS 以及类似于 jQuery 的操作方法来取出和操作数据。

    6 引用 • 1 回帖 • 508 关注
  • 深度学习

    深度学习(Deep Learning)是机器学习的分支,是一种试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高层抽象的算法。

    43 引用 • 44 回帖
  • Logseq

    Logseq 是一个隐私优先、开源的知识库工具。

    Logseq is a joyful, open-source outliner that works on top of local plain-text Markdown and Org-mode files. Use it to write, organize and share your thoughts, keep your to-do list, and build your own digital garden.

    7 引用 • 69 回帖 • 3 关注
  • 电影

    这是一个不能说的秘密。

    123 引用 • 608 回帖
  • Hadoop

    Hadoop 是由 Apache 基金会所开发的一个分布式系统基础架构。用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。

    93 引用 • 122 回帖 • 619 关注
  • wolai

    我来 wolai:不仅仅是未来的云端笔记!

    2 引用 • 14 回帖 • 3 关注
  • 数据库

    据说 99% 的性能瓶颈都在数据库。

    346 引用 • 761 回帖
  • 生活

    生活是指人类生存过程中的各项活动的总和,范畴较广,一般指为幸福的意义而存在。生活实际上是对人生的一种诠释。生活包括人类在社会中与自己息息相关的日常活动和心理影射。

    230 引用 • 1432 回帖
  • Word
    13 引用 • 41 回帖
  • 服务

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

    41 引用 • 24 回帖 • 2 关注
  • JRebel

    JRebel 是一款 Java 虚拟机插件,它使得 Java 程序员能在不进行重部署的情况下,即时看到代码的改变对一个应用程序带来的影响。

    26 引用 • 78 回帖 • 688 关注
  • IPFS

    IPFS(InterPlanetary File System,星际文件系统)是永久的、去中心化保存和共享文件的方法,这是一种内容可寻址、版本化、点对点超媒体的分布式协议。请浏览 IPFS 入门笔记了解更多细节。

    20 引用 • 245 回帖 • 241 关注
  • C++

    C++ 是在 C 语言的基础上开发的一种通用编程语言,应用广泛。C++ 支持多种编程范式,面向对象编程、泛型编程和过程化编程。

    108 引用 • 153 回帖