zoukankan      html  css  js  c++  java
  • RSA非对称可逆加密

    /// <summary>
    /// RSA ECC
    /// 可逆非对称加密
    /// 非对称加密算法的优点是密钥管理很方便,缺点是速度慢。
    /// </summary>
    using System.Security.Cryptography;
    public class RsaEncrypt
    {

    /// <summary>
    /// publicKey,privateKey
    /// </summary>
    /// <returns></returns>
    public static KeyValuePair<string, string> GetKeyPair()
    {
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
    string publicKey = RSA.ToXmlString(false);
    string privateKey = RSA.ToXmlString(true);
    return new KeyValuePair<string, string>(publicKey, privateKey);
    }

    /// <summary>
    /// 加密:内容+公钥
    /// </summary>
    /// <param name="content"></param>
    /// <param name="publicKey"></param>
    /// <returns></returns>
    public static string Encrypt(string content, string publicKey)
    {
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    rsa.FromXmlString(publicKey);
    UnicodeEncoding ByteConverter = new UnicodeEncoding();
    byte[] DataToEncrypt = ByteConverter.GetBytes(content);
    byte[] resultBytes = rsa.Encrypt(DataToEncrypt, false);
    return Convert.ToBase64String(resultBytes);
    }

    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="content"></param>
    /// <param name="publicKey">返还公钥</param>
    /// <param name="privateKey">返回密钥</param>
    /// <returns>加密后结果</returns>
    public static string Encrypt(string content, out string publicKey, out string privateKey)
    {
    RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
    publicKey = rsaProvider.ToXmlString(false);
    privateKey = rsaProvider.ToXmlString(true);

    UnicodeEncoding ByteConverter = new UnicodeEncoding();
    byte[] DataToEncrypt = ByteConverter.GetBytes(content);
    byte[] resultBytes = rsaProvider.Encrypt(DataToEncrypt, false);
    return Convert.ToBase64String(resultBytes);
    }

    /// <summary>
    /// 解密 内容+私钥
    /// </summary>
    /// <param name="content"></param>
    /// <param name="privateKey"></param>
    /// <returns></returns>
    public static string Decrypt(string content, string privateKey)
    {
    byte[] dataToDecrypt = Convert.FromBase64String(content);
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
    RSA.FromXmlString(privateKey);
    byte[] resultBytes = RSA.Decrypt(dataToDecrypt, false);
    UnicodeEncoding ByteConverter = new UnicodeEncoding();
    return ByteConverter.GetString(resultBytes);
    }
    }


    //非对称加密
    string publicKey = "";
    string privateKey = "";
    string rsaEn = RsaEncrypt.Encrypt("netnetnetnetnetnetnetne", out publicKey, out privateKey);
    string rsaDe = RsaEncrypt.Decrypt(rsaEn, privateKey);

    KeyValuePair<string, string> publicPrivate = RsaEncrypt.GetKeyPair();
    string rsaEn1 = RsaEncrypt.Encrypt("net", publicPrivate.Key);
    string rsaDe1 = RsaEncrypt.Decrypt(rsaEn1, publicPrivate.Value);
    //加密key 解密key
    //公开加密key,接受加密消息,因为只有我一个人能解密
    //公开解密key,用于签名,表明数据一定是我发的,因为只有我有加密的key
    //公钥私钥只是跟公开与否有关

  • 相关阅读:
    代码规范总结
    git记住提交密码的技巧
    php foreach遍历
    flight学习笔记
    the resource is not on the build path of a php project
    Google安装postman插件
    PHP开发框架CodeIgniter
    eclipse中php项目开发的环境配置说明
    MyBatis入门篇
    mybatis学习(十二)——mybatis逆向工程
  • 原文地址:https://www.cnblogs.com/anyihen/p/12773191.html
Copyright © 2011-2022 走看看