Csharp/CSharp Tutorial/Security/RSA

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Asymmetric cryptography

<source lang="csharp">using System; using System.IO; using System.Security.Cryptography; class MainClass {

 public static void Main() 
 {
   RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
   Byte[] testData = {1, 2, 3, 4, 5, 6, 7, 8};
   Byte[] encryptedData = rsa.Encrypt(testData, false);
   Console.WriteLine("Encrypted data:");
   for(int i=0; i<encryptedData.GetLength(0); i++)
   {
     Console.Write("{0} ", encryptedData[i]);
   }
   Byte[] decryptedData = rsa.Decrypt(encryptedData, false);
   Console.WriteLine("Decrypted Data:");
   for(int i=0; i<decryptedData.GetLength(0); i++)
   {
     Console.Write("{0} ", decryptedData[i]);
   }
 }

}</source>

Encrypted data:
105 216 155 138 34 149 122 27 220 172 6 69 23 21 224 142 30 166 81 141 15 234 144 235 122 187 99 245
 222 252 154 234 211 79 251 80 253 221 94 91 222 86 225 17 0 96 161 179 155 251 123 140 38 6 161 78
111 193 19 222 251 74 172 104 100 61 39 106 113 67 69 45 237 47 194 189 62 168 98 230 196 149 249 11
3 29 19 66 10 84 73 110 142 142 255 120 138 200 207 79 190 151 164 53 4 198 254 78 203 86 102 233 10
7 216 13 41 166 125 155 58 48 214 27 116 93 211 176 191 183 Decrypted Data:
1 2 3 4 5 6 7 8

Encrypt with RSACryptoServiceProvider

<source lang="csharp">using System; using System.IO; using System.Security; using System.Security.Cryptography; using System.Collections.Generic; using System.Text;

   class Program
   {
       static void Main(string[] args)
       {
           RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
           StreamReader sr = File.OpenText("myKey.xml");
           string rsaXml = sr.ReadToEnd();
           sr.Close();
           rsa.FromXmlString(rsaXml);
           string messageToJane = "this is a test";
           byte[] encrypted = rsa.Encrypt(System.Text.ASCIIEncoding.ASCII.GetBytes(messageToJane), false);
           FileStream fs = new FileStream("Message.dat", FileMode.Create);
           fs.Write(encrypted, 0, encrypted.Length);
           fs.Close();
       }
   }</source>

RSACryptoServiceProvider reads from xml key

<source lang="csharp">using System; using System.IO; using System.Security; using System.Security.Cryptography; using System.Collections.Generic; using System.Text;

   class Program
   {
       static void Main(string[] args)
       {
           StreamReader sr = File.OpenText("myKey.xml");
           string myKey = sr.ReadToEnd();
           sr.Close();
           RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
           rsa.FromXmlString(myKey);
           FileStream fs = new FileStream("Message.dat", FileMode.Open);
           byte[] encrypted = new byte[fs.Length];
           fs.Read(encrypted, 0, (int)fs.Length);
           byte[] decrypted = rsa.Decrypt(encrypted, false);
           fs.Close();
           Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(decrypted));
           
       }
   }</source>

Using RSACryptoServiceProvider

<source lang="csharp">using System; using System.IO; using System.Security.Cryptography; using System.Collections.Generic; using System.Text;

   class Program
   {
       static void Main(string[] args)
       {
           string verifiableMesage = "this is a test";
           SHA1Managed sha = new SHA1Managed();
           byte[] hashValue = sha.ruputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(verifiableMesage));
           StreamReader sr = File.OpenText("Key.xml");
           string myKey = sr.ReadToEnd();
           RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
           rsa.FromXmlString(myKey);
           RSAPKCS1SignatureFormatter sigFormatter = new RSAPKCS1SignatureFormatter(rsa);
           sigFormatter.SetHashAlgorithm("SHA1");
           byte[] signedHash = sigFormatter.CreateSignature(hashValue);
           FileStream fs = new FileStream("signedHash.dat", FileMode.Create);
           fs.Write(signedHash, 0, signedHash.Length);
           fs.Close();
       }
   }</source>

Using RSAPKCS1SignatureDeformatter

<source lang="csharp">using System; using System.IO; using System.Security; using System.Security.Cryptography; using System.Collections.Generic; using System.Text;

   class Program
   {
       static void Main(string[] args)
       {
           string verifiableMesage = "this is a test";
           string     wrongMessage = "this is another test";
           SHA1Managed sha = new SHA1Managed();
           byte[] verifiableMessageHash = sha.ruputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(verifiableMesage));
           byte[] wrongMessageHash = sha.ruputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(wrongMessage));
           FileStream fs = new FileStream("signedHash.dat", FileMode.Open);
           byte[] fileHash = new byte[fs.Length];
           fs.Read(fileHash, 0, (int)fs.Length);
           fs.Close();
           StreamReader sr = File.OpenText("myKey.xml");
           string myKey = sr.ReadToEnd();
           sr.Close();
           RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
           rsa.FromXmlString(myKey);
           RSAPKCS1SignatureDeformatter sigDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
           sigDeformatter.SetHashAlgorithm("SHA1");
           Console.WriteLine(sigDeformatter.VerifySignature(verifiableMessageHash, fileHash));
           Console.WriteLine(sigDeformatter.VerifySignature(wrongMessageHash, fileHash) == false);
       }
   }</source>