简体   繁体   中英

RSA encryption in c#

he very realization, I took with MSDN enter link description here My goal is to save the keys in the file, and then use them for encryption or decryption

Trying to start encrypting

 using System.IO; using System.Security.Cryptography; using System.Text; namespace RSA_Encrypt_Test { internal class Program { private static byte[] PremierKey; private static byte[] SecondKey; private static byte[] dataToEncrypt; private static byte[] encryptedDataNew; private static byte[] encryptedData; private static void Main(string[] args) { //Create a UnicodeEncoder to convert between byte array and string. UnicodeEncoding ByteConverter = new UnicodeEncoding(); //Create byte arrays to hold original, encrypted, and decrypted data. dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt"); using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024)) { PremierKey = RSA.ExportParameters(true).Modulus; SecondKey = RSA.ExportParameters(false).Modulus; using (FileStream fstream = new FileStream(@"C:\\temp\\RSA1.txt", FileMode.OpenOrCreate)) { fstream.Write(PremierKey, 0, PremierKey.Length); } using (FileStream fstream = new FileStream(@"C:\\temp\\RSA2.txt", FileMode.OpenOrCreate)) { fstream.Write(SecondKey, 0, SecondKey.Length); } encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false); } } public static byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) { byte[] encryptedData; using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) { RSA.ImportParameters(RSAKeyInfo); encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding); using (FileStream fstream = File.OpenRead(@"C:\\temp\\RSA1.txt")) { PremierKey = new byte[fstream.Length]; fstream.Read(PremierKey, 0, PremierKey.Length); using (RSACryptoServiceProvider RSA1 = new RSACryptoServiceProvider()) { RSAParameters RSAKeyInfo1 = new RSAParameters(); RSAKeyInfo1.Modulus = SecondKey; RSAKeyInfo1.Exponent = new byte[] {1, 0, 1}; encryptedDataNew = RSA1.Encrypt(dataToEncrypt, false); } } } return encryptedData; } } } 

Here, in a method RSAEncrypt I use two methods of encryption. The first way I pass the data on the key within the application, the second way I load the key from RSA1.txt file. The keys are identical everywhere, esponenta too. But after encryption are obtained different arrays of bytes. Please indicate what went wrong.

PS specifically using an example from MSDN, and not other versions of the Internet, so as not to produce errors, and with them undocumented vulnerability. So please do not offer other projects to use.

You have to serialize the whole RSAParameters object, not only the Modulus part.

        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024))
        {
            var serializer = new XmlSerializer(typeof (RSAParameters));
            var privateAndPublicKey = RSA.ExportParameters(true);
            var publicKeyOnly = RSA.ExportParameters(false);

            // Saving keys
            using (var fileStream = File.OpenWrite(@"C:\temp\PrivateAndPublic.xml"))
                serializer.Serialize(fileStream, privateAndPublicKey);
            using (var fileStream = File.OpenWrite(@"C:\temp\publicKeyOnly.xml"))
                serializer.Serialize(fileStream, publicKeyOnly);

            // Restoring keys
            using (var fileStream = File.OpenRead(@"C:\temp\PrivateAndPublic.xml"))
                privateAndPublicKey = (RSAParameters)serializer.Deserialize(fileStream);
            using (var fileStream = File.OpenRead(@"C:\temp\publicKeyOnly.xml"))
                publicKeyOnly = (RSAParameters) serializer.Deserialize(fileStream);

            RSA.ImportParameters(privateAndPublicKey);
            RSA.ImportParameters(publicKeyOnly );
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM