开发者

Can encryption using RijndaelManaged produce spaces in the output?

I'm trying to implement a requirement in our system that all strings be trimmed before going into the database. There is a layer in our system where I could handle this in one place, but I'm concerned that I migh开发者_StackOverflow中文版t break encrypted strings if I do.

I know the encryption produces some non-alphanumeric characters, but are these limited to non-whitespace?


Most encryption algorithms - including Rijndael - work on binary data, not text. There's no concept of a space (or a non-alphanumeric character) within binary data.

If you must store encrypted data as text, you'll need to convert it from the binary data (e.g. a byte array) into text first. One simple way of doing that is to use Base64 (e.g. Convert.ToBase64String(data)) - and Base64 never includes spaces using the standard codabet.

So to summarize

  • If possible, store encrypted data in binary form (e.g. in an IMAGE field)
  • If you have to store it in a text field, convert it in an appropriate way, e.g. using Base64. Don't use something like Encoding.UTF8, as the encrypted data is not UTF-8-encoded text.
  • Base64 doesn't include spaces, so that aspect shouldn't be a problem for you.


Rinjdael and, more generally, every encryption algorithm may produce spaces in its output. You may want to encode this binary data in a way that prevents spaces, if really needed. I suggest you, however, to just store the data as it is if possible. Many RDBMS provide binary storage support: SQL Server, for example, supports the varbinary data type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜