标签:des style blog http io color ar os sp
原文:C#实现RSA加密和解密详解RSA加密解密源码:
 using System;
using System;  using System.Collections.Generic;
using System.Collections.Generic;  using System.Linq;
using System.Linq;  using System.Text;
using System.Text;  using System.Security.Cryptography;
using System.Security.Cryptography;  
  namespace MyRSA
namespace MyRSA namespace MyRSA {
{ publicclass MyRSA
publicclass MyRSA {
{
 privatestaticstring publicKey =
privatestaticstring publicKey =  "<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+
    "<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+ "TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+
    "TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+  "wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+
    "wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+ "PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+
    "PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+ "/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+
    "/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+ "w9YRXiac=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
    "w9YRXiac=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; privatestaticstring privateKey =
privatestaticstring privateKey =  "<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+
    "<RSAKeyValue><Modulus>6CdsXgYOyya/yQH"+ "TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+
    "TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L"+ "wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+
    "wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx"+ "PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+
    "PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE"+ "/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+
    "/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP"+ "w9YRXiac=</Modulus><Exponent>AQAB</Exponent>"+
    "w9YRXiac=</Modulus><Exponent>AQAB</Exponent>"+ "<P>/aoce2r6tonjzt1IQI6FM4ysR40j/gKvt4d"+
    "<P>/aoce2r6tonjzt1IQI6FM4ysR40j/gKvt4d"+ "L411pUop1Zg61KvCm990M4uN6K8R/DUvAQdrRd"+
    "L411pUop1Zg61KvCm990M4uN6K8R/DUvAQdrRd"+ "VgzvvAxXD7ESw==</P><Q>6kqclrEunX/fmOle"+
    "VgzvvAxXD7ESw==</P><Q>6kqclrEunX/fmOle"+ "VTxG4oEpXY4IJumXkLpylNR3vhlXf6ZF9obEpG"+
    "VTxG4oEpXY4IJumXkLpylNR3vhlXf6ZF9obEpG"+ "lq0N7sX2HBxa7T2a0WznOAb0si8FuelQ==</Q>"+
    "lq0N7sX2HBxa7T2a0WznOAb0si8FuelQ==</Q>"+ "<DP>3XEvxB40GD5v/Rr4BENmzQW1MBFqpki6FU"+
    "<DP>3XEvxB40GD5v/Rr4BENmzQW1MBFqpki6FU"+ "GrYiUd2My+iAW26nGDkUYMBdYHxUWYlIbYo6Te"+
    "GrYiUd2My+iAW26nGDkUYMBdYHxUWYlIbYo6Te"+ "zc3d/oW40YqJ2Q==</DP><DQ>LK0XmQCmY/ArY"+
    "zc3d/oW40YqJ2Q==</DP><DQ>LK0XmQCmY/ArY"+ "gw2Kci5t51rluRrl4f5l+aFzO2K+9v3PGcndjA"+
    "gw2Kci5t51rluRrl4f5l+aFzO2K+9v3PGcndjA"+ "StUtIzBWGO1X3zktdKGgCLlIGDrLkMbM21Q==</DQ><InverseQ>"+
    "StUtIzBWGO1X3zktdKGgCLlIGDrLkMbM21Q==</DQ><InverseQ>"+ "GqC4Wwsk2fdvJ9dmgYlej8mTDBWg0Wm6aqb5kjn"+
    "GqC4Wwsk2fdvJ9dmgYlej8mTDBWg0Wm6aqb5kjn"+ "cWK6WUa6CfD+XxfewIIq26+4Etm2A8IAtRdwPl4"+
    "cWK6WUa6CfD+XxfewIIq26+4Etm2A8IAtRdwPl4"+ "aPjSfWdA==</InverseQ><D>a1qfsDMY8DSxB2D"+
    "aPjSfWdA==</InverseQ><D>a1qfsDMY8DSxB2D"+ "Cr7LX5rZHaZaqDXdO3GC01z8dHjI4dDVwOS5ZFZ"+
    "Cr7LX5rZHaZaqDXdO3GC01z8dHjI4dDVwOS5ZFZ"+ "s7MCN3yViPsoRLccnVWcLzOkSQF4lgKfTq3IH40"+
    "s7MCN3yViPsoRLccnVWcLzOkSQF4lgKfTq3IH40"+ "H5N4gg41as9GbD0g9FC3n5IT4VlVxn9ZdW+WQry"+
    "H5N4gg41as9GbD0g9FC3n5IT4VlVxn9ZdW+WQry"+ "oHdbiIAiNpFKxL/DIEERur4sE1Jt9VdZsH24CJE=</D></RSAKeyValue>";
    "oHdbiIAiNpFKxL/DIEERur4sE1Jt9VdZsH24CJE=</D></RSAKeyValue>";
 staticpublicstring Decrypt(string base64code)
staticpublicstring Decrypt(string base64code) {
{ try
    try {
    {
 //Create a UnicodeEncoder to convert between byte array and string.
        //Create a UnicodeEncoder to convert between byte array and string. UnicodeEncoding ByteConverter =new UnicodeEncoding();
        UnicodeEncoding ByteConverter =new UnicodeEncoding();
 //Create a new instance of RSACryptoServiceProvider to generate
        //Create a new instance of RSACryptoServiceProvider to generate //public and private key data.
        //public and private key data. RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
        RSACryptoServiceProvider RSA =new RSACryptoServiceProvider(); RSA.FromXmlString(privateKey);
        RSA.FromXmlString(privateKey);
 byte[] encryptedData;
        byte[] encryptedData; byte[] decryptedData;
        byte[] decryptedData; encryptedData = Convert.FromBase64String(base64code);
        encryptedData = Convert.FromBase64String(base64code);
 //Pass the data to DECRYPT, the private key information
        //Pass the data to DECRYPT, the private key information  //(using RSACryptoServiceProvider.ExportParameters(true),
        //(using RSACryptoServiceProvider.ExportParameters(true), //and a boolean flag specifying no OAEP padding.
        //and a boolean flag specifying no OAEP padding. decryptedData = RSADecrypt(
        decryptedData = RSADecrypt( encryptedData, RSA.ExportParameters(true), false);
            encryptedData, RSA.ExportParameters(true), false);
 //Display the decrypted plaintext to the console.
        //Display the decrypted plaintext to the console.  return ByteConverter.GetString(decryptedData);
        return ByteConverter.GetString(decryptedData); }
    } catch (Exception exc)
    catch (Exception exc) {
    { //Exceptions.LogException(exc);
        //Exceptions.LogException(exc); Console.WriteLine(exc.Message);
        Console.WriteLine(exc.Message); return"";
        return""; }
    } }
}
 staticpublicstring Encrypt(string toEncryptString)
staticpublicstring Encrypt(string toEncryptString) {
{ try
    try {
    { //Create a UnicodeEncoder to convert between byte array and string.
        //Create a UnicodeEncoder to convert between byte array and string. UnicodeEncoding ByteConverter =new UnicodeEncoding();
        UnicodeEncoding ByteConverter =new UnicodeEncoding();
 //Create byte arrays to hold original, encrypted, and decrypted data.
        //Create byte arrays to hold original, encrypted, and decrypted data. byte[] dataToEncrypt =
        byte[] dataToEncrypt =  ByteConverter.GetBytes(toEncryptString);
            ByteConverter.GetBytes(toEncryptString); byte[] encryptedData;
        byte[] encryptedData; byte[] decryptedData;
        byte[] decryptedData;
 //Create a new instance of RSACryptoServiceProvider to generate
        //Create a new instance of RSACryptoServiceProvider to generate //public and private key data.
        //public and private key data. RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
        RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
 RSA.FromXmlString(privateKey);
        RSA.FromXmlString(privateKey);
 //Pass the data to ENCRYPT, the public key information
        //Pass the data to ENCRYPT, the public key information  //(using RSACryptoServiceProvider.ExportParameters(false),
        //(using RSACryptoServiceProvider.ExportParameters(false), //and a boolean flag specifying no OAEP padding.
        //and a boolean flag specifying no OAEP padding. encryptedData = RSAEncrypt(
        encryptedData = RSAEncrypt( dataToEncrypt, RSA.ExportParameters(false), false);
            dataToEncrypt, RSA.ExportParameters(false), false);
 string base64code = Convert.ToBase64String(encryptedData);
        string base64code = Convert.ToBase64String(encryptedData); return base64code;
        return base64code; }
    } catch (Exception exc)
    catch (Exception exc) {
    { //Catch this exception in case the encryption did
        //Catch this exception in case the encryption did //not succeed.
        //not succeed. //Exceptions.LogException(exc);
        //Exceptions.LogException(exc); Console.WriteLine(exc.Message);
        Console.WriteLine(exc.Message); return"";
        return""; }
    }


 }
}
 staticprivatebyte[] RSAEncrypt(
staticprivatebyte[] RSAEncrypt( byte[] DataToEncrypt,
    byte[] DataToEncrypt,  RSAParameters RSAKeyInfo,
    RSAParameters RSAKeyInfo,  bool DoOAEPPadding)
    bool DoOAEPPadding) {
{ try
    try {
    { //Create a new instance of RSACryptoServiceProvider.
        //Create a new instance of RSACryptoServiceProvider. RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
        RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
 //Import the RSA Key information. This only needs
        //Import the RSA Key information. This only needs //toinclude the public key information.
        //toinclude the public key information. RSA.ImportParameters(RSAKeyInfo);
        RSA.ImportParameters(RSAKeyInfo);
 //Encrypt the passed byte array and specify OAEP padding.
        //Encrypt the passed byte array and specify OAEP padding.   //OAEP padding is only available on Microsoft Windows XP or
        //OAEP padding is only available on Microsoft Windows XP or //later.
        //later.   return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
        return RSA.Encrypt(DataToEncrypt, DoOAEPPadding); }
    } //Catch and display a CryptographicException
    //Catch and display a CryptographicException   //to the console.
    //to the console. catch (CryptographicException e)
    catch (CryptographicException e) {
    { //Exceptions.LogException(e);
        //Exceptions.LogException(e); Console.WriteLine(e.Message);
        Console.WriteLine(e.Message);
 returnnull;
        returnnull; }
    }
 }
}
 staticprivatebyte[] RSADecrypt(
staticprivatebyte[] RSADecrypt( byte[] DataToDecrypt,
    byte[] DataToDecrypt, RSAParameters RSAKeyInfo,
    RSAParameters RSAKeyInfo, bool DoOAEPPadding)
    bool DoOAEPPadding) {
{ try
    try {
    { //Create a new instance of RSACryptoServiceProvider.
        //Create a new instance of RSACryptoServiceProvider. RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
        RSACryptoServiceProvider RSA =new RSACryptoServiceProvider();
 //Import the RSA Key information. This needs
        //Import the RSA Key information. This needs //to include the private key information.
        //to include the private key information. RSA.ImportParameters(RSAKeyInfo);
        RSA.ImportParameters(RSAKeyInfo);
 //Decrypt the passed byte array and specify OAEP padding.
        //Decrypt the passed byte array and specify OAEP padding.   //OAEP padding is only available on Microsoft Windows XP or
        //OAEP padding is only available on Microsoft Windows XP or //later.
        //later.   return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
        return RSA.Decrypt(DataToDecrypt, DoOAEPPadding); }
    } //Catch and display a CryptographicException
    //Catch and display a CryptographicException   //to the console.
    //to the console. catch (CryptographicException e)
    catch (CryptographicException e) {
    { //Exceptions.LogException(e);
        //Exceptions.LogException(e); Console.WriteLine(e.Message);
        Console.WriteLine(e.Message);
 returnnull;
        returnnull; }
    } }
} }
} }
}
 测试代码:
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/lonelyxmas/p/4098069.html