0

I have some problem and i can not define the reasons.

I have function to decrypt some info, the return value is a string that converted from binary to string.

public static string Decrypt(string encryptedText, string completeEncodedKey, int keySize)
    {

        RijndaelManaged aesEncryption = new RijndaelManaged();
        aesEncryption.KeySize = keySize;
        aesEncryption.BlockSize = 128;
        aesEncryption.Mode = CipherMode.CBC;
        aesEncryption.Padding = PaddingMode.Zeros;
        aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[0]);
        aesEncryption.Key = Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(completeEncodedKey)).Split(',')[1]);
        ICryptoTransform decrypto = aesEncryption.CreateDecryptor();
        byte[] encryptedBytes = Convert.FromBase64CharArray(encryptedText.ToCharArray(), 0, encryptedText.Length);// convert the cipertext to binary
        string RESULT = (string)ASCIIEncoding.UTF8.GetString(decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length));//convert the binary to string

        return RESULT;
     }

The problem appears when i call this function and get the result, and then try to display the result with additional strings, for example by this message box:

 String result= function.Decrypt(textToBeDecrypted, key, 128);


MessageBox.Show("This is sample text " + result + " here i want to append another string ");

ONLY APPENDED TEXT (IN THIS EXAMPLE: " here i want to append another string ") IS NOT DISPLAYED

What's wrong with this?

4

3 回答 3

1

尝试这个:

string result = function.Decrypt(textToBeDecrypted, key, 128).Replace("\0", string.Empty);
于 2013-04-29T19:43:56.027 回答
1

http://bytes.com/topic/c-sharp/answers/275256-rijndael-decrypt-returning-escape-characters-end-string

似乎是同一个问题。我敢打赌你最后有一个转义字符(\ 0)。

于 2013-04-29T19:30:56.650 回答
1
aesEncryption.Padding = PaddingMode.Zeros;

您在消息末尾添加了零……就 Win32 MessageBox API 而言,零结束了字符串。

在解密过程中删除填充(使用不同的填充模式使这更容易)。

于 2013-04-29T19:48:03.057 回答