微信小程序 SHA256WithRSA签名

该篇博客介绍了如何使用jsrsasign库进行RSA加SHA256的数据签名操作。首先,提取Key并按ASCII排序,然后使用RSAKey将私钥转换为16进制。接着,初始化SHA256withRSA签名,并更新数据,最后生成并展示签名。在遇到异常时,提示检查私钥的正确性。
摘要由CSDN通过智能技术生成

1.先把 Key 拿出来,按 ASCII 码的增序排序 ,拼接网上很多
2.SHA256WithRSA 签名
点击下载jsrsasign-latest-all

const jsrsasign = require('../../static/js/jsrsasign-latest-all')

transit_api.encryptSign = function (signData) {
  const priKey = "";
  var rsa = new jsrsasign.RSAKey()
  rsa = jsrsasign.KEYUTIL.getKey(priKey) // 将私钥 转成16进制
  const sig = jsrsasign.KJUR.crypto.Signature({
    alg: 'SHA256withRSA'
  });
  sig.init(rsa) 
  sig.updateString(signData)
  const sign = jsrsasign.hextob64(sig.sign()) 
  alert(sign);
  return sign;
}

3.异常处理
在这里插入图片描述

解决方式:请检查私钥的正确性
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是SHA256withRSA签名与验签的Java代码示例: **签名代码:** ``` import java.security.*; import java.util.Base64; public class RSASignatureExample { public static void main(String[] args) throws Exception { String message = "This is a message to be signed."; // Generate key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair keyPair = keyGen.generateKeyPair(); // Create a signature object Signature signature = Signature.getInstance("SHA256withRSA"); // Initialize the signature object with the private key PrivateKey privateKey = keyPair.getPrivate(); signature.initSign(privateKey); // Update the signature object with the message signature.update(message.getBytes()); // Sign the message byte[] signatureBytes = signature.sign(); // Print the signature in Base64 encoding System.out.println("Signature: " + Base64.getEncoder().encodeToString(signatureBytes)); } } ``` **验签代码:** ``` import java.security.*; import java.util.Base64; public class RSASignatureExample { public static void main(String[] args) throws Exception { String message = "This is a message to be signed."; String signatureString = "PLl6yBb6IhOv0Jd3k1y/YlR9a3UOx0WJQy6RbbS/3W8mQ2ztcL5x+JShHl6O8gTlJn4wtNQ7Ggz0ZiPwq9SInQf/9d4eG4qSkZCzVgP9U9bQY7jwFVhWXfzqU6l5K8SxyA2pBw+PQs0N2dJQ2Xk9V3tXZP5h+UcMXwLQe/3K7uh2M="; // Generate key pair KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair keyPair = keyGen.generateKeyPair(); // Create a signature object Signature signature = Signature.getInstance("SHA256withRSA"); // Initialize the signature object with the public key PublicKey publicKey = keyPair.getPublic(); signature.initVerify(publicKey); // Update the signature object with the message signature.update(message.getBytes()); // Verify the signature boolean verified = signature.verify(Base64.getDecoder().decode(signatureString)); // Print the verification result System.out.println("Signature verified: " + verified); } } ``` 以上代码示例中,我们使用SHA256withRSA算法对一个字符串进行签名和验签。在签名时,我们首先生成了一个RSA密钥对,然后使用私钥对消息进行签名,并打印出Base64编码的签名结果。在验签时,我们使用同样的RSA密钥对和签名算法,使用公钥初始化签名对象,并使用该对象对消息进行验证,最终打印验证结果。需要注意的是,验签时需要将签名结果从Base64编码中解码。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值