在 C# 中使用 AES 加密
我似乎找不到使用 AES 128 位加密的清晰示例。
有人有一些示例代码吗?
I can't seem to find a nice clean example of using AES 128 bit encryption.
Does anyone have some sample code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您只想使用内置加密提供程序 RijndaelManaged,请查看以下帮助文章(它还有一个简单的代码示例):
如果您急需示例,这里是所有抄袭的荣耀:
If you just want to use the built-in crypto provider RijndaelManaged, check out the following help article (it also has a simple code sample):
And just in case you need the sample in a hurry, here it is in all its plagiarized glory:
最近,我不得不在自己的项目中再次遇到这个问题 - 并且想分享我一直在使用的稍微简单的代码,因为这个问题和一系列答案不断出现在我的搜索中。
我不会讨论关于更新Salt和初始化向量等内容的频率的安全问题 - 这是安全论坛的主题,并且有一些那里有很多资源值得一看。 这只是用 C# 实现
AesManaged
的代码块。该代码使用起来非常简单。 它实际上只需要以下内容:
默认情况下,实现使用 AesManaged - 但实际上您也可以插入任何其他
SymmetricAlgorithm
。 .NET 4.5 的可用SymmetricAlgorithm
继承者列表可在以下位置找到:http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetryalgorithm.aspx
截至本文发表时,当前列表包括:
AesManaged
RijndaelManaged
DESCryptoServiceProvider
RC2CryptoServiceProvider
TripleDESCryptoServiceProvider
使用
RijndaelManaged 以上面的代码为例,您可以使用:
我希望这对那里的人有帮助。
I've recently had to bump up against this again in my own project - and wanted to share the somewhat simpler code that I've been using, as this question and series of answers kept coming up in my searches.
I'm not going to get into the security concerns around how often to update things like your Salt and Initialization Vector - that's a topic for a security forum, and there are some great resources out there to look at. This is simply a block of code to implement
AesManaged
in C#.The code is very simple to use. It literally just requires the following:
By default, the implementation uses AesManaged - but you could actually also insert any other
SymmetricAlgorithm
. A list of the availableSymmetricAlgorithm
inheritors for .NET 4.5 can be found at:http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm.aspx
As of the time of this post, the current list includes:
AesManaged
RijndaelManaged
DESCryptoServiceProvider
RC2CryptoServiceProvider
TripleDESCryptoServiceProvider
To use
RijndaelManaged
with the code above, as an example, you would use:I hope this is helpful to someone out there.
查看此处的示例..
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanagement(v=VS.100).aspx#Y2262
MSDN 上的示例无法正常运行(发生错误),因为Initial Vector(iv)和Key没有初始值。 我添加了2行代码,现在可以正常工作了。
更多详情请见下文:
Look at sample in here..
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged(v=VS.100).aspx#Y2262
The example on MSDN does not run normally (an error occurs) because there is no initial value of Initial Vector(iv) and Key. I add 2 line code and now work normally.
More details see below:
使用 AES 还是实施 AES? 要使用 AES,需要使用 System.Security.Cryptography.RijndaelManaged 类。
Using AES or implementing AES? To use AES, there is the System.Security.Cryptography.RijndaelManaged class.
有关除了 AES 加密之外还执行密钥派生的更完整示例,请参阅 让 AES 加密在 Javascript 和 C# 上运行。
编辑
旁注:Javascript 密码学被认为是有害的。值得一读。
For a more complete example that performs key derivation in addition to the AES encryption, see the answer and links posted in Getting AES encryption to work across Javascript and C#.
EDIT
a side note: Javascript Cryptography considered harmful. Worth the read.
http://www .codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt
http://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt
试试这个代码,也许有用。
1.创建新的 C# 项目并将以下代码添加到 Form1:
2.创建 clsCrypto.cs 并将以下代码复制粘贴到您的类中并运行您的代码。 我使用 MD5 生成 AES 的初始向量(IV)和密钥。
Try this code, maybe useful.
1.Create New C# Project and add follows code to Form1:
2.Create clsCrypto.cs and copy paste follows code in your class and run your code. I used MD5 to generated Initial Vector(IV) and KEY of AES.
您可以使用文本框中的密码,例如密钥...
使用此代码,您可以加密/解密文本、图片、word 文档、pdf...
将密码从文本框转换为字节数组...
You can use password from text box like key...
With this code you can encrypt/decrypt text, picture, word document, pdf....
Convert password from text box to byte array...
这是一个整洁干净的代码,用于理解用 C# 实现的 AES 256 算法
调用加密函数为
encryptedstring = cryptObj.Encrypt(username, "AGARAMUDHALA", "EZHUTHELLAM", "SHA1", 3, "@1B2c3D4e5F6g7H8", 256);
here is a neat and clean code to understand AES 256 algorithm implemented in C#
call Encrypt function as
encryptedstring = cryptObj.Encrypt(username, "AGARAMUDHALA", "EZHUTHELLAM", "SHA1", 3, "@1B2c3D4e5F6g7H8", 256);