How to generate public/private key in C#. Asymmetric cryptography also known as public-key encryption uses a public/private key pair to encrypt and decrypt data. In.NET, the RSACryptoServiceProvider and DSACryptoServiceProvider classes are used for asymmetric encryption. Being this an RSA key the fields represent specific components of the algorithm. We find in order the modulus n = pq, the public exponent e, the private exponent d, the two prime numbers p and q, and the values dp, dq, and qinv (for the Chinese remainder theorem speed-up).
- Generate Rsa Public Key From Modulus Exponent C In Java
- Generate Rsa Public Key From Modulus Exponent C In Word
crypto.cs
usingSystem; |
usingSystem.Diagnostics; |
usingSystem.Security.Cryptography; |
usingSystem.Text; |
namespaceCrtypto |
{ |
classProgram |
{ |
staticvoidMain(string[] args) |
{ |
// generating public/private keys |
// |
//Debug.WriteLine('private: ' + RSA.ToXmlString(true)); |
//Debug.WriteLine('public: ' + RSA.ToXmlString(false)); |
varpublicKey= |
'<RSAKeyValue><Modulus>21wEnTU+mcD2w0Lfo1Gv4rtcSWsQJQTNa6gio05AOkV/Er9w3Y13Ddo5wGtjJ19402S71HUeN0vbKILLJdRSES5MHSdJPSVrOqdrll/vLXxDxWs/U0UT1c8u6k/Ogx9hTtZxYwoeYqdhDblof3E75d9n2F0Zvf6iTb4cI7j6fMs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>'; |
varprivateKey= |
'<RSAKeyValue><Modulus>21wEnTU+mcD2w0Lfo1Gv4rtcSWsQJQTNa6gio05AOkV/Er9w3Y13Ddo5wGtjJ19402S71HUeN0vbKILLJdRSES5MHSdJPSVrOqdrll/vLXxDxWs/U0UT1c8u6k/Ogx9hTtZxYwoeYqdhDblof3E75d9n2F0Zvf6iTb4cI7j6fMs=</Modulus><Exponent>AQAB</Exponent><P>/aULPE6jd5IkwtWXmReyMUhmI/nfwfkQSyl7tsg2PKdpcxk4mpPZUdEQhHQLvE84w2DhTyYkPHCtq/mMKE3MHw</P><Q>3WV46X9Arg2l9cxb67KVlNVXyCqc/w+LWt/tbhLJvV2xCF/0rWKPsBJ9MC6cquaqNPxWWEav8RAVbmmGrJt51Q</Q><DP>8TuZFgBMpBoQcGUoS2goB4st6aVq1FcG0hVgHhUI0GMAfYFNPmbDV3cY2IBt8Oj/uYJYhyhlaj5YTqmGTYbATQ</DP><DQ>FIoVbZQgrAUYIHWVEYi/187zFd7eMct/Yi7kGBImJStMATrluDAspGkStCWe4zwDDmdam1XzfKnBUzz3AYxrAQ</DQ><InverseQ>QPU3Tmt8nznSgYZ+5jUo9E0SfjiTu435ihANiHqqjasaUNvOHKumqzuBZ8NRtkUhS6dsOEb8A2ODvy7KswUxyA</InverseQ><D>cgoRoAUpSVfHMdYXW9nA3dfX75dIamZnwPtFHq80ttagbIe4ToYYCcyUz5NElhiNQSESgS5uCgNWqWXt5PnPu4XmCXx6utco1UVH8HGLahzbAnSy6Cj3iUIQ7Gj+9gQ7PkC434HTtHazmxVgIR5l56ZjoQ8yGNCPZnsdYEmhJWk=</D></RSAKeyValue>'; |
vartestData=Encoding.UTF8.GetBytes('testing'); |
using ( varrsa=newRSACryptoServiceProvider(1024)) |
{ |
try |
{ |
// client encrypting data with public key issued by server |
// |
rsa.FromXmlString(publicKey); |
varencryptedData=rsa.Encrypt(testData, true); |
varbase64Encrypted=Convert.ToBase64String(encryptedData); |
Debug.WriteLine(base64Encrypted); |
// server decrypting data with private key |
// |
rsa.FromXmlString(privateKey); |
varresultBytes=Convert.FromBase64String(base64Encrypted); |
vardecryptedBytes=rsa.Decrypt(resultBytes, true); |
vardecryptedData=Encoding.UTF8.GetString(decryptedBytes); |
Debug.WriteLine(decryptedData); |
} |
finally |
{ |
rsa.PersistKeyInCsp=false; |
} |
} |
} |
} |
} |
commented May 9, 2015
Why exactly do you need ToBase64String and FromBase64String? I've seen some people saying this is necessary: http://stackoverflow.com/a/2164186/145349 https://disakaiserbot.weebly.com/generating-ssh-key-aws-s3.html. Can you confirm this? Session key example. |
commented Jul 26, 2017
@fjsj it's good to use Base64 encoding to encode a byte sequence to a string instead of Unicode encoding, because not every byte sequence has a Unicode representation. In this example, when the RSA encrypts the input bytes you can't be sure if the output bytes have a Unicode representation, but you can be sure that they have a Base64 representation. |
commented Feb 2, 2018
Generate Rsa Public Key From Modulus Exponent C In Java
![Generate Rsa Public Key From Modulus Exponent C Generate Rsa Public Key From Modulus Exponent C](/uploads/1/2/6/0/126088363/944940783.jpg)
Thanks for code! Rsa key generation function c++. |
Generate Rsa Public Key From Modulus Exponent C In Word
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment