如何将类对象保存到文件,然后使用打开和关闭密钥对其进行加密

发布于 2024-08-23 18:26:07 字数 1641 浏览 5 评论 0原文

如何将类对象保存到文件,然后使用打开和关闭密钥对其进行加密。

封闭密钥必须是所有人通用的(只是为了安全),

并且每个文件的开放密钥必须不同。

我想使用此代码...看起来这就是我想要的,但我仍然需要加密

    public ObjectToFile(_Object : object, _FileName : string) : bool
{
    try
    {
        // create new memory stream
        mutable _MemoryStream : System.IO.MemoryStream = System.IO.MemoryStream();

        // create new BinaryFormatter
        def _BinaryFormatter : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
                    = System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

        // Serializes an object, or graph of connected objects, to the given stream.
        _BinaryFormatter.Serialize(_MemoryStream, _Object);

        // convert stream to byte array
        mutable _ByteArray : array[byte] = _MemoryStream.ToArray();

        // Open file for writing
        def _FileStream : System.IO.FileStream = 
        System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);

        // Writes a block of bytes to this stream using data from a byte array.
        _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

        // close file stream
        _FileStream.Close();

        // cleanup
        _MemoryStream.Close();
        _MemoryStream.Dispose();
        _MemoryStream = null;
        _ByteArray = null;

         true
    }
    catch
    {
        | e is Exception => // Error
        {
            Console.WriteLine("Exception caught in process: {0}", e.ToString());
            false
        }
    }
}

致版主:请不要删除 C# 标签,因为我可以获得 C# 答案,但获得 nemerle 答案的机会很小:)

How to save class object to file and then encrypt it with open and closed key.

closed key must be one for all (just for safe)

and open key must be different for each file.

I want use this code ... Looking like it is what I want but I still need Encryption.

    public ObjectToFile(_Object : object, _FileName : string) : bool
{
    try
    {
        // create new memory stream
        mutable _MemoryStream : System.IO.MemoryStream = System.IO.MemoryStream();

        // create new BinaryFormatter
        def _BinaryFormatter : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
                    = System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

        // Serializes an object, or graph of connected objects, to the given stream.
        _BinaryFormatter.Serialize(_MemoryStream, _Object);

        // convert stream to byte array
        mutable _ByteArray : array[byte] = _MemoryStream.ToArray();

        // Open file for writing
        def _FileStream : System.IO.FileStream = 
        System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);

        // Writes a block of bytes to this stream using data from a byte array.
        _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

        // close file stream
        _FileStream.Close();

        // cleanup
        _MemoryStream.Close();
        _MemoryStream.Dispose();
        _MemoryStream = null;
        _ByteArray = null;

         true
    }
    catch
    {
        | e is Exception => // Error
        {
            Console.WriteLine("Exception caught in process: {0}", e.ToString());
            false
        }
    }
}

to moderators : please don't remove C# tag, because I'm able to get C# answer and there is low chances to get nemerle answer :)

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

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

发布评论

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

评论(1

似最初 2024-08-30 18:26:07

.Net 中没有为此的标准库(至少据我所知没有)。但您可以遵循以下原则:

您有两把钥匙。对称 (AES) 还是非对称(公钥和私钥)。钥匙 1 关闭,钥匙 2 打开。

对于您创建的每个文件第三个密钥,该密钥通常是对称 (AES) 密钥。您使用密钥 1 和密钥 2 对该密钥进行加密,并将这 2 个加密的结果存储在文件的标头中。使用密钥 3 加密您的数据。

要读取,您可以选择密钥 1 或密钥 2 来解密读取文件内容所需的密钥(密钥 3)。

System.Security.Cryptography 命名空间包含您的所有内容需要。

There is no standard library in .Net for this (at least none that I know of). But you could follow these principles:

You have 2 keys. Wether symmetric (AES) or asymmetric (Public & private key). Key 1 is closed, key 2 is open.

For every file you create a 3rd key, this key is typically a symmetric (AES) key. This key you encrypt with key 1 and key 2, and the result of these 2 encryptions you store in the header of the file. With key 3 you encrypt your data.

To read, you can choose key 1 or key 2 to decrypt the key you need (key 3) to read the contents of the file.

The System.Security.Cryptography namespace contains all you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文