【问题标题】:How is encryption done without encoding the string to byte[]?在不将字符串编码为 byte[] 的情况下如何进行加密?
【发布时间】:2014-05-26 20:14:24
【问题描述】:

几天来,我一直在尝试了解 TripleDES 的加密/解密代码。而且我在google上看过很多代码,下面显示的代码就是其中之一。

static void Main(string[] args)
    {

        string original = "Here is some data to encrypt!";
        TripleDESCryptoServiceProvider myTripleDES = new TripleDESCryptoServiceProvider();
        byte[] encrypted = EncryptStringToBytes(original, myTripleDES.Key, myTripleDES.IV);
        string encrypt = Convert.ToBase64String(encrypted);
        string roundtrip = DecryptStringFromBytes(encrypted, myTripleDES.Key, myTripleDES.IV);
        Console.WriteLine("encryted:   {0}", encrypt);
        Console.WriteLine("Round Trip: {0}", roundtrip);
        Console.ReadLine();
    }



    static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {

        byte[] encrypted; 
        using (TripleDESCryptoServiceProvider tdsAlg = new TripleDESCryptoServiceProvider())
        {
            tdsAlg.Key = Key;
            tdsAlg.IV = IV;
            ICryptoTransform encryptor = tdsAlg.CreateEncryptor(tdsAlg.Key, tdsAlg.IV);
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        swEncrypt.Write(plainText);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }
        return encrypted;
    }


    static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
    { 
        string plaintext = null; 
        using (TripleDESCryptoServiceProvider tdsAlg = new TripleDESCryptoServiceProvider())
        {
            tdsAlg.Key = Key;
            tdsAlg.IV = IV;
            ICryptoTransform decryptor = tdsAlg.CreateDecryptor(tdsAlg.Key, tdsAlg.IV);
            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }

        }

        return plaintext;

    } 

代码中没有错误。我工作正常。但奇怪的是,我注意到纯文本从未被编码过。没有像Encoding.Unicode.GetBytes(plainText);Encoding.UTF8.GetBytes(plainText); 或类似的行。所以,我的问题是,(在代码中)作为字符串的纯文本如何转换为加密字节?流中是否有任何工作?如果是这样,那么在哪里以及如何?据我了解,在将字符串转换为字节的流之间没有这样的线。那么,如果没有这种基本转换,整体代码是如何工作的呢?

更新: 此代码真的是有效代码吗?

【问题讨论】:

  • StreamWriter 将字符串转换为 UTF-8 字节。
  • 真的吗?我不知道。

标签: c# tripledes


【解决方案1】:

您将明文发送到swEncrypt.Write(plaintext) 行中的加密流。这会进行字节转换。

【讨论】:

  • 谢谢。我不知道 StreamWriter 会这样做。现在一切都清楚了。
【解决方案2】:

StreamWriter 正在进行编码。使用的constructor 指定了UTF-8 编码:

此构造函数创建一个使用 UTF-8 编码的 StreamWriter,而没有 字节顺序标记 (BOM)

【讨论】:

  • 我觉得问这个问题很傻。我应该对溪流做更多的研究!我一直试图避免流,但现在我明白没有从“流”中逃脱。所有答案都在信息流中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-02
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2017-11-25
相关资源
最近更新 更多