如何在c#中加密和解密xml [英] How to encryt and decrypt xml in c#

查看:62
本文介绍了如何在c#中加密和解密xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我遇到问题可能会有些帮助。



i写这个代码



XDocument doc = XDocument.Load(任何xml文件的文件路径);

string readxml = doc.ToString();

string swapnil = Encrypt(readxml);

XDocument doc1 = XDocument.Parse(swapnil);

string avi = Decrypt(swapnil);



使用xdocument我加载一个xml文件

然后在readxml我正在读取xdocument作为字符串因为我有加密和解密的方法接受字符串。
在swapnil字符串中的
我已经加密了数据。但是我想再次将字符串转换为xdocument类型。但是当我转换得到这个错误时。



根级别的数据无效。第1行,第1位。



下面是加密和解密的方法





私人只读Rfc2898Derive Bytes _DeriveBytes;

private readonly byte [] _InitVectorBytes;

private readonly byte [] _KeyBytes;

private const string InitVector =T = A4rAzu94ez -dra;

private const int PasswordIterations = 1000; // 2;

私有const字符串SaltValue =d =?ustAF = UstenAr3B @ pRu8 = ner5sW&h59_Xe9P2za-eFr2fa&ePHE @ ras!a + uc @;



public string Decrypt(string encryptedText)

{

byte [] encryptedTextBytes = Convert.FromBase64String(encryptedText);

string plainText;



RijndaelManaged rijndaelManaged = new RijndaelManaged {Mode = CipherMode.CBC};



try

{

使用(ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(_KeyBytes,_InitVectorBytes))

{

using(MemoryStream memoryStream) = new MemoryStream(encryptedTextBytes))

{

使用(CryptoStream cryptoStream = new CryptoStream(memoryStream,decryptor,CryptoStreamMode.Read))

{ <无线电通信/>
// TODO:需要更多地研究这个问题。假设加密文本比普通文本长,但可能有更好的方法

byte [] plainTextBytes = encryptedTextBytes; / * new byte [encryptedTextBytes.Length]; * /



int decryptedByteCount = cryptoStream.Read(plainTextBytes,0,plainTextBytes.Length);

plainText = Encoding.UTF8.GetString(plainTextBytes,0,decryptedByteCount);

}

}

}

}

catch(CryptographicException exception)

{

plainText = string.Empty; //假设错误是由无效的密码引起的

}



返回plainText;

}



公共字符串加密(string plainText)

{

string encryptedText;

byte [] plainTextBytes = Encoding.UTF8.GetBytes(plainText);



RijndaelManaged rijndaelManaged = new RijndaelManaged {Mode = CipherMode.CBC};



使用(ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(_KeyBytes,_InitVectorBytes))

{

using(MemoryStream memoryStream = new MemoryStream())

{

使用(CryptoStream cryptoStream = new CryptoStream(memoryStream,encryptor,CryptoStreamMode.Write))

{

cryptoStream.Write(plainTextBytes,0,plain TextBytes.Length);

cryptoStream.FlushFinalBlock();



byte [] cipherTextBytes = memoryStream.ToArray();

encryptedText = Convert.ToBase64String(cipherTextBytes);

}

}

}



返回encryptedText;

}



使用此方法我想加密和解密。



请帮帮我。

Hi,

I stuck in the problem may be some can help.

i write this code

XDocument doc = XDocument.Load(Filepath of any xml file");
string readxml = doc.ToString();
string swapnil = Encrypt(readxml);
XDocument doc1 = XDocument.Parse(swapnil);
string avi = Decrypt(swapnil);

using xdocument i load a xml file
then in readxml i am reading xdocument as string because i have methods for encrytion and decrytion that accept string.
in swapnil string i have encrypted the data. but again i want to convert the string into xdocument type. but when i am converting getting this error.

Data at the root level is invalid. Line 1, position 1.

below is methods for encrytion and decryption


private readonly Rfc2898DeriveBytes _DeriveBytes;
private readonly byte[] _InitVectorBytes;
private readonly byte[] _KeyBytes;
private const string InitVector = "T=A4rAzu94ez-dra";
private const int PasswordIterations = 1000; //2;
private const string SaltValue = "d=?ustAF=UstenAr3B@pRu8=ner5sW&h59_Xe9P2za-eFr2fa&ePHE@ras!a+uc@";

public string Decrypt(string encryptedText)
{
byte[] encryptedTextBytes = Convert.FromBase64String(encryptedText);
string plainText;

RijndaelManaged rijndaelManaged = new RijndaelManaged { Mode = CipherMode.CBC };

try
{
using (ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(_KeyBytes, _InitVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream(encryptedTextBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
//TODO: Need to look into this more. Assuming encrypted text is longer than plain but there is probably a better way
byte[] plainTextBytes = encryptedTextBytes; /*new byte[encryptedTextBytes.Length];*/

int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
}
catch (CryptographicException exception)
{
plainText = string.Empty; // Assume the error is caused by an invalid password
}

return plainText;
}

public string Encrypt(string plainText)
{
string encryptedText;
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

RijndaelManaged rijndaelManaged = new RijndaelManaged { Mode = CipherMode.CBC };

using (ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(_KeyBytes, _InitVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();

byte[] cipherTextBytes = memoryStream.ToArray();
encryptedText = Convert.ToBase64String(cipherTextBytes);
}
}
}

return encryptedText;
}

using this method i want to encrypt and decrypt.

please help me.

推荐答案

傻错误。将加密/解密放在一边,因为这不是你的问题,你的问题是加密你的字符串,然后尝试将加密的字符串(不再是XML)加载到XDocument中。这会给你相同的结果:

Silly error. Setting the encryption/decryption aside, since that's not your question, your issue is that you encrypt your string, and then try to load that encrypted string (which is no longer XML) into an XDocument. This would give you the same result:
XDocument doc1 = XDocument.Parse("this is nonsense");


根级别的数据无效。第1行,第1位。



当然,我怀疑你的编码字符串是以
Data at the root level is invalid. Line 1, position 1.

Of course, i doubt ur encoded string starts with
<root><element1>

等...



你不能用任何字符串制作Xml。

这里有关于xml的教程



< a href =http://www.w3schools.com/xml/> http://www.w3schools.com/xml/ [ ^ ]

希望有帮助

etc...

You cannot make Xml out of any string.
Here is tutorial about xml

http://www.w3schools.com/xml/[^]
hope it helps


这篇关于如何在c#中加密和解密xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆