简体   繁体   English

C#中的RSA加密

[英]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 他非常意识到,我在这里用MSDN 输入了链接描述。我的目标是将密钥保存在文件中,然后将其用于加密或解密

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. 在这里,在RSAEncrypt方法中,我使用两种加密方法。 The first way I pass the data on the key within the application, the second way I load the key from RSA1.txt file. 第一种方法是将数据传递给应用程序中的密钥,第二种方法是从RSA1.txt文件加载密钥。 The keys are identical everywhere, esponenta too. 各个地方的键都一样,esponenta也一样。 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. PS特别使用了来自MSDN的示例,而不是Internet的其他版本,以免产生错误,并带有未记录的漏洞。 So please do not offer other projects to use. 因此,请不要提供其他要使用的项目。

You have to serialize the whole RSAParameters object, not only the Modulus part. 您必须序列化整个RSAParameters对象,而不仅仅是序列部分。

        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 );
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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