【问题标题】:C# Byte[] EncryptionC# 字节[] 加密
【发布时间】:2020-11-26 04:17:41
【问题描述】:

我有一个 Byte[] 字段,它是我需要加密的文件内容。没有什么特别或花哨的,只是足以确保下一个得到它的人不费吹灰之力就无法轻松解码它。我会使用 .Net Framework 4.0 附带的加密,但我绝对不需要让文件变得比实际更大。

我想过只是简单地反转数组或在末尾添加几个字节......?

如果我可以避免使数组变得更大,那就太好了。

有什么建议吗?

谢谢!

【问题讨论】:

标签: c# encryption


【解决方案1】:

增加 1-16 个字节有影响吗?默认情况下,AES 将使用以下方法填充:

    private static void EncryptThenDecrypt(byte[] msg)
    {
        byte[] message = msg; // fill with your bytes

        if (message is null)
        {
            return;
        }

        byte[] encMessage; // the encrypted bytes
        byte[] decMessage; // the decrypted bytes - s/b same as message
        byte[] key;
        byte[] iv;

        using (SymmetricAlgorithm aes = Aes.Create())
        {
            if (aes is null)
            {
                iv = key = null;
                encMessage = Array.Empty<byte>();
            }
            else
            {
                aes.GenerateKey();
                aes.GenerateIV();
                key = aes.Key;
                iv = aes.IV;
                encMessage = EncryptBytes(aes, message);
            }
        }

        using (SymmetricAlgorithm aes = Aes.Create())
        {
            if (aes is null || key is null)
            {
                decMessage = Array.Empty<byte>();
            }
            else
            {
                aes.Key = key;
                aes.IV = iv;
                decMessage = DecryptBytes(aes, encMessage);
            }
        }

        Debug.Assert(message.SequenceEqual(decMessage), "Decrypted bytes do not match original bytes.");
    }

    private static byte[] EncryptBytes(SymmetricAlgorithm alg, byte[] message)
    {
        if (message is null)
        {
#pragma warning disable S1168 // Empty arrays and collections should be returned instead of null
            return null;
#pragma warning restore S1168 // Empty arrays and collections should be returned instead of null
        }

        if (message.Length == 0)
        {
            return message;
        }

        if (alg is null)
        {
            throw new ArgumentNullException(nameof(alg));
        }

        using (MemoryStream stream = new MemoryStream())
        using (ICryptoTransform encryptor = alg.CreateEncryptor())
        using (CryptoStream encrypt = new CryptoStream(stream, encryptor, CryptoStreamMode.Write))
        {
            encrypt.Write(message, 0, message.Length);
            encrypt.FlushFinalBlock();
            return stream.ToArray();
        }
    }

    private static byte[] DecryptBytes(SymmetricAlgorithm alg, byte[] message)
    {
        if (message is null)
        {
#pragma warning disable S1168 // Empty arrays and collections should be returned instead of null
            return null;
#pragma warning restore S1168 // Empty arrays and collections should be returned instead of null
        }

        if (message.Length == 0)
        {
            return message;
        }

        if (alg is null)
        {
            throw new ArgumentNullException(nameof(alg));
        }

        using (MemoryStream stream = new MemoryStream())
        using (ICryptoTransform decryptor = alg.CreateDecryptor())
        using (CryptoStream encrypt = new CryptoStream(stream, decryptor, CryptoStreamMode.Write))
        {
            encrypt.Write(message, 0, message.Length);
            encrypt.FlushFinalBlock();
            return stream.ToArray();
        }
    }

【讨论】:

  • 加密后如何解密?
  • 首先,在您创建SymmetricAlgorithm 时,请将您的 Key 和 IV 放在手边。然后使用DecryptBytes方法我将要编辑答案。
  • 好东西!非常感谢!
【解决方案2】:

不要发明自己的加密机制(即通过混淆实现安全性),请使用classes provided by the framework 之一。

【讨论】:

  • 如果我有各种大小的文件,这不会显着改变数组的大小,使其比原来的大很多吗?
  • 任何值得做的方法都会增加文件的大小。文件的当前大小是多少,您可以采取的一种方法是简单地压缩文件。您不必宣传您对文件所做的操作,它肯定不会增加文件大小,甚至可能会减小大小。
猜你喜欢
  • 2019-05-08
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 2015-10-08
  • 1970-01-01
  • 2013-04-06
相关资源
最近更新 更多