读书人

C#兑现RSA加密和解密详解

发布时间: 2013-04-05 10:24:33 作者: rapoo

C#实现RSA加密和解密详解

RSA加密解密源码:

C#兑现RSA加密和解密详解
public class MyRSAC#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解private static string publicKey = C#兑现RSA加密和解密详解"<RSAKeyValue><Modulus>6CdsXgYOyya/yQH" +C#兑现RSA加密和解密详解"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L" + C#兑现RSA加密和解密详解"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx" +C#兑现RSA加密和解密详解"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE" +C#兑现RSA加密和解密详解"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP" +C#兑现RSA加密和解密详解"w9YRXiac=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";C#兑现RSA加密和解密详解private static string privateKey = C#兑现RSA加密和解密详解"<RSAKeyValue><Modulus>6CdsXgYOyya/yQH" +C#兑现RSA加密和解密详解"TO96dB3gEurM2UQDDVGrZoe6RcAVTxAqDDf5L" +C#兑现RSA加密和解密详解"wPycZwtNOx3Cfy44/D5Mj86koPew5soFIz9sx" +C#兑现RSA加密和解密详解"PAHRF5hcqJoG+q+UfUYTHYCsMH2cnqGVtnQiE" +C#兑现RSA加密和解密详解"/PMRMmY0RwEfMIo+TDpq3QyO03MaEsDGf13sP" +C#兑现RSA加密和解密详解"w9YRXiac=</Modulus><Exponent>AQAB</Exponent>" +C#兑现RSA加密和解密详解"<P>/aoce2r6tonjzt1IQI6FM4ysR40j/gKvt4d" +C#兑现RSA加密和解密详解"L411pUop1Zg61KvCm990M4uN6K8R/DUvAQdrRd" +C#兑现RSA加密和解密详解"VgzvvAxXD7ESw==</P><Q>6kqclrEunX/fmOle" +C#兑现RSA加密和解密详解"VTxG4oEpXY4IJumXkLpylNR3vhlXf6ZF9obEpG" +C#兑现RSA加密和解密详解"lq0N7sX2HBxa7T2a0WznOAb0si8FuelQ==</Q>" +C#兑现RSA加密和解密详解"<DP>3XEvxB40GD5v/Rr4BENmzQW1MBFqpki6FU" +C#兑现RSA加密和解密详解"GrYiUd2My+iAW26nGDkUYMBdYHxUWYlIbYo6Te" +C#兑现RSA加密和解密详解"zc3d/oW40YqJ2Q==</DP><DQ>LK0XmQCmY/ArY" +C#兑现RSA加密和解密详解"gw2Kci5t51rluRrl4f5l+aFzO2K+9v3PGcndjA" +C#兑现RSA加密和解密详解"StUtIzBWGO1X3zktdKGgCLlIGDrLkMbM21Q==</DQ><InverseQ>" +C#兑现RSA加密和解密详解"GqC4Wwsk2fdvJ9dmgYlej8mTDBWg0Wm6aqb5kjn" +C#兑现RSA加密和解密详解"cWK6WUa6CfD+XxfewIIq26+4Etm2A8IAtRdwPl4" +C#兑现RSA加密和解密详解"aPjSfWdA==</InverseQ><D>a1qfsDMY8DSxB2D" +C#兑现RSA加密和解密详解"Cr7LX5rZHaZaqDXdO3GC01z8dHjI4dDVwOS5ZFZ" +C#兑现RSA加密和解密详解"s7MCN3yViPsoRLccnVWcLzOkSQF4lgKfTq3IH40" +C#兑现RSA加密和解密详解"H5N4gg41as9GbD0g9FC3n5IT4VlVxn9ZdW+WQry" +C#兑现RSA加密和解密详解"oHdbiIAiNpFKxL/DIEERur4sE1Jt9VdZsH24CJE=</D></RSAKeyValue>";C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解static public string Decrypt(string base64code)C#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解tryC#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Create a UnicodeEncoder to convert between byte array and string.C#兑现RSA加密和解密详解        UnicodeEncoding ByteConverter = new UnicodeEncoding();C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Create a new instance of RSACryptoServiceProvider to generateC#兑现RSA加密和解密详解//public and private key data.C#兑现RSA加密和解密详解        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();C#兑现RSA加密和解密详解        RSA.FromXmlString(privateKey);C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解byte[] encryptedData;C#兑现RSA加密和解密详解byte[] decryptedData;C#兑现RSA加密和解密详解        encryptedData = Convert.FromBase64String(base64code);C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Pass the data to DECRYPT, the private key information C#兑现RSA加密和解密详解//(using RSACryptoServiceProvider.ExportParameters(true),C#兑现RSA加密和解密详解//and a boolean flag specifying no OAEP padding.C#兑现RSA加密和解密详解        decryptedData = RSADecrypt(C#兑现RSA加密和解密详解            encryptedData, RSA.ExportParameters(true), false);C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Display the decrypted plaintext to the console. C#兑现RSA加密和解密详解        return ByteConverter.GetString(decryptedData);C#兑现RSA加密和解密详解    }C#兑现RSA加密和解密详解catch (Exception exc)C#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解//Exceptions.LogException(exc);C#兑现RSA加密和解密详解        Console.WriteLine(exc.Message);C#兑现RSA加密和解密详解return "";C#兑现RSA加密和解密详解    }C#兑现RSA加密和解密详解}C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解static public string Encrypt(string toEncryptString)C#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解tryC#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解//Create a UnicodeEncoder to convert between byte array and string.C#兑现RSA加密和解密详解        UnicodeEncoding ByteConverter = new UnicodeEncoding();C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Create byte arrays to hold original, encrypted, and decrypted data.C#兑现RSA加密和解密详解        byte[] dataToEncrypt = C#兑现RSA加密和解密详解            ByteConverter.GetBytes(toEncryptString);C#兑现RSA加密和解密详解byte[] encryptedData;C#兑现RSA加密和解密详解byte[] decryptedData;C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Create a new instance of RSACryptoServiceProvider to generateC#兑现RSA加密和解密详解//public and private key data.C#兑现RSA加密和解密详解        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解        RSA.FromXmlString(privateKey);C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Pass the data to ENCRYPT, the public key information C#兑现RSA加密和解密详解//(using RSACryptoServiceProvider.ExportParameters(false),C#兑现RSA加密和解密详解//and a boolean flag specifying no OAEP padding.C#兑现RSA加密和解密详解        encryptedData = RSAEncrypt(C#兑现RSA加密和解密详解            dataToEncrypt, RSA.ExportParameters(false), false);C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解string base64code = Convert.ToBase64String(encryptedData);C#兑现RSA加密和解密详解return base64code;C#兑现RSA加密和解密详解    }C#兑现RSA加密和解密详解catch (Exception exc)C#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解//Catch this exception in case the encryption didC#兑现RSA加密和解密详解//not succeed.C#兑现RSA加密和解密详解//Exceptions.LogException(exc);C#兑现RSA加密和解密详解        Console.WriteLine(exc.Message);C#兑现RSA加密和解密详解return "";C#兑现RSA加密和解密详解    }C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解}C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解static private byte[] RSAEncrypt(C#兑现RSA加密和解密详解byte[] DataToEncrypt, C#兑现RSA加密和解密详解    RSAParameters RSAKeyInfo, C#兑现RSA加密和解密详解bool DoOAEPPadding)C#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解tryC#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解//Create a new instance of RSACryptoServiceProvider.C#兑现RSA加密和解密详解        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Import the RSA Key information. This only needsC#兑现RSA加密和解密详解//toinclude the public key information.C#兑现RSA加密和解密详解        RSA.ImportParameters(RSAKeyInfo);C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Encrypt the passed byte array and specify OAEP padding.  C#兑现RSA加密和解密详解//OAEP padding is only available on Microsoft Windows XP orC#兑现RSA加密和解密详解//later.  C#兑现RSA加密和解密详解        return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);C#兑现RSA加密和解密详解    }C#兑现RSA加密和解密详解//Catch and display a CryptographicException  C#兑现RSA加密和解密详解//to the console.C#兑现RSA加密和解密详解    catch (CryptographicException e)C#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解//Exceptions.LogException(e);C#兑现RSA加密和解密详解        Console.WriteLine(e.Message);C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解return null;C#兑现RSA加密和解密详解    }C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解}C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解static private byte[] RSADecrypt(C#兑现RSA加密和解密详解byte[] DataToDecrypt,C#兑现RSA加密和解密详解    RSAParameters RSAKeyInfo,C#兑现RSA加密和解密详解bool DoOAEPPadding)C#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解tryC#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解//Create a new instance of RSACryptoServiceProvider.C#兑现RSA加密和解密详解        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Import the RSA Key information. This needsC#兑现RSA加密和解密详解//to include the private key information.C#兑现RSA加密和解密详解        RSA.ImportParameters(RSAKeyInfo);C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解//Decrypt the passed byte array and specify OAEP padding.  C#兑现RSA加密和解密详解//OAEP padding is only available on Microsoft Windows XP orC#兑现RSA加密和解密详解//later.  C#兑现RSA加密和解密详解        return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);C#兑现RSA加密和解密详解    }C#兑现RSA加密和解密详解//Catch and display a CryptographicException  C#兑现RSA加密和解密详解//to the console.C#兑现RSA加密和解密详解    catch (CryptographicException e)C#兑现RSA加密和解密详解{C#兑现RSA加密和解密详解//Exceptions.LogException(e);C#兑现RSA加密和解密详解        Console.WriteLine(e.Message);C#兑现RSA加密和解密详解C#兑现RSA加密和解密详解return null;C#兑现RSA加密和解密详解    }C#兑现RSA加密和解密详解}C#兑现RSA加密和解密详解}C#兑现RSA加密和解密详解}


?测试代码:

????????static?void?Main(string[]?args)
????????{
????????????string?encodeString?=?MyRSA.Encrypt("1234567");
????????????Console.WriteLine(encodeString);


????????????string?decode?=?MyRSA.Decrypt(encodeString);
????????????Console.WriteLine(decode);

????????????Console.ReadLine();
????????}

?

?

转自:http://www.csharpwin.com/csharpspace/8761r456.shtml

读书人网 >C#

热点推荐