zoukankan      html  css  js  c++  java
  • 通用类 RSA RSA加解密

    /// <summary>
        /// RSA加解密
        /// </summary>
        public class RSA
        {
            /// <summary>
            /// RSA加密
            /// </summary>
            /// <param name="xmlPublicKey">私钥</param>
            /// <param name="encryptString">需要加密的数据</param>
            /// <returns>RSA公钥加密后的数据</returns>
            public static string RSAEncrypt(string xmlPublicKey, string encryptString)
            {
                string result;
                try
                {
                    RSACryptoServiceProvider.UseMachineKeyStore = true;
                    RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
                    provider.FromXmlString(xmlPublicKey);
                    byte[] bytes = new UnicodeEncoding().GetBytes(encryptString);
                    result = Convert.ToBase64String(provider.Encrypt(bytes, false));
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                return result;
            }
    
            /// <summary>
            /// RSA解密
            /// </summary>
            /// <param name="xmlPrivateKey">公钥</param>
            /// <param name="decryptString">需要解密的数据</param>
            /// <returns>解密后的数据</returns>
            public static string RSADecrypt(string xmlPrivateKey, string decryptString)
            {
                string result;
                try
                {
                    RSACryptoServiceProvider.UseMachineKeyStore = true;
                    RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
                    provider.FromXmlString(xmlPrivateKey);
                    byte[] rgb = Convert.FromBase64String(decryptString);
                    byte[] buffer2 = provider.Decrypt(rgb, false);
                    result = new UnicodeEncoding().GetString(buffer2);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                return result;
            }
    
            /// <summary>
            /// 生成公钥、私钥
            /// </summary>
            /// <param name="PrivateKeyPath">私钥文件保存路径,包含文件名</param>
            /// <param name="PublicKeyPath">公钥文件保存路径,包含文件名</param>
            public static void CreateKey(out string privateKey, out string publicKey)
            {
                RSACryptoServiceProvider.UseMachineKeyStore = true;
                RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
                privateKey = provider.ToXmlString(true);
                publicKey = provider.ToXmlString(false);
            }
        }
    
  • 相关阅读:
    linux shell 小技能
    Jenkins 2.60.x 2种发送邮件方式
    Linux wget 批量下载
    日语阅读-2-如何看“何々的”的用法
    日语阅读-1-如何看ほう的用法
    [日本语]初级语法
    Python基础
    Python基础(10)
    Python基础(8)
    Python基础(7)
  • 原文地址:https://www.cnblogs.com/acyy/p/2662254.html
Copyright © 2011-2022 走看看