zoukankan      html  css  js  c++  java
  • C#中RSA的简单使用

            static void Main(string[] args)
            {
                try
                {
                    //创建一个编码实例用来将字符串转换成byte数组
                    UnicodeEncoding ByteConverter = new UnicodeEncoding();
    
                    //Create byte arrays to hold original, encrypted, and decrypted data.
                    byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
                    byte[] encryptedData;
                    byte[] decryptedData;
                    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                    {
                        encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);
                        decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
                        Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
                    }
                }
                catch (ArgumentNullException)
                {
                    Console.WriteLine("Encryption failed.");
    
                }
                Console.Read();
            }
    
            static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
            {
                try
                {
                    byte[] encryptedData;
                    //创建一个RSACryptoServiceProvider实例.
                    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                    {
    
                        //指定共有钥匙
                        RSA.ImportParameters(RSAKeyInfo);
                        //加密,并且指定是否运行在XP更高的版本中
                        encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
                    }
                    return encryptedData;
                }
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.Message);
    
                    return null;
                }
    
            }
    
            static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
            {
                try
                {
                    byte[] decryptedData;
                    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                    {
                        RSA.ImportParameters(RSAKeyInfo);
                        decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
                    }
                    return decryptedData;
                }
                //Catch and display a CryptographicException  
                //to the console.
                catch (CryptographicException e)
                {
                    Console.WriteLine(e.ToString());
    
                    return null;
                }
    
            }

    Hold on, everything is possible.
  • 相关阅读:
    asp iis5.1x 2147467259 (0x80004005)
    asp 编辑 文本框为何会自动多出个逗号?
    asp 调用Recordset对象操作数据库
    windows2003 iis中播放flv格式的视频设置
    金额拆分 "万千百"..
    类似银行交易记录
    太扯了asp
    asp中通过Connection链接数据库
    给被Access过大问题困扰的网站,提供几种解决方案
    六款WEB上传组件性能测试与比较
  • 原文地址:https://www.cnblogs.com/student-note/p/6866561.html
Copyright © 2011-2022 走看看