加密32位字符 [英] Encrypt 32 bit character

查看:151
本文介绍了加密32位字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何使用C#加密32位字符
在此先感谢

OP作为评论发布:

我的意思是char数据类型.英语是1字节.我认为在其他语言(例如中文)中可能是2字节或最大4字节.是否正确.我需要加密中文密码(即2字节)或最多4个字节(不确定).所以我使用了如下所示的代码

Hi,

How can I encrypt a 32 bit character using C#

Thanks in advance

The OP posted as a comment:

I mean char datatype.In english it is 1 byte.I think in some other languages such as chinese it may be 2 byte or max 4 byte.Is it correct or not.I need to encrypt password which is in chinese(ie 2 byte or max 4 byte.not sure).So I used the code as shown below

crypto.Key = ASCIIEncoding.Default.GetBytes("UDNAHNNU");
                crypto.IV = ASCIIEncoding.Default.GetBytes("UDNAHNNU");
                byte[] inputByte = ASCIIEncoding.Default.GetBytes("オープンコンテント");
                ICryptoTransform Encryptor = crypto.CreateEncryptor();
                byte[] outputByte = Encryptor.TransformFinalBlock(inputByte, 0, inputByte.Length);
                strout = Convert.ToBase64String(outputByte);




它成功提供了加密的字符串.之后,我尝试使用以下代码解密加密的值




It successfully gave the encrypted string.After that I tried to decrypt the encrypted value using the below code

byte[] inpt = Convert.FromBase64String(richTextBox2.Text);
            crypto.Key = ASCIIEncoding.Default.GetBytes("UDNAHNNU");
            ICryptoTransform Decryptor = crypto.CreateDecryptor();
            byte[] oupt = Decryptor.TransformFinalBlock(inpt, 0, inpt.Length);
            richTextBox1.Text = ASCIIEncoding.Default.GetString(oupt);



但它返回"??????"像这样.请帮帮我.

[edit]添加了OP其他信息-OriginalGriff [/edit]



But it returns "??????" like this.Please help me.

[edit]OP additional info added - OriginalGriff[/edit]

推荐答案

您要问的内容有一些问题:
1)什么是"32位字符"? char数据类型是16位,ASCII字符是7或8位. UTF-32是可能的,但实际上很少见.
2)为什么要尝试加密一个32位值?您想怎么办?由于缺少输入数据,因此任何加密都不大可能很强.
3)您是在谈论Encoding字符,而不是Encrypting吗?

OP发布了更多信息.


啊!完全不加密.对其进行哈希处理.
这里有一个提示/技巧,提供了基础知识:密码存储:操作方法. [^ ]
There are a few problems with what you are asking:
1) What is a "32 bit character"? The char datatype is 16 bit, ASCII characters are 7 or 8 bit. UTF-32 is possible, but incredibly rare in practice.
2) Why are you trying to encrypt a single 32 bit value? What do you want to do with it? Since there is a lack of input data, any encryption is unlikely to be strong.
3) Are you talking about Encoding characters, rather than Encrypting?

OP posted further info.


Ah! Don''t encrypt it at all. Hash it.
There is a Tip/Trick giving the basics here: Password Storage: How to do it.[^]


首先:严格来说,字符不是8位,16位或32位.字符的特征在于其Unicode代码点,该Unicode代码点是从抽象数学意义上理解的整数,而与计算机中的二进制表示形式,字节序等无关.机器表示形式由Unicode UTF定义( Unicode转换格式).

对于.NET,使用UTF-16编码. BOM以外的字符(基本多语言平原,代码点0到0xFFFF)由代理对(两个16位字)表示.

参见:
http://unicode.org/ [ ^ ],
http://unicode.org/faq/utf_bom.html [ http://msdn.microsoft.com/en-us/library/system.text .encoding.aspx [ ^ ].

对于任何Unicode字符串,无论字符适合BMP还是不在BMP中,四种编码都是不错的选择:System.Text.UnicodeEncoding(UTF-16,实际上是丑陋的Windows行话),System.Text.UTF32EncodingSystem.Text.UTF7Encoding(已经过时了) )和System.Text.UTF8Encoding(最常用于流,互操作,Web等)

明确了Unicode字符串的序列化主题后,我们就可以进行加密了.

有关加密主题,请参见System.Security.Cryptography命名空间: http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.aspx [
First of all: strictly speaking, a character is not 8, 16 or 32-bit. A character is characterized by its Unicode code point, which is an integer understood in its abstract mathematical sense, regardless of its binary presentation in a computer, endianess, etc. The machine presentation is defined by Unicode UTFs (Unicode Transformation Format).

With .NET, UTF-16 encoding is used. The characters beyond BOM (Base Multilingual Plain, code points 0 to 0xFFFF) are represented by surrogate pairs, two 16-bit words.

See:
http://unicode.org/[^],
http://unicode.org/faq/utf_bom.html[^].

Now, encryption is performed in a way totally agnostic to the structure of data. All the data is presented as array of bytes. So, the first step — obtaining array of bytes, is performed by System.Text.Encoding.GetBytes. It''s important, that after decryption the same encoding was used to call GetChars method. See http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^].

For any Unicode strings, four encodings are good, no matter if characters fit in BMP or go outside of it: System.Text.UnicodeEncoding (UTF-16, in fact — ugly Windows jargon), System.Text.UTF32Encoding, System.Text.UTF7Encoding (pretty much outdated) and System.Text.UTF8Encoding (most used for stream, interop, Web, etc.)

When a topic of serialization of Unicode strings is clear, we''re ready for encryption.

For encryption topics, see System.Security.Cryptography namespace: http://msdn.microsoft.com/en-us/library/system.security.cryptography.aspx[^].

—SA


这篇关于加密32位字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆