【问题标题】:C# AES and RSA File Encryption - How to use IV?C# AES 和 RSA 文件加密 - 如何使用 IV?
【发布时间】:2015-08-29 13:33:10
【问题描述】:

我正在编写一个在以下情况下工作的程序:

  • 我有一些机密日志文件需要备份到服务器。
  • 我有一个程序每天都会生成这些日志文件。
  • 这些日志文件很少需要打开。
  • 我只有一个 RSA 公钥/私钥对。
  • 该程序只有 RSA 公钥。
  • 每次程序生成其中一个机密文件时,我都会生成一个随机 AES 密钥。
  • 程序使用此 AES 密钥加密日志文件。
  • 然后我使用 RSA 公钥加密 AES 密钥
  • 然后我将 AES 加密文件和 RSA 加密的 AES 密钥都备份到服务器。

据我了解,该协议适合我的用例。

我遇到的问题是用 C# 对其进行编码。我遇到了我的 AES 加密需要一个初始化向量(IV),我试图通过在两者上使用公共 RSA 密钥来加密它和 AES 密钥。但是 512(2 * 256) 的大小比 RSA 乐于加密的要大。所以我发现,因为我每次都像 AES 密钥一样随机创建初始化向量,所以我可以将 IV 添加到 AES 密文的前面。但是,我不确定执行此操作的代码将插入到我的函数中的哪个位置

对“协议”的正确方向或将IV写入密文的其他方式的任何帮助都会很棒。提前谢谢你。

static public Tuple<byte[], byte[]> EncryptAES(byte[] toEncryptAES, RSAParameters RSAPublicKey)
    {
        byte[] encryptedAES = null;
        byte[] encryptedRSA = null;

        using (MemoryStream ms = new MemoryStream())
        {
            using (RijndaelManaged AES = new RijndaelManaged())
            {
                AES.KeySize = 256;
                AES.BlockSize = 128;
                AES.Mode = CipherMode.CBC;
                AES.GenerateIV();
                AES.GenerateKey();
                encryptedRSA = RSAEncrypt(AES.Key, RSAPublicKey);

                using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    ms.Write(AES.IV, 0, AES.KeySize); //DOESNT WORK HERE
                    //Can't use CS to write it to the stream else it will encrypt along with file
                    cs.Write(toEncryptAES, 0, toEncryptAES.Length);
                    cs.Close();
                }
                encryptedAES = ms.ToArray();
            }
        }
        return new Tuple<byte[], byte[]>(encryptedAES, encryptedRSA);
    }
    static public byte[] DecryptAES(byte[] toDecryptAES, byte[] AESKeyAndIV, RSAParameters RSAPrivateKey)
    {
        byte[] AESKey = RSADecrypt(AESKeyAndIV, RSAPrivateKey);

        using (MemoryStream ms = new MemoryStream())
        {
            using (RijndaelManaged AES = new RijndaelManaged())
            {
                AES.KeySize = 256;
                AES.BlockSize = 128;
                AES.Key = AESKey;
                ms.Read(AES.IV, 0, AES.KeySize); //Not sure if can read MS here
                AES.Mode = CipherMode.CBC;

                using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    //Would I need to move 0 to 256?
                    cs.Write(toDecryptAES, 0, toDecryptAES.Length);
                    cs.Close();
                }
                return ms.ToArray();
            }
        }
    }

【问题讨论】:

  • “但是 512(2 * 256) 的大小比 RSA 愿意加密的要大。” 这表明有两点不好:(1) 你的 IV 是 256 -bit long 不应该是这种情况,因为您将块大小设置为 128 位,这也是 IV 大小。 (2) 这表明您正在使用大小小于 768 位的 RSA 密钥。 今天,您至少应该使用 2048 位 RSA 密钥。
  • 啊,你是对的。我认为 IV 等于密钥大小,而不是块大小。当然是块大小。我会整理那个。还有 RSA 密钥,RSA 不喜欢加密小于其密钥大小的任何东西,对吗?我以为我制作了一个 4096 RSA 密钥,所以我会检查那部分并在使用它之前对其进行验证。感谢您的帮助,这两部分都非常重要。

标签: c# encryption cryptography aes rsa


【解决方案1】:

你离得很近,在创建 CryptoStream 之前写出 IV

static public Tuple<byte[], byte[]> EncryptAES(byte[] toEncryptAES, RSAParameters RSAPublicKey)
{
    byte[] encryptedAES = null;
    byte[] encryptedRSA = null;

    using (MemoryStream ms = new MemoryStream())
    {
        using (RijndaelManaged AES = new RijndaelManaged())
        {
            AES.KeySize = 256;
            AES.BlockSize = 128;
            AES.Mode = CipherMode.CBC;
            AES.GenerateIV();
            AES.GenerateKey();
            encryptedRSA = RSAEncrypt(AES.Key, RSAPublicKey);

            ms.Write(AES.IV, 0, AES.KeySize); //Move the write here.

            using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(toEncryptAES, 0, toEncryptAES.Length);
                cs.Close();
            }
            encryptedAES = ms.ToArray();
        }
    }
    return new Tuple<byte[], byte[]>(encryptedAES, encryptedRSA);
}

对于解密,请确保循环读取直到完全读取 IV 的字节 [],Stream.Read 不能保证读取您要求它读取的所有字节。我通常会创建一个静态方法ReadFully 以确保读取所有字节。

private static byte[] ReadFully(Stream stream, int length)
{
    int offset = 0;
    byte[] buffer = new byte[length];
    while(offset < length)
    {
        offset += stream.Read(buffer, offset, length - offset);
    }
    return buffer;
}

然后只需使用该方法读取 IV。您还想使用cs.Read 而不是cs.Write 来读取加密数据并将流置于读取模式,但是使用.CopyTo 并将数据复制到新的MemoryStream 会更容易。

static public byte[] DecryptAES(byte[] toDecryptAES, byte[] AESKeyAndIV, RSAParameters RSAPrivateKey)
{
    byte[] AESKey = RSADecrypt(AESKeyAndIV, RSAPrivateKey);

    using (MemoryStream source = new MemoryStream(toDecryptAES))
    {
        using (RijndaelManaged AES = new RijndaelManaged())
        {
            AES.KeySize = 256;
            AES.BlockSize = 128;
            AES.Key = AESKey;
            var iv = ReadFully(source, AES.KeySize);
            AES.IV = iv;
            AES.Mode = CipherMode.CBC;

            using (var cs = new CryptoStream(source, AES.CreateDecryptor(), CryptoStreamMode.Read))
            {
                using(var dest = new MemoryStream())
                {
                    cs.CopyTo(dest);
                    return dest.ToArray();
                }
            }
        }
    }
}

对于其他读者,请注意 RSAEncryptRSADecrypt 是调用 RSACryptoServiceProvider 的包装器。

【讨论】:

  • 哇,这是我所希望的最佳答案。太感谢了!读取流的写入模式很好,愚蠢的错误。
猜你喜欢
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-06
  • 1970-01-01
  • 2012-02-21
相关资源
最近更新 更多