The rsa encryption algorithm is Shared using examples

  • 2020-06-01 10:54:37
  • OfStack

 Generates the private and public keys 
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
// Get the private key saved mainly RSAParameters In the 8 The parameters 
privateKey = myrsa.ToXmlString(true); 
// I get the public key and I save it RSAParameters In the 2 A parameter 
publicKey = myrsa.ToXmlString(false); 
RAS Realize the encryption 
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
// To get the public key 
// Convert the content you want to encrypt to byte[]
byte[] PlainTextBArray = (new UnicodeEncoding()).GetBytes(" Here's what you want to encrypt ");
// use .NET In the Encrypt Method of encryption 
byte[] CypherTextBArray = myrsa.Encrypt(PlainTextBArray, false);
// Finally, encrypt it byte[] Converted to Base64String , this is the encrypted content 
Result = Convert.ToBase64String(CypherTextBArray)

RAS Implement decryption 
System.Security.Cryptography.RSACryptoServiceProvider myrsa = new RSACryptoServiceProvider();
// Get the private key 
// I'm going to encrypt it String Converted to byte[]
byte[] PlainTextBArray = Convert.FromBase64String(" I just encrypted it string");
// use .NET In the Decrypt Methods the decryption 
byte[] DypherTextBArray = myrsa.Decrypt(PlainTextBArray, false);
// Convert after decryption byte[] , which brings us to our original pre-encrypted content 
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);

byte[] messagebytes = Encoding.UTF8.GetBytes("luo ROM. ");
            RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
            string privatekey = oRSA.ToXmlString(true);
            string publickey = oRSA.ToXmlString(false);
            // The private key signature   
            RSACryptoServiceProvider oRSA3 = new RSACryptoServiceProvider();
            byte[] AOutput = oRSA3.SignData(messagebytes, "SHA1");
            // A public key to verify   
            RSACryptoServiceProvider oRSA4 = new RSACryptoServiceProvider();
            bool bVerify = oRSA4.VerifyData(messagebytes, "SHA1", AOutput); 

Related articles: