如何加密中文字符串 [英] How to encrypt a chinese string

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

问题描述



我的项目需要支持中文字符.为此,我必须对中文进行加密

字符串.我在C ++中尝试过,结果长度是8个字符

长度生成64个长度的加密输出,16个字符长度生成128个长度

加密的输出,依此类推.也就是说,加密字符串的长度变大了,

更大.我将在下面说明如何进行加密

(1)使用ToBase64String将中文字符串转换为Base64字符串.

(2)然后使用32个长度的密钥对结果字符串进行加密

这是我的问题.如何使用C#加密中文字符串,应该使用

Hi,

My project need to support chinese character.For this I have to encrypt chinese

string.I tried it in C++ and the resulted length is such a way that 8 character

length generates 64 length encrypted output,16 character length generates 128 length

encrypted output and so on.That is length of the encrypted string goes larger and

larger.I will explain how I did the encryption below

(1)Convert the chinese string to Base64 string using ToBase64String.

(2)Then encrypt the resulted string using a 32 length key

Here is my question.How to encrypt a chinese string using C# which should

generates small length output?

推荐答案

中文字符串?你是什​​么意思?这些天没有中文字符串了.只有Unicode.还是您指的是传统编码之一?

现在,为什么您认为需要将其转换为Base64String?如果我说您需要像通常那样将其转换为字节数组怎么办?使用System.Text.Encoding来做到这一点;请参阅 http://msdn.microsoft.com/en-us/library/system.text .encoding.aspx [ ^ ].当然,您可以使用除System.Text.ASCIIEncoding之外的任何编码.我建议System.Text.UTF8Encoding.

现在,加密.没有诸如使用32位长度的密钥加密结果字符串"之类的东西-有许多算法.首先,在对称算法或非对称算法之间做出选择.请参阅 http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/system.security.cryptography.asymmetricalgorithm.aspx [ http://en.wikipedia.org/wiki/Public-key_cryptography [http://en.wikipedia.org/wiki/Symmetric_key_algorithms [ ^ ].

小尺寸输出"?您听说过没有奇迹吗?听说过信息学吗?请参见 http://en.wikipedia.org/wiki/Informatics_%28academic_field%29 [ ^ ].


我只是想过:如果您需要较小的输出长度,也许您的意思不是加密而是加密哈希函数( http ://en.wikipedia.org/wiki/Cryptographic_hash_function [ ^ ])?这不是加密,而是完全不同的事情.与不可逆散列数据相反,加密数据可以解密,因此只能用于比较(和某些相关目的).如果是这样,我建议您使用SHA系列的算法,请参见 http://en.wikipedia.org/wiki/SHA- 2 [ ^ ].有关.NET的实现,请参见 http://msdn.microsoft.com/zh-我们/library/system.security.cryptography.hashalgorithm.aspx [ ^ ].

—SA
Chinese string? What do you mean by that? There are not Chinese strings these days; there is only Unicode. Or do you mean one of the legacy encodings?

Now, why do you think you need to convert it to Base64String? What if I say that you need to convert it to array of byte, as it is always done? Use System.Text.Encoding to do that; see http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^]. You could use any encoding except System.Text.ASCIIEncoding, of course; I would advise System.Text.UTF8Encoding.

Now, encryption. There is no such thing as "encrypt the resulted string using a 32 length key" — there is a number of algorithms. First, make a choice between symmetric or assymmetic algorithm. Please see http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.aspx[^], http://msdn.microsoft.com/en-us/library/system.security.cryptography.asymmetricalgorithm.aspx[^].

You will need good understanding of the ideas of encryption, which are not trivial.
Start here: http://en.wikipedia.org/wiki/Public-key_cryptography[^] (these algorithms are assymmetric) and http://en.wikipedia.org/wiki/Symmetric_key_algorithms[^].

"Small length output"? Have you ever heard that there is no such thing as miracle? Ever heard of informatics? See http://en.wikipedia.org/wiki/Informatics_%28academic_field%29[^].


I just thought: if you require a small length output, maybe you mean not encryption but cryptographic hash function (http://en.wikipedia.org/wiki/Cryptographic_hash_function[^])? This is not encryption but is a completely different thing. Encrypted data can be decrypted, in contrast to hashed data which is irreversible, so it can be used only for comparison (and some related purposes). If so, I would advise using the algorithm from SHA family, see http://en.wikipedia.org/wiki/SHA-2[^]. For .NET implementation, see http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx[^].

—SA


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

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