我有以下加密和解密代码。我设法加密字符串但无法解密

时间:2016-11-30 12:42:58

标签: c# rijndael

这里是加密代码

我是否正确编码了?或者我需要做出改变?我正在使用visual studio 2013和framework 4.5。

            cipherData = textBox_Plain_text.Text;
            plainbytes = Encoding.Unicode.GetBytes(cipherData);
            plainKey = Encoding.Unicode.GetBytes("0123456789abcded");
            desObj.Key = plainKey;
            desObj.Mode = CipherMode.CBC;
            desObj.Padding = PaddingMode.PKCS7;

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, desObj.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(plainbytes, 0, plainbytes.Length);
            cs.Close();
            cipherbytes = ms.ToArray();
            ms.Close();
            textBox_Encrypted_text.Text = Encoding.ASCII.GetString(cipherbytes);

这里是解密的代码。在我尝试解密之后,只显示第一个字母

  System.IO.MemoryStream ms1 = new System.IO.MemoryStream(cipherbytes);
  CryptoStream cs1 = new CryptoStream(ms1, desObj.CreateDecryptor(), CryptoStreamMode.Read);

            cs1.Read(cipherbytes, 0, cipherbytes.Length);
            plainbytes2 = ms1.ToArray();
            cs1.Close();
            ms1.Close();
            textBox_Decrypted_text.Text = Encoding.ASCII.GetString(plainbytes2);

结果

enter image description here

0 个答案:

没有答案