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);
            }
        }
    
  • 相关阅读:
    Windows性能计数器应用
    Azure Oracle Linux VNC 配置
    Azure 配置管理系列 Oracle Linux (PART6)
    Azure 配置管理系列 Oracle Linux (PART5)
    Azure 配置管理系列 Oracle Linux (PART4)
    Azure 配置管理系列 Oracle Linux (PART3)
    Azure 配置管理系列 Oracle Linux (PART2)
    vagrant多节点配置
    docker基本操作
    LINUX开启允许对外访问的网络端口命令
  • 原文地址:https://www.cnblogs.com/acyy/p/2662254.html
Copyright © 2011-2022 走看看