zzk0904
荡舟心许
采纳率0%
2017-08-17 07:05 浏览 6.1k

C#实现RSA签名,签名算法采用SHA-256,JAVA部分已有,因需要转为C#代码,望大神出手相助

10

package test;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.RSAPrivateKeySpec;
import java.util.Calendar;
import java.util.Date;

public class test
{
public static void main(String[] args) throws Exception
{
String msg = "0|20140411|1\n银联基金交易账号|用户在基金公司的账号|申请日期|银联订单号|确认日期|基金公司确认编号|返回码|失败原因\n1040000000371674|20000015|20140410|20120528990000120140410000000120|20140411|2250000000016|0000|\n";
byte[] data = msg.getBytes("UTF-8");
// 私钥
BigInteger modulus = new BigInteger("00af9133ade333df8b9aabd776334d0d8f5d96597fe2041fff716185305934cc7a386ab451f28156730ce8a76adcf9bf93ffc87fa47c0d60c4652f1700f50304997a20563cc854f1f84c921f9b1f72d0d3e989d4b22669014859135e89ced24bbac3af1d1a4a76ff5736f81fdc461d2f96cebc5f36eacae7981e7ea59443448fa1", 16);
BigInteger privateExponent = new BigInteger("009c46140920e6c881bc405f7e1ff5e32f0cafcaa4912af8cc6840d4cc0f6b76e3079abb3d022dec246686249773707efd9ebcfa19102edc46948650e49232888116e4b54fe77b6eaf5061db4f50c89646241559f602d477efb0d6b755a67a40e978753f56d16142660c47bc91408cf01deaaf3f30180050e2730980e9498cdd3d", 16);

    RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(modulus, privateExponent);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
    //用SHA-256做摘要
    MessageDigest md= MessageDigest.getInstance("SHA-256");
    data=md.digest(data);
    // 签名
    Signature instance = Signature.getInstance("SHA1withRSA");
    instance.initSign(privateKey);
    instance.update(data);
    byte[] sing  = instance.sign();
    System.out.println(byteArr2HexString(sing));
}
// 以下为调用到的公共方法
public static String byteArr2HexString(byte[] bytearr) {
    if (bytearr == null) {
        return "null";
    }
    StringBuffer sb = new StringBuffer();
    for (int k = 0; k < bytearr.length; k++) {
        if ((bytearr[k] & 0xFF) < 16) {
            sb.append("0");
        }
        sb.append(Integer.toString(bytearr[k] & 0xFF, 16));
    }
    return sb.toString();
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • devmiao devmiao 2017-08-19 14:33
    点赞 1 评论 复制链接分享
  • weixin_44565956 Mr . 2019-03-22 16:21
    
            private string CreateSignature(string baseString, string pathToPrivateKey)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                using (StreamReader sr = new StreamReader(pathToPrivateKey))
                {
                    PemReader pr = new PemReader(sr);
                    RSAParameters keyParameters = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)pr.ReadObject());
    
                    rsa.ImportParameters(keyParameters);
                }
                var signData = rsa.SignData(Encoding.UTF8.GetBytes(baseString), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                return Convert.ToBase64String(signData);
            }
    
    点赞 评论 复制链接分享

相关推荐