AES256 encryption, output characters depending on input… why?

0 投票
最新提问 用户: (160 分)

I'm using VS to encrypt some data using this function:

public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
    byte[] encryptedBytes = null;

    // Set your salt here, change it to meet your flavor:
    // The salt bytes must be at least 8 bytes.
    byte[] saltBytes = new byte[] { 6, 99, 26, 12, 68, 22, 89, 12, 49, 55, 92, 82, 87, 86, 10, 75, 98, 122, 73 };

    using (MemoryStream ms = new MemoryStream())
        using (RijndaelManaged AES = new RijndaelManaged())
            AES.KeySize = 256;
            AES.BlockSize = 256;

            var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
            AES.Key = key.GetBytes(AES.KeySize / 8);
            AES.IV = key.GetBytes(AES.BlockSize / 8);

            AES.Mode = CipherMode.CBC;

            using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
            encryptedBytes = ms.ToArray();

    return encryptedBytes;

My question is why when I put plain text to encrypt the output only contains readable characters that can be viewed with any text processor like notepad, but, if the data to encrypt is from a file containing special characters the output now also contains special characters that can't be viewed from text processor... why???

Example of encrypt "hello" "fMIiLZzIKME2gTAarpQqP7y8kOjQvDS12lSOOBtaCbI="

Example of encrypt binary data: "b!,4ov潞1煤l j么玫 ;>虏)j娄鹿氓眉LNnU+5"

发表于 用户: (100 分)
Please remove "Cypher" from the tags. This tag is related to Cypher Query Language.

2 个回答

0 投票
最新回答 用户: (140 分)

Because AES doens't deal with chars, or strings, only with bytes.

If you do want to be able to look at the result in a text editor, you'll have to encode the outputted bytes.

One of such encoders would be the base64 one.

Which would output a human "readable" string.

发表于 用户: (160 分)
Yes, I know AES only see bytes... but is really rare that the output contains non readable characters only if the input containt it too... don't you think?
发表于 用户: (140 分)
I would've called coincidence on that one, but then there was your self answer :)
0 投票
最新回答 用户: (160 分)

I found the probem... I'm an idiot... I have a Convert.ToBase64String function called when the input is plain text... I didn't see this.



欢迎来到 Security Q&A ,有什么不懂的可以尽管在这里提问,你将会收到社区其他成员的回答。