C# - 从流序列化/反序列化 DES 加密文件

发布于 2024-07-23 05:44:11 字数 177 浏览 7 评论 0原文

有谁有关于如何将序列化数据加密到文件然后使用 DES 将其读回的示例吗?

我已经编写了一些不起作用的代码,但我宁愿看到新的尝试,而不是继续我的代码。

编辑:抱歉,忘了提及我需要一个使用 XmlSerializer.Serialize/Deserialize 的示例。

Does anyone have any examples of how to encrypt serialized data to a file and then read it back using DES?

I've written some code already that isn't working, but I'd rather see a fresh attempt instead of pursuing my code.

EDIT: Sorry, forgot to mention I need an example using XmlSerializer.Serialize/Deserialize.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天涯离梦残月幽梦 2024-07-30 05:44:12

加密

public static void EncryptAndSerialize(string filename, MyObject obj, SymmetricAlgorithm key)
{
    using(FileStream fs = File.Open(filename, FileMode.Create))
    {
        using(CryptoStream cs = new CryptoStream(fs, key.CreateEncryptor(), CryptoStreamMode.Write))
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(MyObject));
            xmlser.Serialize(cs, obj); 
        }
    }
}

解密:

public static MyObject DecryptAndDeserialize(string filename, SymmetricAlgorithm key)    
{
    using(FileStream fs = File.Open(filename, FileMode.Open))
    {
        using(CryptoStream cs = new CryptoStream(fs, key.CreateDecryptor(), CryptoStreamMode.Read))
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(MyObject));
            return (MyObject) xmlser.Deserialize(cs);
        }
    }
}

用法:

DESCryptoServiceProvider key = new DESCryptoServiceProvider();
MyObject obj = new MyObject();
EncryptAndSerialize("testfile.xml", obj, key);
MyObject deobj = DecryptAndDeserialize("testfile.xml", key);

您需要将 MyObject 更改为您要序列化的对象的类型,但这是总体思路。 技巧是使用相同的 SymmetricAlgorithm 实例来加密和解密。

Encryption

public static void EncryptAndSerialize(string filename, MyObject obj, SymmetricAlgorithm key)
{
    using(FileStream fs = File.Open(filename, FileMode.Create))
    {
        using(CryptoStream cs = new CryptoStream(fs, key.CreateEncryptor(), CryptoStreamMode.Write))
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(MyObject));
            xmlser.Serialize(cs, obj); 
        }
    }
}

Decryption:

public static MyObject DecryptAndDeserialize(string filename, SymmetricAlgorithm key)    
{
    using(FileStream fs = File.Open(filename, FileMode.Open))
    {
        using(CryptoStream cs = new CryptoStream(fs, key.CreateDecryptor(), CryptoStreamMode.Read))
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(MyObject));
            return (MyObject) xmlser.Deserialize(cs);
        }
    }
}

Usage:

DESCryptoServiceProvider key = new DESCryptoServiceProvider();
MyObject obj = new MyObject();
EncryptAndSerialize("testfile.xml", obj, key);
MyObject deobj = DecryptAndDeserialize("testfile.xml", key);

You need to change MyObject to whatever the type of your object is that you are serializing, but this is the general idea. The trick is to use the same SymmetricAlgorithm instance to encrypt and decrypt.

鹿港巷口少年归 2024-07-30 05:44:12

该线程给出了基本想法。 这是一个使函数变得通用的版本,并且还允许您传递加密密钥,因此它是可逆的。

public static void EncryptAndSerialize<T>(string filename, T obj, string encryptionKey) {
  var key = new DESCryptoServiceProvider();
  var e = key.CreateEncryptor(Encoding.ASCII.GetBytes("64bitPas"), Encoding.ASCII.GetBytes(encryptionKey));
  using (var fs = File.Open(filename, FileMode.Create))
  using (var cs = new CryptoStream(fs, e, CryptoStreamMode.Write))
      (new XmlSerializer(typeof (T))).Serialize(cs, obj);
}

public static T DecryptAndDeserialize<T>(string filename, string encryptionKey) {
  var key = new DESCryptoServiceProvider();
  var d = key.CreateDecryptor(Encoding.ASCII.GetBytes("64bitPas"), Encoding.ASCII.GetBytes(encryptionKey));
  using (var fs = File.Open(filename, FileMode.Open))
  using (var cs = new CryptoStream(fs, d, CryptoStreamMode.Read))
      return (T) (new XmlSerializer(typeof (T))).Deserialize(cs);
}

This thread gave the basic idea. Here's a version that makes the functions generic, and also allows you to pass an encryption key so it's reversible.

public static void EncryptAndSerialize<T>(string filename, T obj, string encryptionKey) {
  var key = new DESCryptoServiceProvider();
  var e = key.CreateEncryptor(Encoding.ASCII.GetBytes("64bitPas"), Encoding.ASCII.GetBytes(encryptionKey));
  using (var fs = File.Open(filename, FileMode.Create))
  using (var cs = new CryptoStream(fs, e, CryptoStreamMode.Write))
      (new XmlSerializer(typeof (T))).Serialize(cs, obj);
}

public static T DecryptAndDeserialize<T>(string filename, string encryptionKey) {
  var key = new DESCryptoServiceProvider();
  var d = key.CreateDecryptor(Encoding.ASCII.GetBytes("64bitPas"), Encoding.ASCII.GetBytes(encryptionKey));
  using (var fs = File.Open(filename, FileMode.Open))
  using (var cs = new CryptoStream(fs, d, CryptoStreamMode.Read))
      return (T) (new XmlSerializer(typeof (T))).Deserialize(cs);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文