zoukankan      html  css  js  c++  java
  • C# RAS 非对称加密类 支持长字符串

    /// <summary>
    /// ikmb@163.com
    /// </summary>
    public class MyRAS
        {
            /// <summary>
            /// RAS加密
            /// </summary>
            /// <param name="xmlPublicKey">公钥</param>
            /// <param name="EncryptString">明文</param>
            /// <returns>密文</returns>
     
            public static string RSAEncrypt(string xmlPublicKey, string EncryptString)
            {
                byte[] PlainTextBArray;
                byte[] CypherTextBArray;
                string Result=String.Empty;
                System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPublicKey);
                int t = (int)(Math.Ceiling((double)EncryptString.Length / (double)50));
                //分割明文
                for (int i = 0; i <= t-1; i++)
                {
    
                    PlainTextBArray = (new UnicodeEncoding()).GetBytes(EncryptString.Substring(i * 50, EncryptString.Length - (i * 50) > 50 ? 50 : EncryptString.Length - (i * 50)));
                    CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
                    Result += Convert.ToBase64String(CypherTextBArray) + "ThisIsSplit";
                }
                return Result;
            }
            /// <summary>
            /// RAS解密
            /// </summary>
            /// <param name="xmlPrivateKey">私钥</param>
            /// <param name="DecryptString">密文</param>
            /// <returns>明文</returns>
            public static string RSADecrypt(string xmlPrivateKey, string DecryptString)
            {
                byte[] PlainTextBArray;
                byte[] DypherTextBArray;
                string Result=String.Empty;
                System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPrivateKey);
                string[] Split = new string[1];
                Split[0] = "ThisIsSplit";
                //分割密文
                string[] mis = DecryptString.Split(Split, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < mis.Length; i++)
                {
                    PlainTextBArray = Convert.FromBase64String(mis[i]);
                    DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
                    Result += (new UnicodeEncoding()).GetString(DypherTextBArray);
                }
                return Result;
            }
     
            /// <summary>
            /// 产生公钥和私钥对
            /// </summary>
            /// <returns>string[] 0:私钥;1:公钥</returns>
            public static string[] RSAKey()
            {
                string[] keys = new string[2];
                System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                keys[0] = rsa.ToXmlString(true);
                keys[1] = rsa.ToXmlString(false);
                return keys;
            }
        }
  • 相关阅读:
    2019-2020-2 网络对抗技术 20175232 司浩楠 Exp2 后门原理与实践
    2019-2020-2 网络对抗技术 20175232 司浩楠 Exp1 PC平台逆向破解
    2019-2020-1 20175232 20175233 《信息安全系统设计基础》实验五 通讯协议设计
    Linux中Qt的安装
    面向对象编程与面向过程编程的区别与联系
    Web服务器端程序的实现
    Web服务器文件传输程序客户端程序实现
    Web服务器实现文件传输程序设计
    屏蔽信号的多路选择I/O
    浅谈HTTP请求与响应
  • 原文地址:https://www.cnblogs.com/niaowo/p/4757774.html
Copyright © 2011-2022 走看看