当前位置: 首页>>代码示例>>C#>>正文


C# RSACryptoServiceProvider.Decrypt方法代码示例

本文整理汇总了C#中RSACryptoServiceProvider.Decrypt方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.Decrypt方法的具体用法?C# RSACryptoServiceProvider.Decrypt怎么用?C# RSACryptoServiceProvider.Decrypt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RSACryptoServiceProvider的用法示例。


在下文中一共展示了RSACryptoServiceProvider.Decrypt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DecryptSavedAnswer

        public static void DecryptSavedAnswer()
        {
            byte[] cipherBytes =
            {
                0x35, 0x6F, 0x8F, 0x2C, 0x4D, 0x1A, 0xAC, 0x6D,
                0xE7, 0x52, 0xA5, 0xDF, 0x26, 0x54, 0xA6, 0x34,
                0xF5, 0xBB, 0x14, 0x26, 0x1C, 0xE4, 0xDC, 0xA2,
                0xD8, 0x4D, 0x8F, 0x1C, 0x55, 0xD4, 0xC7, 0xA7,
                0xF2, 0x3C, 0x99, 0x77, 0x9F, 0xE4, 0xB7, 0x34,
                0xA6, 0x28, 0xB2, 0xC4, 0xFB, 0x6F, 0x85, 0xCA,
                0x19, 0x21, 0xCA, 0xC1, 0xA7, 0x8D, 0xAE, 0x95,
                0xAB, 0x9B, 0xA9, 0x88, 0x5B, 0x44, 0xC6, 0x9B,
                0x44, 0x26, 0x71, 0x5D, 0x02, 0x3F, 0x43, 0x42,
                0xEF, 0x4E, 0xEE, 0x09, 0x87, 0xEF, 0xCD, 0xCF,
                0xF9, 0x88, 0x99, 0xE8, 0x49, 0xF7, 0x8F, 0x9B,
                0x59, 0x68, 0x20, 0xF3, 0xA7, 0xB2, 0x94, 0xA4,
                0x23, 0x70, 0x83, 0xD9, 0xAC, 0xE7, 0x5E, 0xEE,
                0xE9, 0x7B, 0xE4, 0x4F, 0x73, 0x2E, 0x9B, 0xD8,
                0x2A, 0x75, 0xFB, 0x6C, 0xB9, 0x39, 0x6D, 0x72,
                0x8A, 0x9C, 0xCD, 0x58, 0x1A, 0x27, 0x79, 0x97,
            };

            byte[] output;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(TestData.RSA1024Params);
                output = rsa.Decrypt(cipherBytes, true);
            }

            Assert.Equal(TestData.HelloBytes, output);
        }
开发者ID:AustinWise,项目名称:corefx,代码行数:32,代码来源:EncryptDecrypt.cs

示例2: DecryptSavedAnswerUnusualExponent

        public static void DecryptSavedAnswerUnusualExponent()
        {
            byte[] cipherBytes =
            {
                0x55, 0x64, 0x05, 0xF7, 0xBF, 0x99, 0xD8, 0x07,
                0xD0, 0xAC, 0x1B, 0x1B, 0x60, 0x92, 0x57, 0x95,
                0x5D, 0xA4, 0x5B, 0x55, 0x0E, 0x12, 0x90, 0x24,
                0x86, 0x35, 0xEE, 0x6D, 0xB3, 0x46, 0x3A, 0xB0,
                0x3D, 0x67, 0xCF, 0xB3, 0xFA, 0x61, 0xBB, 0x90,
                0x6D, 0x6D, 0xF8, 0x90, 0x5D, 0x67, 0xD1, 0x8F,
                0x99, 0x6C, 0x31, 0xA2, 0x2C, 0x8E, 0x99, 0x7E,
                0x75, 0xC5, 0x26, 0x71, 0xD1, 0xB0, 0xA5, 0x41,
                0x67, 0x19, 0xF7, 0x40, 0x04, 0xBE, 0xB2, 0xC0,
                0x97, 0xFB, 0xF6, 0xD4, 0xEF, 0x48, 0x5B, 0x93,
                0x81, 0xF8, 0xE1, 0x6A, 0x0E, 0xA0, 0x74, 0x6B,
                0x99, 0xC6, 0x23, 0xF5, 0x02, 0xDE, 0x47, 0x49,
                0x1E, 0x9D, 0xAE, 0x55, 0x20, 0xB5, 0xDE, 0xA0,
                0x04, 0x32, 0x37, 0x4B, 0x24, 0xE4, 0x64, 0x1B,
                0x1B, 0x4B, 0xC0, 0xC7, 0x30, 0x08, 0xA6, 0xAE,
                0x50, 0x86, 0x08, 0x34, 0x70, 0xE5, 0xB0, 0x3B,
            };

            byte[] output;

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(TestData.UnusualExponentParameters);
                output = rsa.Decrypt(cipherBytes, true);
            }

            Assert.Equal(TestData.HelloBytes, output);
        }
开发者ID:AustinWise,项目名称:corefx,代码行数:32,代码来源:EncryptDecrypt.cs

示例3: Decrypt

 private static byte[] Decrypt(string privateKeyXml, byte[] encryptedBytes, RsaKeyLengths rsaKeyLength)
 {
     using (var RSA = new RSACryptoServiceProvider((int) rsaKeyLength))
     {
         RSA.FromXmlString(privateKeyXml);
         byte[] bytes = RSA.Decrypt(encryptedBytes, DoOAEPPadding);
         return bytes;
     }
 }
开发者ID:HAlakeshwar,项目名称:ServiceStack,代码行数:9,代码来源:CryptUtils.cs

示例4: DecryptRSA

    /// <summary>
    ///     A string extension method that decrypt a string.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <param name="key">The key.</param>
    /// <returns>The decrypted string.</returns>
    /// <example>
    ///     <code>
    ///           using Microsoft.VisualStudio.TestTools.UnitTesting;
    ///           using Z.ExtensionMethods;
    ///           
    ///           namespace ExtensionMethods.Examples
    ///           {
    ///               [TestClass]
    ///               public class System_String_DecryptRSA
    ///               {
    ///                   [TestMethod]
    ///                   public void DecryptRSA()
    ///                   {
    ///                       // Type
    ///                       string @this = &quot;7E-24-A5-CF-8E-7A-83-52-90-CA-81-6F-26-04-7C-E6-F4-25-47-26-A9-55-04-83-32-78-1A-C0-E4-5D-90-66-A6-E1-58-59-A8-48-E1-20-21-B9-FE-84-31-53-52-9B-45-E1-B2-93-71-92-DA-29-5B-99-D1-41-19-9C-3E-13-4B-2B-BC-08-94-31-A4-F9-B9-0A-04-2F-C6-78-B1-47-27-11-2C-E6-AF-BF-A8-F2-F3-F6-4E-CB-EB-79-5E-80-C6-A1-0A-D7-7C-F1-16-0E-41-14-4E-76-7E-9E-DD-61-BF-11-5E-62-79-2D-C4-11-D2-F6-3D-7F-DD-87-C4-4E&quot;;
    ///           
    ///                       // Examples
    ///                       string value = @this.DecryptRSA(&quot;Buzz&quot;); // return &quot;Fizz&quot;;
    ///           
    ///                       // Unit Test
    ///                       Assert.AreEqual(&quot;Fizz&quot;, value);
    ///                   }
    ///               }
    ///           }
    ///     </code>
    /// </example>
    public static string DecryptRSA(this string @this, string key) {
        var cspp = new CspParameters {KeyContainerName = key};
        var rsa = new RSACryptoServiceProvider(cspp) {PersistKeyInCsp = true};
        string[] decryptArray = @this.Split(new[] {"-"}, StringSplitOptions.None);
        byte[] decryptByteArray = Array.ConvertAll(decryptArray, (s => Convert.ToByte(byte.Parse(s, NumberStyles.HexNumber))));
        byte[] bytes = rsa.Decrypt(decryptByteArray, true);

        return Encoding.UTF8.GetString(bytes);
    }
开发者ID:Nucs,项目名称:nlib,代码行数:41,代码来源:String.DecryptRSA.cs

示例5: Main

    public static void Main(String[] args) {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        RijndaelManaged aes = new RijndaelManaged();
        aes.KeySize = 128; //  192;
        aes.Key = new byte[] {0xC7, 0x42, 0xD1, 0x37, 0x4B, 0xAC, 0xFE, 0x94,
                              0x9D, 0x59, 0x79, 0x92, 0x71, 0x48, 0xD6, 0x8E};
//                              ,1,2,3,4,5,6,7,8};

        byte[] aesKey = aes.Key;
        if (args.Length > 0) {
            FileStream fs = new FileStream(args[0], FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            String xmlString = sr.ReadToEnd();
            rsa.FromXmlString(xmlString);
        } else {
            FileStream fs = new FileStream("rsa.xml", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(rsa.ToXmlString(true));
            sw.Close();
        }
        Console.WriteLine("RSA Key is:\n"+rsa.ToXmlString(true));

        Console.WriteLine("AES Key is:");
        PrintByteArray(aesKey);
        byte[] encryptedKey1 = rsa.Encrypt(aesKey, true);
        Console.WriteLine("Encrypted AES Key is:");
        PrintByteArray(encryptedKey1);
        byte[] decryptedKey1 = rsa.Decrypt(encryptedKey1, true);
        Console.WriteLine("Decrypted AES Key is:");
        PrintByteArray(decryptedKey1);

        byte[] encryptedKey2 = rsa.Encrypt(aesKey, false);
        Console.WriteLine("Encrypted2 AES Key is:");
        PrintByteArray(encryptedKey2);
        byte[] decryptedKey2 = rsa.Decrypt(encryptedKey2, false);
        Console.WriteLine("Decrypted AES Key is:");
        PrintByteArray(decryptedKey2);

    }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:39,代码来源:RSAOAEP.cs

示例6: DecryptString

 /// <summary>
 /// 字符串解密
 /// </summary>
 /// <param name="encryptString">密文</param>
 /// <param name="privateKey">私钥</param>
 /// <returns>遇到解密失败将会返回原字符串</returns>
 public static string DecryptString(string encryptString, string privateKey)
 {
     string source = string.Empty;
     try
     {
         RSACryptoServiceProvider.UseMachineKeyStore = true;
         RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
         rsaProvider.FromXmlString(FromBase64Key(privateKey));
         byte[] data = rsaProvider.Decrypt(HexStringToBytes(encryptString), false);
         source = System.Text.Encoding.ASCII.GetString(data);
     }
     catch { }
     return source;
 }
开发者ID:hj-nicholas,项目名称:BaseFrame,代码行数:20,代码来源:RSAHelper.cs

示例7: RSADecrypt

 public static string RSADecrypt(string encryptedString)
 {
     CspParameters param = new CspParameters();
     param.KeyContainerName = "PowerGridIndia";
     using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
     {
         int len = encryptedString.Length;
         encryptedString = encryptedString.Replace(" ", "+");
         byte[] encryptdata = Convert.FromBase64String(encryptedString);
         byte[] decryptdata = rsa.Decrypt(encryptdata, false);
         string plaindata = System.Text.Encoding.Default.GetString(decryptdata);
         return plaindata;
     }
 }
开发者ID:raazalok,项目名称:IntranetHome,代码行数:14,代码来源:EncDec.cs

示例8: RsaCryptRoundtrip

        public static void RsaCryptRoundtrip()
        {
            byte[] crypt;
            byte[] output;

            using (var rsa = new RSACryptoServiceProvider())
            {
                crypt = rsa.Encrypt(TestData.HelloBytes, true);
                output = rsa.Decrypt(crypt, true);
            }

            Assert.NotEqual(crypt, output);
            Assert.Equal(TestData.HelloBytes, output);
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:14,代码来源:EncryptDecrypt.cs

示例9: RsaDecryptAfterExport

        public static void RsaDecryptAfterExport()
        {
            byte[] output;

            using (var rsa = new RSACryptoServiceProvider())
            {
                byte[] crypt = rsa.Encrypt(TestData.HelloBytes, true);

                // Export the key, this should not clear/destroy the key.
                RSAParameters ignored = rsa.ExportParameters(true);
                output = rsa.Decrypt(crypt, true);
            }

            Assert.Equal(TestData.HelloBytes, output);
        }
开发者ID:johnhhm,项目名称:corefx,代码行数:15,代码来源:EncryptDecrypt.cs

示例10: Decrypt

    private static string Decrypt(byte[] encData)
    {
        var prvParams = "";

        using (var reader = new StreamReader(new FileStream("1.pub", FileMode.Open)))
        {
            prvParams = reader.ReadToEnd();
        }

        using (var rsa = new RSACryptoServiceProvider())
        {
            rsa.FromXmlString(prvParams);

            return ByteConverter.GetString(rsa.Decrypt(encData, false));
        }
    }
开发者ID:vrublevskiy,项目名称:UANB_wpf_client,代码行数:16,代码来源:Program.cs

示例11: RSADecrypt

    public static byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo)
    {
        try
        {
            byte[] decryptedData;

            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {
                RSA.ImportParameters(RSAKeyInfo);
                decryptedData = RSA.Decrypt(DataToDecrypt,false);
            }
            return decryptedData;
        }
        catch (CryptographicException e)
        {
            Console.WriteLine(e.ToString());

            return null;
        }
    }
开发者ID:saeidghoreshi,项目名称:partition1,代码行数:20,代码来源:Program.cs

示例12: Encrypt

	public static string Encrypt(string toEncrypt)
	{
		RSACryptoServiceProvider CSPRSA = null;
		string result;
		try
		{
			CSPRSA = new RSACryptoServiceProvider();
			CSPRSA.FromXmlString(SAFSecurityKeys.loadKeysFromFile());
			byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
			result = Convert.ToBase64String(CSPRSA.Decrypt(toEncryptArray, false));
		}
		catch
		{
			result = null;
		}
		finally
		{
			if (CSPRSA != null)
			{
				CSPRSA.Clear();
			}
		}
		return result;
	}
开发者ID:apedro-silva,项目名称:SF.Expand.SAF,代码行数:24,代码来源:CryptorEngineRSA.cs

示例13: Decrypt

 public static byte[] Decrypt(byte[] encryptedBytes, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
 {
     using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
     {
         rsa.FromXmlString(privateKeyXml);
         byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding);
         return bytes;
     }
 }
开发者ID:pkmnfrk,项目名称:ServiceStack,代码行数:9,代码来源:CryptUtils.cs

示例14: RSADecrypt

            public static byte[] RSADecrypt(byte[] dataToDecrypt, RSAParameters pars, bool padding)
            {
                try
                {
                    byte[] encryptedData;
                    using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                    {
                        RSA.ImportParameters(pars);
                        encryptedData = RSA.Decrypt(dataToDecrypt, padding);
                    }
                    return encryptedData;
                }
                catch (Exception)
                {

                    return null;

                }
            }
开发者ID:techspider,项目名称:spykiller,代码行数:19,代码来源:SPYAPI.cs

示例15: StartClient

    public static void StartClient()
    {
        // Data buffer for incoming data.
        byte[] bytes;

        // Connect to a remote device.
        try
        {
            // Establish the remote endpoint for the socket.
            // This example uses port 11000 on the local computer.
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

            // Create a TCP/IP  socket.
            Socket sender = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            // Connect the socket to the remote endpoint. Catch any errors.
            try
            {
                sender.Connect(remoteEP);

                Console.WriteLine("Socket connected to {0}",
                    sender.RemoteEndPoint.ToString());

                // Encode the data string into a byte array.
                //   byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");

                // Send the data through the socket.
                // int bytesSent = sender.Send(msg);

                // Receive the response from the remote device.

                string data = null;
                bytes = new byte[16000];
                while (true)
                {
                    int bytesCount = sender.Receive(bytes);

                    //answer = bytes;
                    data = Encoding.ASCII.GetString(bytes);
                    if (data.IndexOf("{Key}") > -1)
                    {
                        byte[] answer = new byte[256];
                        for (int i = 0; i < 256; i++)
                            answer[i] = bytes[i + 5];
                        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                        rsa.FromXmlString(File.ReadAllText("c:\\private.txt"));
                        keyAES = rsa.Decrypt(answer, false);
                        byte[] buff = Encoding.UTF8.GetBytes("Ключ был успешно получен!");
                        sender.Send(buff);
                        Console.Write("Сессионный ключ был успешно получен\n");
                        data = null;
                        bytesCount = sender.Receive(bytes);

                        //answer = bytes;
                        data = Encoding.ASCII.GetString(bytes);

                    }
                    if (data!=null)
                    {

                        byte[] encMes = new byte[bytesCount];
                        Array.Copy(bytes, 0, encMes, 0, bytesCount);
                        AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
                        aes.KeySize = 256;
                        aes.Key = keyAES;
                        //aes.Padding = PaddingMode.Zeros;
                        byte[] iv = new byte[16];
                        aes.IV = iv;
                        ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                        // Create the streams used for decryption.
                        string plaintext;
                        using (MemoryStream msDecrypt = new MemoryStream(encMes))
                        {
                            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                            {
                                using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                                {

                                    // Read the decrypted bytes from the decrypting stream
                                    // and place them in a string.
                                    plaintext = srDecrypt.ReadToEnd();
                                    File.WriteAllText("c:\\result", plaintext);
                                    Console.WriteLine("Расшифрованный принятый файл : {0}", plaintext);
                                }
                            }
                        }
                        break;
                    }
                }

                Console.Read();
                // Release the socket.
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();

            }
//.........这里部分代码省略.........
开发者ID:Zlover,项目名称:CipherFile,代码行数:101,代码来源:Program.cs


注:本文中的RSACryptoServiceProvider.Decrypt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。