zoukankan      html  css  js  c++  java
  • c#RSA 私钥加签公钥解签

    /// RSA签名
    /// </summary>
    /// <param name="data">待签名数据</param>
    /// <param name="privateKeyJava"></param>
    /// <returns></returns>
    public static string RSASignBouncyCastle(string data, string privateKeyJava)
    {
    RsaKeyParameters privateKeyParam = (RsaKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKeyJava));
    ISigner signer = SignerUtilities.GetSigner("SHA1WithRSA");
    signer.Init(true, privateKeyParam);//参数为true加签,参数为false验签
    var dataByte = Encoding.GetEncoding("UTF-8").GetBytes(data);
    signer.BlockUpdate(dataByte, 0, dataByte.Length);
    // return Encoding.GetEncoding("UTF-8").GetString(signer.GenerateSignature()); //签名结果 非Base64String
    return Convert.ToBase64String(signer.GenerateSignature());
    }
    /// <summary>
    /// 验签
    /// </summary>
    /// <param name="data">源数据</param>
    /// <param name="publicKey"></param>
    /// <param name="signature">base64签名</param>
    /// <returns></returns>
    public static bool VerifyBouncyCastle(string data, string publicKey, string signature)
    {
    RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
    ISigner signer = SignerUtilities.GetSigner("SHA1WithRSA");
    signer.Init(false, publicKeyParam);
    byte[] dataByte = Encoding.GetEncoding("UTF-8").GetBytes(data);
    signer.BlockUpdate(dataByte, 0, dataByte.Length);
    //byte[] signatureByte = Encoding.GetEncoding(encoding).GetBytes(signature);// 非Base64String
    byte[] signatureByte = Convert.FromBase64String(signature);
    return signer.VerifySignature(signatureByte);
    }

    -------------------------------------------

    学而不思则罔,思而不学则殆

    你好,我是【咬轮猫】

    -------------------------------------------

  • 相关阅读:
    首页下拉广告代码
    索引 CREATE INDEX
    苏三起解 故事
    日期正则表达式
    除去非空字符串函数
    数据库什么是字段应该设置索引
    警务通
    动态生成缩略图
    超级实用且不花哨的js代码大全
    无线警务移动办公系统解决方案
  • 原文地址:https://www.cnblogs.com/Hero-/p/9370021.html
Copyright © 2011-2022 走看看