C#_RSA的加解密与签名验证

生成公钥和私钥文件

using System.Security.Cryptography;
using System.IO;
/// <summary>
/// 生成公钥、私钥
/// </summary>
/// <param name="PrivateKeyPath">私钥文件保存路径,包含文件名</param>
/// <param name="PublicKeyPath">公钥文件保存路径,包含文件名</param>
public void RSAKey(string PrivateKeyPath, string PublicKeyPath)
{
    RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
    SaveKey(PrivateKeyPath, provider.ToXmlString(true));//保存私钥文件
    SaveKey(PublicKeyPath, provider.ToXmlString(false));//保存公钥文件
}
/// <summary>
/// 保存公钥/私钥文件
/// </summary>
/// <param name="path">公钥/私钥文件保存路径</param>
/// <param name="publickey">公钥/私钥值</param>
public void SaveKey(string path, string key)
{
    FileStream stream = new FileStream(path, FileMode.Create);
    StreamWriter sw = new StreamWriter(stream);
    sw.WriteLine(key);
    sw.Close();
    stream.Close();
}


加密与解密
/// <summary>
/// RSA加密
/// </summary>
/// <param name="xmlPublicKey">公钥</param>
/// <param name="m_strEncryptString">需要加密的数据</param>
/// <returns>RSA公钥加密后的数据</returns>
public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString)
{
    string str2;
    try
    {
        RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
        provider.FromXmlString(xmlPublicKey);
        byte[] bytes = new UnicodeEncoding().GetBytes(m_strEncryptString);
        str2 = Convert.ToBase64String(provider.Encrypt(bytes, false));
    }
    catch (Exception exception)
    {
        throw exception;
    }
    return str2;
}

/// <summary>
/// RSA解密
/// </summary>
/// <param name="xmlPrivateKey">私钥</param>
/// <param name="m_strDecryptString">需要解密的数据</param>
/// <returns>解密后的数据</returns>
public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)
{
    string str2;
    try
    {
        RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
        provider.FromXmlString(xmlPrivateKey);
        byte[] rgb = Convert.FromBase64String(m_strDecryptString);
        byte[] buffer2 = provider.Decrypt(rgb, false);
        str2 = new UnicodeEncoding().GetString(buffer2);
    }
    catch (Exception exception)
    {
        throw exception;
    }
    return str2;
}


签名与签名验证

/// <summary>
/// 签名
/// </summary>
/// <param name="p_strKeyPrivate">私钥</param>
/// <param name="m_strHashbyteSignature">需签名的数据</param>
/// <returns>签名后的值</returns>
public string SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature)
{
    byte[] rgbHash = Convert.FromBase64String(m_strHashbyteSignature);
    RSACryptoServiceProvider key = new RSACryptoServiceProvider();
    key.FromXmlString(p_strKeyPrivate);
    RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
    formatter.SetHashAlgorithm("MD5");
    byte[] inArray = formatter.CreateSignature(rgbHash);
    return Convert.ToBase64String(inArray);
}

/// <summary>
/// 签名验证
/// </summary>
/// <param name="p_strKeyPublic">公钥</param>
/// <param name="p_strHashbyteDeformatter">待验证的用户名</param>
/// <param name="p_strDeformatterData">注册码</param>
/// <returns>签名是否符合</returns>
public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
{
    try
    {
        byte[] rgbHash = Convert.FromBase64String(p_strHashbyteDeformatter);
        RSACryptoServiceProvider key = new RSACryptoServiceProvider();
        key.FromXmlString(p_strKeyPublic);
        RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);
        deformatter.SetHashAlgorithm("MD5");
        byte[] rgbSignature = Convert.FromBase64String(p_strDeformatterData);
        if (deformatter.VerifySignature(rgbHash, rgbSignature))
        {
            return true;
        }
        return false;
    }
    catch
    {
        return false;
    }
}


posted @ 2022-04-22 15:27  淮雨清青  阅读(570)  评论(0编辑  收藏  举报