1

我使用以下代码使用 3-DES 算法使用密钥加密字符串:

private bool Encode(string input, out string output, byte[] k, bool isDOS7)
    {
        try
        {
            if (k.Length != 16)
            {
                throw new Exception("Wrong key size exception");
            }
            int length = input.Length % 8;
            if (length != 0)
            {
                length = 8 - length;
                for (int i = 0; i < length; i++)
                {
                    input += " ";

                }
            }
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

            des.Mode = CipherMode.ECB;
            des.Padding = PaddingMode.Zeros;
            des.Key = k;
            ICryptoTransform ic = des.CreateEncryptor();



            byte[] bytePlainText = Encoding.Default.GetBytes(input);
            MemoryStream ms = new MemoryStream();
            CryptoStream cStream = new CryptoStream(ms,
                ic,
                CryptoStreamMode.Write);

            cStream.Write(bytePlainText, 0, bytePlainText.Length);
            cStream.FlushFinalBlock();
            byte[] cipherTextBytes = ms.ToArray();
            cStream.Close();
            ms.Close();
            output = Encoding.Default.GetString(cipherTextBytes);

        }
        catch (ArgumentException e)
        {
            output = e.Message;
            //Log.Instance.WriteToEvent("Problem encoding, terminalID= "+objTerminalSecurity.TerminalID+" ,Error" + output, "Security", EventLogEntryType.Error);
            return false;
        }
        return true;
    }

我将输出参数原样发送到 WCF http-binding webservice,我注意到实际编码的字符串看起来不同,看起来有一些 \t 和 \n 但字符大致相同。

这是怎么回事,为什么服务器会得到不同的编码字符串?

4

1 回答 1

3

通常密文是 base64 编码的,以便在传输过程中保持二进制安全。

此外,我不会将 3DES 与 ECB 一起使用。太糟糕了,你一定是从某个地方复制粘贴的。在 cbc 模式下使用 AES,并考虑添加 cmac 或 hmac。

于 2012-06-18T17:15:30.933 回答