加密字符串未返回解密

时间:2016-02-02 21:02:51

标签: c# encryption

重现意外结果的步骤:

  1. 使用http://aesencryption.net/我使用HappyCoding加密文本yecpPqAJ+PnBMtggWVz42WME3TjhG313OhvBuUJOFtc=作为关键字,并在下拉列表中选择256-Bit选项。结果我收到了Lox/sfjNyXOzP9ZE8Fjj9REcuB+iJ1EXXuNjf2du29c=
  2. 然后我在代码中通过Decrypt函数运行它:

    var testAesString = "Lox/sfjNyXOzP9ZE8Fjj9REcuB+iJ1EXXuNjf2du29c=";
    var decryptedString = Decrypt(testAesString, key);
    

    并接收"�ГYC���{R\u0017V��@\u0013�NH�$�|�\u001a)˪n�mp"而不是“HappyCoding”

  3. Decrypt功能的代码如下:

    private static string Decrypt(string stringCypher_Text, string stringKey)
            {
                Byte[] Key = Convert.FromBase64String(stringKey);
    
                Byte[] Cypher_Text = Convert.FromBase64String(stringCypher_Text);
    
                RijndaelManaged Crypto = null;
                MemoryStream MemStream = null;
                ICryptoTransform Decryptor = null;
                CryptoStream Crypto_Stream = null;
                StreamReader Stream_Read = null;
                string Plain_Text;
    
                try
                {
                    Crypto = new RijndaelManaged();
                    Crypto.Padding = PaddingMode.Zeros;
                    Crypto.Key = Key;
                    Crypto.BlockSize = 256;
                    Crypto.Mode = CipherMode.ECB;
                    Crypto.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    
                    Decryptor = Crypto.CreateDecryptor(Crypto.Key, Crypto.IV);
                    MemStream = new MemoryStream(Cypher_Text);
                    Crypto_Stream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);
                    Stream_Read = new StreamReader(Crypto_Stream);
                    Plain_Text = Stream_Read.ReadToEnd();
                }
                finally
                {
                    if (Crypto != null)
                        Crypto.Clear();
    
                    MemStream.Flush();
                    MemStream.Close();
                }
                return Plain_Text;
            }
    

    我没有收到任何错误。我收到了意想不到的结果。我不知道如何进一步测试它。我的想法可能是我用来接收加密值的网站首先使用不同的设置等。

    赞赏如何测试和/或解决的任何方向。

2 个答案:

答案 0 :(得分:2)

更多地使用它并获得以下工作。该网站似乎只抓取您用作密钥的任何字符串的前32个字节。

public class MainClass<T>{

   UtilityClass utility;

   public MainClass(){
       utility = new InjectorHelper().getHelper();
   }

   ...

   public static class InjectorHelper{

       @Inject
       UtilityClass utilityClass;

       public InjectorHelper(){
           Injector.getInstance().getAppComponent().inject(this);
       }

       public UtilityClass getUtilityClass(){
           return utilityClass
       }
   }
}

答案 1 :(得分:0)

因为密文是两个块,但是纯文本少于一个块,我猜第一个块是IV。

尝试使用密文的前16个字节作为CBC模式的IV,并解密接下来的16个字节。

相关问题