Database Reference
In-Depth Information
24, you create the RSA encryption object and call its Encrypt method on line 27. Because encrypting with
RSA automatically incorporates a vector, there is no need to keep track of it. So, the CipherText vector
variable is set to 0:
1. private string _THUMBPRINT_ =
2. "01 71 11 17 0a b4 96 7b ca 1f f3 e5 bc 0f 68 9d c6 c0 3b 7b";
3.
4. /// <summary>
5. /// Encrypts a string value using a self-signed certificate
6. /// </summary>
7. /// <param name="value">The value to encrypt</param>
8. /// <returns></returns>
9. public CipherText EncryptByCert(string value)
10. {
11. byte[] buffer = UTF8Encoding.UTF8.GetBytes(value);
12.
13. X509Store store = new X509Store(StoreName.Root,
14. StoreLocation.LocalMachine);
15. store.Open(OpenFlags.ReadOnly);
16.
17. X509Certificate2 x509 =
18. store.Certificates.Find(
19. X509FindType.FindByThumbprint,
20. _THUMBPRINT_, true)[0];
21.
22. store.Close();
23.
24. RSACryptoServiceProvider rsaEncrypt = null;
25. rsaEncrypt = (RSACryptoServiceProvider)x509.PublicKey.Key;
26.
27. byte[] encryptedBytes = rsaEncrypt.Encrypt(buffer, false);
28.
29. CipherText ct = new CipherText();
30. ct.cipher = encryptedBytes;
31. ct.vector = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0,
32. 0, 0, 0, 0, 0, 0, 0};
33.
34. return ct;
35. }
The decryption code is shown next and is very similar to the preceding example. You make a call to
Decrypt instead of Encrypt on the RSA object:
1. public string DecryptByCert(CipherText ct)
2. {
3. X509Store store = new X509Store(StoreName.Root,
4. StoreLocation.LocalMachine);
5. store.Open(OpenFlags.ReadOnly);
6.
7. X509Certificate2 x509 =
8. store.Certificates.Find(
9. X509FindType.FindByThumbprint,
10. _THUMBPRINT_, true)[0];
11. store.Close();
Search WWH ::




Custom Search