zoukankan      html  css  js  c++  java
  • RSA加密解密,Base64String

            ///<remarks>
            /// DotNet.Utilities.RSACryption cryption = new DotNet.Utilities.RSACryption();
            /// cryption.RSAKey_String(out string pubKey, out string priKey);
            /// string message = "test";
            /// string RSAEncryptMessage = cryption.RSAEncrypt_String(pubKey, message);
            /// string res = cryption.RSADecrypt_String(priKey, RSAEncryptMessage);
            /// </remarks>
            /// <summary>
            /// RSA 的密钥产生 产生私钥 和公钥 
            /// </summary>
            /// <param name="publicKey">和公钥</param>
            /// <param name="privateKey">私钥</param>
            public void RSAKey_String(out string publicKey, out string privateKey)
            {
                System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                byte[] publicKeyBytes = rsa.ExportCspBlob(false);
                byte[] privateKeyBytes = rsa.ExportCspBlob(true);
                publicKey = Convert.ToBase64String(publicKeyBytes);
                privateKey = Convert.ToBase64String(privateKeyBytes);
            }
    
            /// <summary>
            /// RSA的加密函数 String
            /// </summary>
            /// <param name="stringPublicKey"></param>
            /// <param name="m_strEncryptString"></param>
            /// <returns></returns>
            public string RSAEncrypt_String(string stringPublicKey, string m_strEncryptString)
            {
    
                byte[] PlainTextBArray;
                byte[] CypherTextBArray;
                string Result;
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.ImportCspBlob(Convert.FromBase64String(stringPublicKey));
                PlainTextBArray = Encoding.UTF8.GetBytes(m_strEncryptString);
                CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
                Result = Convert.ToBase64String(CypherTextBArray);
                return Result;
    
            }
    
             /// <summary>
            /// RSA的解密函数 String
            /// </summary>
            /// <param name="stringPrivateKey">base64 string</param>
            /// <param name="m_strDecryptString">待解密</param>
            /// <returns></returns>
            public string RSADecrypt_String(string stringPrivateKey, string m_strDecryptString)
            {
                byte[] PlainTextBArray;
                byte[] DypherTextBArray;
                string Result;
                System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.ImportCspBlob(Convert.FromBase64String(stringPrivateKey));
                PlainTextBArray = Convert.FromBase64String(m_strDecryptString);
                DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
                Result = Encoding.UTF8.GetString(DypherTextBArray);
                return Result;
            }
    
  • 相关阅读:
    Ruby创始人谈Ruby的blocks和closure结构
    C语言字节对齐
    如今的开发者应了解哪些过去闻所未闻的新技能
    mongo下面总是缺少那么几个好用的工具试试这个吧MongoDB管理工具
    我们程序员为什么难晋升
    CMMI vs. Scrum vs. XP
    Rspec在Rails项目中的使用
    什么是Scrum?
    大型软件产品的敏捷案例 分享
    补充“为什么Scrum不行” (转自陈勇)
  • 原文地址:https://www.cnblogs.com/TTonly/p/11411581.html
Copyright © 2011-2022 走看看