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

            public static string RsaEncrypt(string publickey, string content)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                byte[] cipherbytes;
                rsa.FromXmlString(publickey);
                cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
                return Convert.ToBase64String(cipherbytes);
            }
            public static string RSADecrypt(string privatekey, string content)
            {
                var rsa = new RSACryptoServiceProvider();
                byte[] cipherbytes;
                rsa.FromXmlString(privatekey);
                cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
                return Encoding.UTF8.GetString(cipherbytes);
            } 

    生成key:

                string _publicKey;
                string _privateKey;
                RSACryptoServiceProvider _rsaService;
                _rsaService = new RSACryptoServiceProvider();
                _publicKey = _rsaService.ToXmlString(false);
                _privateKey = _rsaService.ToXmlString(true);

    可java一起用:

    public static string RSAEncryptSpilt(string publickey, string content)
            {
                //公钥
                //publickey = _publickey; 
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    
                byte[] byteArray = Encoding.UTF8.GetBytes(content);
                //byte[] byteArray = System.Text.Encoding.Default.GetBytes(content);
                content = Convert.ToBase64String(byteArray);
                rsa.FromXmlString(publickey);
                byte[] OriginalData = Convert.FromBase64String(content);
    
                if (OriginalData == null || OriginalData.Length <= 0)
                {
                    throw new NotSupportedException();
                }
                if (rsa == null)
                {
                    throw new ArgumentNullException();
                }
    
    
                int bufferSize = (rsa.KeySize / 8) - 37;
                byte[] buffer = new byte[bufferSize];
            
                using (MemoryStream input = new MemoryStream(OriginalData))
                using (MemoryStream ouput = new MemoryStream())
                {
                    while (true)
                    {
                        int readLine = input.Read(buffer, 0, bufferSize);
                        if (readLine <= 0)
                        {
                            break;
                        }
                        byte[] temp = new byte[readLine];
                        Array.Copy(buffer, 0, temp, 0, readLine);
                        byte[] encrypt = rsa.Encrypt(temp, false);
                        ouput.Write(encrypt, 0, encrypt.Length);
                    }
                    return Convert.ToBase64String(ouput.ToArray());
                }
            }
    
            public static string RSADecryptSpilt(string privatekey, string content)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    
                byte[] EncryptDada = Convert.FromBase64String(content);
    
                if (EncryptDada == null || EncryptDada.Length <= 0)
                {
                    throw new NotSupportedException();
                }
                //私钥
                //privatekey = _privatekey; 
    
                rsa.FromXmlString(privatekey);
                int keySize = rsa.KeySize / 8;
                byte[] buffer = new byte[keySize];
    
                using (MemoryStream input = new MemoryStream(EncryptDada))
                using (MemoryStream output = new MemoryStream())
                {
                    while (true)
                    {
                        int readLine = input.Read(buffer, 0, keySize);
                        if (readLine <= 0)
                        {
                            break;
                        }
                        byte[] temp = new byte[readLine];
                        Array.Copy(buffer, 0, temp, 0, readLine);
                        byte[] decrypt = rsa.Decrypt(temp, false);
                        output.Write(decrypt, 0, decrypt.Length);
                    }
                    return Encoding.UTF8.GetString(output.ToArray());
                }
    
            }
  • 相关阅读:
    北斗授时系统,GPS授时服务器—在行业应用及介绍
    GPS时间同步服务器,NTP时间同步服务器,——数据采集系统应用
    北斗卫星时钟(北斗授时服务器)厂家介绍及价格分析
    linux系统下ntp网络时钟服务器(NTP服务器)的搭建和使用
    2020年SAP项目艰辛曲折的开工历程 III
    2020年SAP项目艰辛曲折的开工历程 II
    2020年SAP项目艰辛曲折的开工历程 I
    2020年肺炎疫情期间看的几部古装电视剧
    做人不忘本,才能走得更远 --- 我看电视剧《雍正王朝》
    邬先生及时功成身退,是明哲保身的聪明做法 --- 我看电视剧《雍正王朝》
  • 原文地址:https://www.cnblogs.com/ly7454/p/4546424.html
Copyright © 2011-2022 走看看