繁体   English   中英

C# AES 和 RSA 文件加密 - 如何使用 IV?

[英]C# AES and RSA File Encryption - How to use IV?

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

  • 我有一些需要备份到服务器的机密日志文件。
  • 我有一个程序每天都会生成这些日志文件。
  • 这些日志文件很少需要打开。
  • 我只有一个 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();
            }
        }
    }

您非常接近,请在创建 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 的 byte[], 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包装器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM