• 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);
    }

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

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

    你好,我是【咬轮猫】

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

  • 相关阅读:
    二叉树的镜像
    Prototype 模式
    假如编程语言在一起聚餐
    从零开始——Ubuntu系统安装LAMP
    从零开始——Ubuntu系统配置与软件安装
    从零开始——制作Ubuntu系统U盘启动
    apt-get upgrade failure with libc
    再生龙Clonezilla备份Ubuntu系统
    Ubuntu上apache多端口配置虚拟主机的方法
    bash & Makefile & 正则表达式学习
  • 原文地址:https://www.cnblogs.com/Hero-/p/9370021.html
Copyright © 2020-2023  润新知