Rsa Cryptography in c#. i have encrypted an xml file and saved it
Rsa Cryptography in c#. i have encrypted an xml file and saved it .after that i transferred this file to another pc .the main problem is that it co开发者_如何学Gouldn,t decrypt.i dont know why?? when iam decrypting on the same machine the xml file is decrypted..
public static byte[] decryptFile(XmlDocument Doc, RSA rsaKey, string KeyName)
{
Doc = new XmlDocument();
Doc.PreserveWhitespace = true;
Doc.Load(@"MLPACK1.xml");
EncryptedXml encXml = new EncryptedXml(Doc);
encXml.AddKeyNameMapping(KeyName, rsaKey);
encXml.DecryptDocument();
string contentOfDocument = Doc.OuterXml;
byte[] buffer = StrToByteArray(contentOfDocument);
return buffer;
}
You have used ProtectedData.Protect(....) method. Third parameter of the method is scope(enum) type of System.Security.Cryptography.DataProtectionScope.
So seems you have set DataProtectionScope.LocalMachine (or even may be DataProtectionScope.CurrentUser) for DPS and passed it to Protect method.
Like this
DataProtectionScope DPS = DataProtectionScope.LocalMachine
ProtectedData.Protect(bytes, null, DPS);
So this can be the source of the problem.
Is your key the same on both machines? You should take the public key from the machine where this will be decrypted and use this to encrypt it on the other machine.
精彩评论