當前位置: 首頁>>代碼示例>>C#>>正文


C# RSACryptoServiceProvider.Decrypt方法代碼示例

本文整理匯總了C#中System.Security.Cryptography.RSACryptoServiceProvider.Decrypt方法的典型用法代碼示例。如果您正苦於以下問題:C# RSACryptoServiceProvider.Decrypt方法的具體用法?C# RSACryptoServiceProvider.Decrypt怎麽用?C# RSACryptoServiceProvider.Decrypt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Security.Cryptography.RSACryptoServiceProvider的用法示例。


在下文中一共展示了RSACryptoServiceProvider.Decrypt方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{
    static void Main()
    {
        try
        {
            //Create a UnicodeEncoder to convert between byte array and string.
            ASCIIEncoding ByteConverter = new ASCIIEncoding();

            string dataString = "Data to Encrypt";

            //Create byte arrays to hold original, encrypted, and decrypted data.
            byte[] dataToEncrypt = ByteConverter.GetBytes(dataString);
            byte[] encryptedData;
            byte[] decryptedData;

            //Create a new instance of the RSACryptoServiceProvider class 
            // and automatically create a new key-pair.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            //Display the origianl data to the console.
            Console.WriteLine("Original Data: {0}", dataString);

            //Encrypt the byte array and specify no OAEP padding.  
            //OAEP padding is only available on Microsoft Windows XP or
            //later.  
            encryptedData = RSAalg.Encrypt(dataToEncrypt, false);

            //Display the encrypted data to the console. 
            Console.WriteLine("Encrypted Data: {0}", ByteConverter.GetString(encryptedData));

            //Pass the data to ENCRYPT and boolean flag specifying 
            //no OAEP padding.
            decryptedData = RSAalg.Decrypt(encryptedData, false);

            //Display the decrypted plaintext to the console. 
            Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
        }
        catch(CryptographicException e)
        {
            //Catch this exception in case the encryption did
            //not succeed.
            Console.WriteLine(e.Message);
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Security.Cryptography,代碼行數:51,代碼來源:RSACryptoServiceProvider.Decrypt

示例2: Main

//引入命名空間
using System;
using System.IO;
using System.Security.Cryptography;

public class Example19_11 
{
    public static void Main() 
    {

        // Create a new crypto provider
        RSACryptoServiceProvider rsa = 
            new RSACryptoServiceProvider();

        // Data to encrypt
        Byte[] testData = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        // Encrypt the data
        Byte[] encryptedData = rsa.Encrypt(testData, false);
        Console.WriteLine("Encrypted data:");
        for(int i=0; i<encryptedData.GetLength(0); i++)
        {
            Console.Write("{0} ", encryptedData[i]);
        }
        Console.WriteLine();

        // Decrypt the data
        Byte[] decryptedData = rsa.Decrypt(encryptedData, false);
        Console.WriteLine("Decrypted Data:");
        for(int i=0; i<decryptedData.GetLength(0); i++)
        {
            Console.Write("{0} ", decryptedData[i]);
        }
        Console.WriteLine();
    }

}
開發者ID:C#程序員,項目名稱:System.Security.Cryptography,代碼行數:37,代碼來源:RSACryptoServiceProvider.Decrypt


注:本文中的System.Security.Cryptography.RSACryptoServiceProvider.Decrypt方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。