加密到内存流,传递给文件流

Encrypt to memory stream, pass it to file stream

基于此代码:

    static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        byte[] encrypted;
        // Create an Rijndael object
        // with the specified key and IV.
        using (Rijndael rijAlg = Rijndael.Create())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);

                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }


        // Return the encrypted bytes from the memory stream.
        return encrypted;

    }

我也需要将结果(加密)保存到文件中。我试图创建一个文件流并 CopyTo 或 WriteTo 将内存流形成到文件流,但输出为空:

    static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        byte[] encrypted;
        // Create an Rijndael object
        // with the specified key and IV.
        using (Rijndael rijAlg = Rijndael.Create())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);

                    }
                    encrypted = msEncrypt.ToArray();
                    using (FileStream file = new FileStream("file.bin", FileMode.Create, System.IO.FileAccess.Write))
                    {
                        msEncrypt.CopyTo(file);
                    }
                }
            }
        }

因为我需要一个文件(不需要内存流),所以我直接用文件流替换了内存流,现在我有一个包含加密文本的文件:

    static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        //byte[] encrypted;
        // Create an Rijndael object
        // with the specified key and IV.
        using (Rijndael rijAlg = Rijndael.Create())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption.
            //using (MemoryStream msEncrypt = new MemoryStream())
            using (FileStream fileEncrypt = new FileStream("file.bin", FileMode.Create, System.IO.FileAccess.Write))
            {
                using (CryptoStream csEncrypt = new CryptoStream(fileEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);

                    }
                    //encrypted = fileEncrypt.ToArray();
                }
            }
        }


        // Return the encrypted bytes from the memory stream.

        return null;
    }

编辑:此处是包含以下内容的文本文件的结果: 大家好,我叫月亮! 很高兴认识你!

我的解决方案: zªžRí#°ìªPélZ_êY–eòÞiOLÍô]xÏÛùNeסÔí°"þà

使用 Xanatos 一个: +h†+vÁïц60œû¦&PÐvd;xäAÊ<²¾’ïõž2×-†êº,]°

我的另一个尝试: ¤µØ¬?ùVF‹R(ªÆä∼Éöb·¯Sƒ¸øÂiœô¶Žµ),vÒÕ¥–F3U

再次尝试使用 Xanatos 提示: D*’``¢Ý4RúŽP2ÚwvŠE§ÚÅâ8íÜèq%ÉO¶}¸J‘—ôå ¼7

mmmm...每次我加密时它们都会改变....我以前不知道,抱歉...谢谢 Xanatos,您的解决方案很好!

我的评论全错了:-)

现在...我忘记了在 Dispose() 上,在这种情况下 StreamWriter Dispose() 是基础流(CryptoStream)。

static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
    // Check arguments.
    if (plainText == null || plainText.Length <= 0)
        throw new ArgumentNullException("plainText");
    if (Key == null || Key.Length <= 0)
        throw new ArgumentNullException("Key");
    if (IV == null || IV.Length <= 0)
        throw new ArgumentNullException("Key");
    byte[] encrypted;
    // Create an Rijndael object
    // with the specified key and IV.
    using (Rijndael rijAlg = Rijndael.Create())
    {
        rijAlg.Key = Key;
        rijAlg.IV = IV;

        // Create a decrytor to perform the stream transform.
        ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {

                    //Write all data to the stream.
                    swEncrypt.Write(plainText);

                }

                encrypted = msEncrypt.ToArray();
            }

            using (FileStream file = new FileStream("file.bin", FileMode.Create, System.IO.FileAccess.Write))
            {
                file.Write(encrypted, 0, encrypted.Length);
            }
        }
    }
}

所以我建议的修改是简单地保留 msEncrypt.ToArray() "as is" 并将 byte[] encrypted 写为 byte[]

因为我需要一个文件(不需要内存流),所以我直接用文件流替换了内存流,现在我有一个包含加密文本的文件:

    static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        //byte[] encrypted;
        // Create an Rijndael object
        // with the specified key and IV.
        using (Rijndael rijAlg = Rijndael.Create())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption.
            //using (MemoryStream msEncrypt = new MemoryStream())
            using (FileStream fileEncrypt = new FileStream("file.bin", FileMode.Create, System.IO.FileAccess.Write))
            {
                using (CryptoStream csEncrypt = new CryptoStream(fileEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);

                    }
                    //encrypted = fileEncrypt.ToArray();
                }
            }
        }


        // Return the encrypted bytes from the memory stream.

        return null;
    }

@Plutonix,你是对的....我决定使用 Xanatos 的解决方案来解决我的问题:

    static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        byte[] encrypted;
        // Create an Rijndael object
        // with the specified key and IV.
        using (Rijndael rijAlg = Rijndael.Create())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);

                    }
                    encrypted = msEncrypt.ToArray();
                    //csEncrypt.Flush();
                    //msEncrypt.Position = 0;
                    using (FileStream file = new FileStream("file.bin", FileMode.Create, System.IO.FileAccess.Write))
                    {
                        file.Write(encrypted, 0, encrypted.Length);
                    }
                }
            }
        }


        // Return the encrypted bytes from the memory stream.
        return encrypted;

    }