将二进制转换为字符串时不显示字符串

时间:2013-04-29 19:24:18

标签: c# string binary ascii

我遇到了一些问题,我无法确定原因。

我有解密某些信息的功能,返回值是一个从二进制转换为字符串的字符串。

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;
     }

当我调用此函数并获得结果时出现问题,然后尝试使用其他字符串显示结果,例如通过此消息框:

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


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

仅附加文字(在此示例中:“此处我想附加另一个字符串”)未显示

这有什么问题?

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

试试这个:

string result = function.Decrypt(textToBeDecrypted, key, 128).Replace("\0", string.Empty);

答案 2 :(得分:1)

aesEncryption.Padding = PaddingMode.Zeros;

您在邮件末尾添加了零...并且就Win32 MessageBox API而言,零结束字符串。

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