Rsacryptoserviceprovider Example

Rsacryptoserviceprovider Example
Simple RSA Encryption, Decryption, signing and signature verification using Base64 encoded strings in C#
RSA.cs
Rsacryptoserviceprovider Example
usingSystem;
usingSystem.Security.Cryptography;
usingSystem.Text;
namespaceMyProject.Data.Encryption
{
/* Encryption, decryption, signing and signature verification that's compatible with
* simple-free-encryption-tool
*/
publicclassRSA
{
publicconstboolOAEP_PADDING=true;
/// <summary>
/// PKCS1 padding is required for most encryption using JavaScript packages
/// </summary>
publicconstboolPKCS1_PADDING=false;
publicstaticstringEncrypt(
RSACryptoServiceProvidercsp,
stringplaintext
) {
returnConvert.ToBase64String(
csp.Encrypt(
Encoding.UTF8.GetBytes(plaintext),
PKCS1_PADDING
)
);
}
publicstaticstringDecrypt(
RSACryptoServiceProvidercsp,
stringencrypted
) {
returnEncoding.UTF8.GetString(
csp.Decrypt(
Convert.FromBase64String(encrypted),
PKCS1_PADDING
)
);
}
publicstaticstringSign(
RSACryptoServiceProvidercsp,
stringplaintext
) {
// compute sha256 hash of the data
byte[] hash=newSHA256CryptoServiceProvider()
.ComputeHash(Encoding.UTF8.GetBytes(plaintext));
// base64 encode the signature
returnConvert.ToBase64String(
csp.SignHash(hash, CryptoConfig.MapNameToOID('SHA256'))
);
}
publicstaticboolVerify(
RSACryptoServiceProvidercsp,
stringplaintext,
stringsignature
) {
// compute sha256 hash of the data
byte[] hash=newSHA256CryptoServiceProvider()
.ComputeHash(Encoding.UTF8.GetBytes(plaintext));
returncsp.VerifyHash(
hash,
CryptoConfig.MapNameToOID('SHA256'),
Convert.FromBase64String(signature));
}
publicstaticvoidrunTests(){
Console.WriteLine('Starting RSA tests');
stringplaintext='this is my secret message';
RSACryptoServiceProviderinitialProvider=newRSACryptoServiceProvider(2048);
stringencrypted=Encrypt(initialProvider, plaintext);
Console.WriteLine('plaintext encrypted to: '+encrypted);
stringdecrypted=Decrypt(initialProvider, encrypted);
Console.WriteLine('plaintext decrypted to: '+decrypted);
stringsignature=Sign(initialProvider, plaintext);
Console.WriteLine('signature: '+signature);
Console.WriteLine('signature verified: '+Verify(initialProvider, plaintext, signature));
// key and signature exported from simple-free-encryption-tool
stringpublicKey=@'-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA26+x8URoo0U5vk0Cs921
kVsqOnHYHs+YRiNuw64QIT/YnikCrEIqo3oEeH3Jt641LuplGiUPdQy3AaqvE2Dw
oiBHve0p1T/zGEMC9xTFdjIyD3eO0e9IROMZ3mAtbBqEYO6apG1yIoCh9lxPwZ0O
6FyKklpj1vZpw+GjnRMTPjoN/5TgEZ6RAZLYrRoKYS5b0GFMe5gLojTg+2PJ0pwk
OfSSIwJ+M4ngGPdIczKO3hxxJxWXOYvEsMuUzhUPPmi6nF+pv+6X10G9RpJpq2fd
nsodcxYx9YWA/2rLlvmLRmpH8plgRe83aHB1RIf5UNXgZWp7rsO7vf58rLFo2CQg
rQIDAQAB
-----END PUBLIC KEY-----';
signature='LKeGxl2O8eXzkiYDbB5hO5NEUzde5ggqJPyLTTO2CtOD8ESnEhzm864mCMbhl0yxDccfN1g6r9DeG39g6O8A8Bx8JvHvuPaq8PNFz0jOU0v3CoNw8LKQUfJYzGRNHTQDRKk1kqIn+8+tJfhAbGceHlneCzMuEz6FoIDkwr8IKSxqkJpvKGmv2LRWGyjtpYNZf71B9EjjeCeyRBRmCcD2CKs/A2+tj4mWTxzs/+U9ik2BSR9fnBq0fcPzOCCgxtI9sYxqR650AnZONVHmozUO0M3dzgSfltrFORsVT4QFm0bbdZqDTsQnulfJPSHY3CIV85qA5OD7M6GM6tdLAAVl8A';
RSACryptoServiceProviderimportedProvider=RSAKeys.ImportPublicKey(publicKey);
Console.WriteLine('imported signature verified: '+Verify(importedProvider, plaintext, signature));
Console.WriteLine('RSA tests completed');
}
}
}
Rsacryptoserviceprovider signdata example c#
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Rsacryptoserviceprovider Verifydata Example

Here are the examples of the csharp api class System.Security.Cryptography.RSA.FromXmlString(string) taken from open source projects. Casper a15he. By voting up you can indicate which examples are. I have this example code for loading CER files (from a genius called Ji Zhou) - I need similar to be able to load PVK files - the problem being that amongst all the 'documentation' for security and encryption a lot of it doesn;t quite work when tested (Ji Jhou's stuff being the exception.).

Rsacryptoserviceprovider Encrypt Example

  • I am using the RSACryptoServiceProvider which comes with C#, the only thing I am really doing myself is how I am packaging and storing the keys as to make it easier to work with. $ endgroup $ – JensB Jun 6 '15 at 10:55.
  • Here are the examples of the csharp api class System.Security.Cryptography.RSA.ToXmlString(bool) taken from open source projects. By voting up you can indicate which examples are.