解密AES-128-ECB文本时的结果不正确

时间:2014-05-29 16:41:30

标签: c# encryption aes encryption-symmetric ecb

我有一段Base64文本,我知道它是在AES-128-ECB mode加密的,甚至知道密钥(YELLOW SUBMARINE,16个字节)。我正在使用以下代码对其进行解密。

textbox1包含cyphertext;

textbox2包含密钥

    private void button6_Click(object sender, EventArgs e)
    {
        Byte[] CTBytes = System.Convert.FromBase64String(textBox1.Text);            
        Byte[] KeyBytes = new Byte[textBox2.TextLength];
        string Key = textBox2.Text;
        Aes Decryptor = Aes.Create();
        Decryptor.Mode = CipherMode.ECB;
        Decryptor.BlockSize = 128;
        for (int iCounter = 0; iCounter < textBox2.Text.Length; iCounter++)            
            KeyBytes[iCounter] = Convert.ToByte(textBox2.Text[iCounter]);          
        Decryptor.KeySize = KeyBytes.Length * 8;
        Decryptor.Key = KeyBytes;            
        Decryptor.Padding = PaddingMode.None;
        ICryptoTransform Decr = Decryptor.CreateDecryptor();
        Byte[] plainText = null;
                    using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, Decr, CryptoStreamMode.Write))
            {
                cs.Write(CTBytes, 0, CTBytes.Length);
            }

            plainText = ms.ToArray();
        }                        
        string s = System.Text.Encoding.ASCII.GetString(plainText);
        textBox3.Text = s;
        string s1 = System.Text.Encoding.Unicode.GetString(plainText);
        textBox2.Text = s1;
    }

但结果不正确(无意义)。有没有我错过的东西?如果有必要,我也可以提供密文。

更新:密文

U2FsdGVkX1+V1HkNnBtngUKxuppRFBa0cxtj5jwMcQsgxA+WdPK2JtNCHIyJ4xyj
TABANu/jEXSigEG8NiVczGSkWipGV1rVT3+QmrQAAToAm68BR9ldVHG2IUpwN20s
rKi6bcSNKk7+w9YoSrkO8eX9JEiBvgAUxNrdShY/K7BUFKkwr0y/kJAo9w2wy9vA
7RjUKggwe3DSm/OIrs+yOTUGo0lla0PdF9jwsOraVX6HeAFT8bfAaKFGXqKgzYKZ
KfAoP4qQdg2qWH1FohCsxTMPg6VpjSZo5c21RubgBm3pwdIHibhapFv+9xkgCt6C
y4LL2iaBc+WzCjezYPvXD3GGJjhI1dtDWs9ihoGLh6EBe9Nj9A8Aoi2U2jcNrbkD
PPtYR1vwsYurt20YPqOAUxdytquANgHXQPs74M4MLoHkjmZALQ2TFVFUb4tZXpEe
U7Dv/3uJkz1Exnmona4D4pdrlEgALqPftmUaC7EDWLa9WGPYeYPO0ktD9uEBiATS
HZM7i6erQgVdh1zsNPa4+vhOdSf5EdOW3MrXFMqns/M6foO3tA60JDBhDzwdmzTq
HEgaSPNU6rbhEtUcdzOoPwT5zw5mJr01Y46CRliNz2Hbg/Q+no5sPdolJBO6VL/v
FHf29rkPv0r/yIm4jTCtgzJRTJ0twWl0WvsSWh8E11AQh7rrAq+5aUMr5+CfZq5E
/nGEM5e9rx/oMZ68T6Rwz9oXE+kJazUt5ipj/ccu1P/HlCcRHXa2tt7o+tS+koN4
F60UdRm4ExoeWKRhpD93Xlf22GFKST8abA/cRjFSLwiB+oI0vZLXQ6FFz2F9Iogl
eXxjIx7JUpOsYYxgnUzznffzMT33p5GivXWE91LU7sptRtSDpmrn1UUhjE+KkFGi
hAOAjMzqG7Oos5V1h8SQaBug2yk4H2GX9SkBtLul6YU=

0 个答案:

没有答案