可以加密使用RijndaelManaged在输出中产生空格吗? [英] 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 might break encrypted strings if I do.
我知道加密产生一些非字母数字字符,但这些字符限制在非空格?
I know the encryption produces some non-alphanumeric characters, but are these limited to non-whitespace?
推荐答案
大多数加密算法 - 包括Rijndael - 二进制数据,而不是文本。在二进制数据中没有空格(或非字母数字字符)的概念。
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.
如果您必须将加密数据存储为文本,那么需要将它从二进制数据(例如字节数组)转换为文本。一个简单的方法是使用Base64(例如 Convert.ToBase64String(data)
) - 而Base64从不包含使用标准codabet的空格。
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.
总结
- 如果可能,以二进制形式存储加密数据(例如在IMAGE字段中)
- 如果您将存储在文本字段中,则以适当的方式进行转换,例如使用Base64。 不要使用类似
Encoding.UTF8
的内容,因为加密数据不是 UTF-8编码的文本。 - Base64不包含空格,所以方面不应该是您的问题。
- 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.
这篇关于可以加密使用RijndaelManaged在输出中产生空格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!