首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用FileStreams的Strings对函数进行重新编码

如何使用FileStreams的Strings对函数进行重新编码
EN

Stack Overflow用户
提问于 2019-05-07 07:26:18
回答 2查看 68关注 0票数 1

我正在构建一个需要加密保存等的游戏,所以我做了一些功能来帮助我加密它们。但是,我目前拥有的唯一函数是使用FileStreams作为输入和输出,但我希望使用字符串。函数在文件上运行良好,但我在将FileStreams转换为MemoryStreams到字符串时遇到了困难。注意:我已经删除了不相关的代码。OurCodeWorld.GenerateRandomSalt() 100%工作,用FileStream加密进行测试。

完整代码:

Program.cs:

代码语言:javascript
复制
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;


namespace Encryption_test
{

    class Program
    {
        static public string encryptedExtension = ".aes";
        static public string decryptedExtension = ".decrypted";

        static void Main(string[] args)
        {
            string path = Environment.CurrentDirectory;
            Log($"Current path: {path}");
            string filePath = $"{path}/unencrypted.txt";
            string message =
@"Hello world!
This is my message. 1234";

            RunStackOverFlowString();

            //Sleep forever...zzz
            Thread.Sleep(10000);
            Console.WriteLine();

            float TicksToMs(long ticks)
            {
                return ticks / (float)Stopwatch.Frequency * 1000F;
            }

            void RunStackOverFlowString()
            {
                byte[] salt = OurCodeWorld.GenerateRandomSalt();
                int iterations = 1024;
                string password = "";
                string enc = StackOverflow.EncryptString(message, password, salt, iterations);
                Log($"Enc: {enc}");
                string dec = StackOverflow.DecryptString(enc, password, salt, iterations);
                Log($"Dec: {dec}");
            }
        private static void WriteFile(string path, string value)
        {
            FileStream _file = File.OpenWrite(path);
            byte[] info = new UTF8Encoding(true).GetBytes(value);
            _file.Write(info, 0, info.Length);
            _file.Close();
        }

        private static string ReadFile(string filePath, long length = long.MaxValue)
        {
            FileStream _file = File.OpenRead(filePath);
            if (length == long.MaxValue)
                length = _file.Length;

            byte[] b = new byte[length];
            UTF8Encoding temp = new UTF8Encoding(true);

            _file.Read(b, 0, b.Length);
            _file.Close();

            return temp.GetString(b);
        }

        private static void DeleteFile(string path)
        {
            File.Delete(path);
        }
        private static void CreateFile(string path)
        {
            if (File.Exists(path))
                DeleteFile(path);
            FileStream a = File.Open(path, FileMode.Create);
            a.Close();
        }
        static void Log(string message)
        {
            Console.WriteLine(message);
        }
    }
}

StackOverFlow.cs:

代码语言:javascript
复制
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

class StackOverflow
{

    // Rfc2898DeriveBytes constants:

    /// <summary>Decrypt a file.</summary>
    /// <remarks>NB: "Padding is invalid and cannot be removed." is the Universal CryptoServices error.  Make sure the password, salt and iterations are correct before getting nervous.</remarks>
    /// <param name="sourceFilename">The full path and name of the file to be decrypted.</param>
    /// <param name="destinationFilename">The full path and name of the file to be output.</param>
    /// <param name="password">The password for the decryption.</param>
    /// <param name="salt">The salt to be applied to the password.</param>
    /// <param name="iterations">The number of iterations Rfc2898DeriveBytes should use before generating the key and initialization vector for the decryption.</param>
    public static void DecryptFile(string sourceFilename, string destinationFilename, string password, byte[] salt, int iterations)
    {
        AesManaged aes = new AesManaged();
        aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
        aes.KeySize = aes.LegalKeySizes[0].MaxSize;
        // NB: Rfc2898DeriveBytes initialization and subsequent calls to   GetBytes   must be eactly the same, including order, on both the encryption and decryption sides.
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
        aes.Key = key.GetBytes(aes.KeySize / 8);
        aes.IV = key.GetBytes(aes.BlockSize / 8);
        aes.Mode = CipherMode.CBC;
        ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);

        using (FileStream destination = new FileStream(destinationFilename, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
            {
                try
                {
                    using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        source.CopyTo(cryptoStream);
                    }
                }
                catch (CryptographicException exception)
                {
                    if (exception.Message == "Padding is invalid and cannot be removed.")
                        throw new ApplicationException("Universal Microsoft Cryptographic Exception (Not to be believed!)", exception);
                    else
                        throw;
                }
            }
        }
    }

    /// <summary>Encrypt a file.</summary>
    /// <param name="sourceFilename">The full path and name of the file to be encrypted.</param>
    /// <param name="destinationFilename">The full path and name of the file to be output.</param>
    /// <param name="password">The password for the encryption.</param>
    /// <param name="salt">The salt to be applied to the password.</param>
    /// <param name="iterations">The number of iterations Rfc2898DeriveBytes should use before generating the key and initialization vector for the decryption.</param>
    public static void EncryptFile(string sourceFilename, string destinationFilename, string password, byte[] salt, int iterations)
    {
        AesManaged aes = new AesManaged();
        aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
        aes.KeySize = aes.LegalKeySizes[0].MaxSize;
        // NB: Rfc2898DeriveBytes initialization and subsequent calls to   GetBytes   must be eactly the same, including order, on both the encryption and decryption sides.
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
        aes.Key = key.GetBytes(aes.KeySize / 8);
        aes.IV = key.GetBytes(aes.BlockSize / 8);
        aes.Mode = CipherMode.CBC;
        ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);

        using (FileStream destination = new FileStream(destinationFilename, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
            {
                using (FileStream source = new FileStream(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    source.CopyTo(cryptoStream);
                }
            }
        }
    }

    //THIS IS MY CODE
    public static string EncryptString(string inputString, string password, byte[] salt, int iterations)
    {
        AesManaged aes = new AesManaged();
        aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
        aes.KeySize = aes.LegalKeySizes[0].MaxSize;
        // NB: Rfc2898DeriveBytes initialization and subsequent calls to   GetBytes   must be eactly the same, including order, on both the encryption and decryption sides.
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
        aes.Key = key.GetBytes(aes.KeySize / 8);
        aes.IV = key.GetBytes(aes.BlockSize / 8);
        aes.Mode = CipherMode.CBC;
        ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);

        var source = StringToStream(inputString);
        var output = new MemoryStream();

        CryptoStream cryptoStream = new CryptoStream(output, transform, CryptoStreamMode.Write);
        source.CopyTo(cryptoStream);
        return StreamToString(output);
    }

    public static string DecryptString(string inputString, string password, byte[] salt, int iterations)
    {
        AesManaged aes = new AesManaged();
        aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
        aes.KeySize = aes.LegalKeySizes[0].MaxSize;
        // NB: Rfc2898DeriveBytes initialization and subsequent calls to   GetBytes   must be eactly the same, including order, on both the encryption and decryption sides.
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(password, salt, iterations);
        aes.Key = key.GetBytes(aes.KeySize / 8);
        aes.IV = key.GetBytes(aes.BlockSize / 8);
        aes.Mode = CipherMode.CBC;
        ICryptoTransform transform = aes.CreateDecryptor(aes.Key, aes.IV);

        var source = StringToStream(inputString);
        var output = new MemoryStream();

        CryptoStream cryptoStream = new CryptoStream(output, transform, CryptoStreamMode.Write);
        source.CopyTo(cryptoStream);
        return StreamToString(output);
    }


    public static Stream StringToStream(string s)
    {
        var stream = new MemoryStream();
        var writer = new StreamWriter(stream);
        writer.Write(s);
        writer.Flush();
        stream.Position = 0;
        return stream;
    }
    public static string StreamToString(Stream s)
    {
        s.Position = 0;
        byte[] buffer = new byte[s.Length];
        s.Read(buffer, 0, (int)s.Length);
        return Encoding.Default.GetString(buffer);
    }
}

我已经测试了StreamToStringStringToStream方法,它们工作得很好。当我运行DecryptString时,没有输出,该函数返回乱码字符串,通常看起来类似于这个Dec: ?K??? ?@?????n?l?r????T?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-07 08:27:39

我测试了密码。有两个问题:

  1. 您使用加密的字节并使用编码将它们转换为字符串。这个过程通常会失败,因为您不能接受任意字节并将它们转换为字符串。字节和字符串之间的映射不是1:1。很可能,这里根本不应该使用字符串。相反,将数据保持为字节格式,并将这些字节写入文件。方法签名应该类似于byte[] Encrypt(byte[] input, ...)。加密代码不需要从字符串转换到字符串。
  2. EncryptString必须使用FlushFinalBlock刷新密码流。如果没有这样做,数据在结尾将丢失。
票数 0
EN

Stack Overflow用户

发布于 2019-05-07 08:06:35

字符串和二进制数据之间的转换是用和Encoding完成的。编码决定如何将字符转换为二进制数据。常见的封装是Unicode、UTF8或ASCII。如果您想要可预测的结果,则需要对这两种转换使用特定的编码。

StringToStream中,您根本不指定编码,而在StreamToString中使用Encoding.DefaultEncoding.Default不提供可预测的结果,因为它使用当前代码页(如果您在Windows上运行)。因此,您不能确定在这两种方法中都使用了相同的编码。

解决方案是在两端提供相同的编码:

代码语言:javascript
复制
public static Stream StringToStream(string s)
{
    byte[] buffer = Encoding.UTF8.GetBytes(s);
    return new MemoryStream(buffer);
}

public static string StreamToString(Stream s)
{
    s.Position = 0;
    byte[] buffer = new byte[s.Length];
    s.Read(buffer, 0, (int)s.Length);
    return Encoding.UTF8.GetString(buffer);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56017544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档