Certificate Encryption

I am completely new to this whole encryption issue, and I'm trying to make a simple application to encrypt this string. Here is my code:

public static X509Certificate2 getPublicKey() { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); X509Certificate2 cert2 = new X509Certificate2("c:\\certificate.cer"); return cert2; } public static string cipherRequest(byte[] stringToEncrypt) { X509Certificate2 certificate = getPublicKey(); RSACryptoServiceProvider rsa = certificate.PublicKey.Key as RSACryptoServiceProvider; byte[] cryptedData = rsa.Encrypt(stringToEncrypt, true); return Convert.ToBase64String(cryptedData); } public static void Main() { try { ASCIIEncoding ByteConverter = new ASCIIEncoding(); byte[] test = ByteConverter.GetBytes("stringtoencrypt"); string first = cipherRequest(test); string second= cipherRequest(test); Console.WriteLine("first: {0}", first); Console.WriteLine("second: {0}", second); } catch(CryptographicException e) { Console.WriteLine(e.Message); } } 

Therefore, every time I call cipherRequest , it produces different results. I checked that the certificate is uploaded, but it gives different results.

Any thoughts?

+7
source share
1 answer

A random addition is added before the actual encryption to avoid certain attacks. This is why you get different results every time you call the encryption method.

See this post for more details:

Does RSA in C # not create the same encrypted string for certain keys?

+7
source

All Articles