我正在尝试创建许可证文件,我需要加密。
我有License
个对象和List<License>licenses
。我需要在将流保存到xml文件之前加密流,以便无法轻松读取。
我找到了这篇文章:MSDN Code: Writing Class Data to an XML File (Visual C#)
public class Book
{
public string title;
static void Main()
{
Book introToVCS = new Book();
introToVCS.title = "Intro to Visual CSharp";
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(introToVCS.GetType());
System.IO.StreamWriter file =
new System.IO.StreamWriter("c:\\IntroToVCS.xml");
writer.Serialize(file, introToVCS);
file.Close();
}
}
这篇文章:CodeProject: Using CryptoStream in C#
编写xml文件:
FileStream stream = new FileStream(�C:\\test.txt�,
FileMode.OpenOrCreate,FileAccess.Write);
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
cryptic.IV = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
CryptoStream crStream = new CryptoStream(stream,
cryptic.CreateEncryptor(),CryptoStreamMode.Write);
byte[] data = ASCIIEncoding.ASCII.GetBytes(�Hello World!�);
crStream.Write(data,0,data.Length);
crStream.Close();
stream.Close();
读取xml文件:
FileStream stream = new FileStream(�C:\\test.txt�,
FileMode.Open,FileAccess.Read);
DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
cryptic.Key = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
cryptic.IV = ASCIIEncoding.ASCII.GetBytes(�ABCDEFGH�);
CryptoStream crStream = new CryptoStream(stream,
cryptic.CreateDecryptor(),CryptoStreamMode.Read);
StreamReader reader = new StreamReader(crStream);
string data = reader.ReadToEnd();
reader.Close();
stream.Close();
我很难将这两者结合起来。任何人都可以帮助我吗?
答案 0 :(得分:3)
实际上,您应该考虑使用EncryptedXml类。您可以加密XML内容,而不是加密XML本身。
加密可能需要不同的加密强度,密钥库等方法。请遵循MSDN文档中的示例。这不是一个简短的实现,但它运作良好。