zoukankan      html  css  js  c++  java
  • RSA加密算法

    class Program
    {
    static void Main(string[] args)
    {


    RSAPublicKey P = new RSAPublicKey();
    P.Exponent = "AAAAAAAA";
    P.Modulus = "BBBBBBBBBB";
    string S = XmlUtil.Serializer(typeof(RSAPublicKey), P);
    //string activeDir = Server.MapPath("~/Custom/");

    // RSAPublicKey PublicKey = XmlUtil.Deserialize(typeof(RSAPublicKey), @"D:\xml\Publickey.xml") as RSAPublicKey;
    string a = "123";
    string a2 = RSAEncrypt(a);
    string b = RSADecrypt(a2);
    }

    private static RSAPublicKey getRSAPublicKey()
    {
    RSAPublicKey PublicKey = XmlUtil.Deserialize(typeof(RSAPublicKey), @"D:xmlPublickey.xml") as RSAPublicKey;
    return PublicKey;

    }


    private static RSAPrivateKey getRSARSAPrivateKey()
    {
    RSAPrivateKey PrivateKey = XmlUtil.Deserialize(typeof(RSAPrivateKey), @"D:xmlPrivateKey.xml") as RSAPrivateKey;
    return PrivateKey;

    }
    /// <summary>
    /// RSA加密 yms2016
    /// </summary>
    /// <param name="plainText">明文</param>
    /// <returns>密文</returns>
    public static string RSAEncrypt(string plainText)
    {
    RSAPublicKey key = getRSAPublicKey();
    try
    {
    RSAParameters rsaParam = new RSAParameters();
    //rsaParam.Exponent = Convert.FromBase64String(GlobalParameter.RSA_PARAM_EXPONENT);
    //rsaParam.Modulus = Convert.FromBase64String(GlobalParameter.RSA_PARAM_MODULUS);

    //rsaParam.Exponent = Convert.FromBase64String("AQAB");
    //rsaParam.Modulus = Convert.FromBase64String("6YPSotU5ioFyM0fyJIz2uP5NUodIkTC3mOVemRd8OV5tCtdf+LmpnI4NIR5phxoIdN7GzoLiryGWs/E4jUvjCkVCY+XO4R+xEkrUSBCytUjiSHcMGvjX3ZosGoT7H/IuMGclRz9lOptbCSWZYQmfHea4yrJ1Azybu9lj1BT6ZnU=");

    rsaParam.Exponent = Convert.FromBase64String(key.Exponent.Trim());
    rsaParam.Modulus = Convert.FromBase64String(key.Modulus.Trim());

    return RSAEncrypt(plainText, rsaParam);
    }
    catch (CryptographicException)
    {
    return null;
    }
    }

    /// <summary>
    /// RSA解密
    /// </summary>
    /// <param name="cypherText">密文</param>
    /// <returns>明文</returns>
    public static string RSADecrypt(string cypherText)
    {
    RSAPrivateKey key = getRSARSAPrivateKey();
    try
    {
    RSAParameters rsaParam = new RSAParameters();
    //rsaParam.Exponent = Convert.FromBase64String(GlobalParameter.RSA_PARAM_EXPONENT);
    //rsaParam.Modulus = Convert.FromBase64String(GlobalParameter.RSA_PARAM_MODULUS);
    //rsaParam.InverseQ = Convert.FromBase64String(GlobalParameter.RSA_PARAM_INVERSEQ);
    //rsaParam.D = Convert.FromBase64String(GlobalParameter.RSA_PARAM_D);
    //rsaParam.P = Convert.FromBase64String(GlobalParameter.RSA_PARAM_P);
    //rsaParam.Q = Convert.FromBase64String(GlobalParameter.RSA_PARAM_Q);
    //rsaParam.DP = Convert.FromBase64String(GlobalParameter.RSA_PARAM_DP);
    //rsaParam.DQ = Convert.FromBase64String(GlobalParameter.RSA_PARAM_DQ);

    //rsaParam.Exponent = Convert.FromBase64String("AQAB");
    //rsaParam.Modulus = Convert.FromBase64String("6YPSotU5ioFyM0fyJIz2uP5NUodIkTC3mOVemRd8OV5tCtdf+LmpnI4NIR5phxoIdN7GzoLiryGWs/E4jUvjCkVCY+XO4R+xEkrUSBCytUjiSHcMGvjX3ZosGoT7H/IuMGclRz9lOptbCSWZYQmfHea4yrJ1Azybu9lj1BT6ZnU=");
    //rsaParam.InverseQ = Convert.FromBase64String("USMiemxLJHeYXFk0N2gfVz4x7q/Wc1L4y9/KRRzAXgjc6cq7223SL1td7lCl/sX4puRevt466U6tZRG/u6lozA==");
    //rsaParam.D = Convert.FromBase64String("sYVTdhKfIdpylOWXAY5gahZ5dcn+stHUYVnN5phiCcLtJpbBEPckKC4dcRYLp3d1AlPx00DRT847ISS6l0AfolUclUanw1mTUfe29D7TrG4/ZGBqNbit1PcqT/gT+dl5JWC1cZU/+Z3fGNpjMwwWhsGlimow5ViRpalt4FiPMoE=");
    //rsaParam.P = Convert.FromBase64String("/wh2zBrCZGux+aLRCakuH/oGB4PsgcYrILWz7esWLBpqZg5P3YHuOV55aCbPpu8EwFfvs0XgY8ra02odUGlssQ==");
    //rsaParam.Q = Convert.FromBase64String("6mZ5IhA6osdI7r/D2JkGE3YAUS6kE3iEJxwS8mv5Dnse5b/xVg/ShhhGEeEywIktLZdKSymW0ilxAMciPPV3BQ==");
    //rsaParam.DP = Convert.FromBase64String("17jGG/FKmvzJp3pETWKwZFXtga/ifwcr9dgcoVPoBlg1xau3fItqAVbbZPDnVrH5F+WKGN8zZdXx8UBiGWGDkQ==");
    //rsaParam.DQ = Convert.FromBase64String("uexEIte6msj1uaJT5uch4afUgpA7RtJuCFOFV9J6/5h7HoOMGtZ8By8TzTTSO9PTCPmazVmUruh+IL7TEoUlPQ==");

    rsaParam.Exponent = Convert.FromBase64String(key.Exponent.Trim());
    rsaParam.Modulus = Convert.FromBase64String(key.Modulus.Trim());
    rsaParam.InverseQ = Convert.FromBase64String(key.InverseQ.Trim());
    rsaParam.D = Convert.FromBase64String(key.D.Trim());
    rsaParam.P = Convert.FromBase64String(key.P.Trim());
    rsaParam.Q = Convert.FromBase64String(key.Q.Trim());
    rsaParam.DP = Convert.FromBase64String(key.DP.Trim());
    rsaParam.DQ = Convert.FromBase64String(key.DQ.Trim());


    return RSADecrypt(cypherText, rsaParam);
    }
    catch (CryptographicException)
    {
    return null;
    }
    }

    /// <summary>
    /// RSA加密
    /// </summary>
    /// <param name="plainText">明文</param>
    /// <param name="rsaParam">公钥私钥参数</param>
    /// <returns>密文</returns>
    public static string RSAEncrypt(string plainText, RSAParameters rsaParam)
    {
    try
    {
    UTF8Encoding encoder = new UTF8Encoding();
    byte[] dataToEncrypt = encoder.GetBytes(plainText);
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    rsa.ImportParameters(rsaParam);
    byte[] bytesCypherText = rsa.Encrypt(dataToEncrypt, false);
    return Convert.ToBase64String(bytesCypherText);
    }
    catch (CryptographicException)
    {
    return null;
    }
    }

    /// <summary>
    /// RSA解密
    /// </summary>
    /// <param name="cypherText">密文</param>
    /// <param name="rsaParam">公钥私钥参数</param>
    /// <returns>明文</returns>
    public static string RSADecrypt(string cypherText, RSAParameters rsaParam)
    {
    try
    {
    byte[] dataToDecrypt = Convert.FromBase64String(cypherText);
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    rsa.ImportParameters(rsaParam);
    byte[] bytesPlainText = rsa.Decrypt(dataToDecrypt, false);
    UTF8Encoding encoder = new UTF8Encoding();
    return encoder.GetString(bytesPlainText);
    }
    catch (CryptographicException)
    {
    return null;
    }
    }
    }

    如有兴趣,加qq群围观:337335820

  • 相关阅读:
    hive0.13.1安装-mysql server作为hive的metastore
    hadoop2.2集群部署教程连接
    hadoop2.4.1伪分布模式部署
    spring cloud (四、服务消费者demo_consumer)
    spring cloud (三、服务提供者demo_provider)
    spring cloud (二、服务注册安全demo_eureka)
    spring cloud (一、服务注册demo_eureka)
    maven里面pom文件的各标签介绍
    如何删除github里面的项目
    用过的工具列表及作用
  • 原文地址:https://www.cnblogs.com/xinanheishao/p/4169335.html
Copyright © 2011-2022 走看看