Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TripleDESCryptoServiceProvider奇怪字符_C# - Fatal编程技术网

C# TripleDESCryptoServiceProvider奇怪字符

C# TripleDESCryptoServiceProvider奇怪字符,c#,C#,我试图解密一些我加密的文本,但是,我得到了奇怪的字符: ﶇ쒏栎ル찢샋�㨺⸸⓬왛㓘瞅苽景崲胏퐛㽵㈊褗邌≟䒙쉪ਊᮻ 以下是加密文本: 1Cu+45SHXGSSHMJIYUZX3UQYPR6N4VXUWIVZYTDCUS05L4TZT4JJBJLG6UJBZZGGJRWWTVMOWC3FGLBU/IsvM6U3il3i 如何获取常规文本 public static string DecryptData(string encryptedtext, string key) {

我试图解密一些我加密的文本,但是,我得到了奇怪的字符:

ﶇ쒏栎ル찢샋�㨺⸸⓬왛㓘瞅苽景崲胏퐛㽵㈊褗邌≟䒙쉪ਊᮻ

以下是加密文本:

1Cu+45SHXGSSHMJIYUZX3UQYPR6N4VXUWIVZYTDCUS05L4TZT4JJBJLG6UJBZZGGJRWWTVMOWC3FGLBU/IsvM6U3il3i

如何获取常规文本

       public static string DecryptData(string encryptedtext, string key)
    {
        key = m_Key;
        try
        {
            TripleDESCryptoServiceProvider TripleDes = new TripleDESCryptoServiceProvider();
            TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
            TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);

            // Convert the encrypted text string to a byte array.
            byte[] encryptedBytes = Convert.FromBase64String(encryptedtext);

            // Create the stream.
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            // Create the decoder to write to the stream.
            CryptoStream decStream = new CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

            // Use the crypto stream to write the byte array to the stream.
            decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
            decStream.FlushFinalBlock();

            // Convert the plaintext stream to a string.
            return System.Text.Encoding.Unicode.GetString(ms.ToArray());
        }
        catch (Exception)
        {
            return DecryptDataOldKey(encryptedtext, string.Empty);
        }
    }
这是我的加密代码:

        public static string EncryptData(string plaintext, string key = "")
    {
        TripleDESCryptoServiceProvider TripleDes = new TripleDESCryptoServiceProvider();
        TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
        TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);

        // Convert the plaintext string to a byte array.
        byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plaintext);

        // Create the stream.
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        // Create the encoder to write to the stream.
        CryptoStream encStream = new CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

        // Use the crypto stream to write the byte array to the stream.
        encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
        encStream.FlushFinalBlock();

        // Convert the encrypted stream to a printable string.
        return Convert.ToBase64String(ms.ToArray());
    }

加密和解密数据时应使用相同的编码,例如UTF8。将System.Text.Encoding.Unicode更改为System.Text.Encoding.UTF8

,另外,还有相当多的对象,它们的类实现了
IDisposable
接口。您应该使用语句将这些对象的生存期包装在
中,以确保正确的确定性处理。此外,
DecryptData()
似乎有点奇怪-您正在用类级成员
m_key
覆盖传入的参数
key
。也许您在这里使用不同的密钥进行加密和解密?您好,我使用Unicode对其进行加密。从现在起我应该使用UTF8吗?