.NET中的对称加密/解密 [英] Symmetric encrypt/decrypt in .NET
本文介绍了.NET中的对称加密/解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
更新:我真的很想看到一些代码,而不仅仅是链接。非常感谢!
解决方案
查看此页面。
粘贴在这里:
int Rfc2898KeygenIterations = 100;
int AesKeySizeInBits = 128;
String Password =VerySecret!;
byte [] Salt = new byte [16];
System.Random rnd = new System.Random();
rnd.NextBytes(Salt);
byte [] rawPlaintext = System.Text.Encoding.Unicode.GetBytes(This are all clear now!);
byte [] cipherText = null;
byte [] plainText = null;
使用(Aes aes = new AesManaged())
{
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = AesKeySizeInBits;
int KeyStrengthInBytes = aes.KeySize / 8;
System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 =
new System.Security.Cryptography.Rfc2898DeriveBytes(Password,Salt,Rfc2898KeygenIterations);
aes.Key = rfc2898.GetBytes(KeyStrengthInBytes);
aes.IV = rfc2898.GetBytes(KeyStrengthInBytes);
使用(MemoryStream ms = new MemoryStream())
{
using(CryptoStream cs = new CryptoStream(ms,aes.CreateEncryptor(),CryptoStreamMode.Write))
{
cs.Write(rawPlaintext,0,rawPlaintext.Length);
}
cipherText = ms.ToArray();
}
使用(MemoryStream ms = new MemoryStream())
{
using(CryptoStream cs = new CryptoStream(ms,aes.CreateDecryptor(),CryptoStreamMode。写))
{
cs.Write(cipherText,0,cipherText.Length);
}
plainText = ms.ToArray();
}
}
string s = System.Text.Encoding.Unicode.GetString(plainText);
Console.WriteLine(s);
I am after a symmetric encryption/decryption routine in C#. I know there have been a few questions on this topic before, but most of the answers seem to be about the philosophy of encryption rather than giving actual code.
Update: I'd really like to see some code, rather than just links. Many thanks!
解决方案
Look at the example code at the bottom of this page.
Copy-pasting it here:
int Rfc2898KeygenIterations= 100;
int AesKeySizeInBits = 128;
String Password = "VerySecret!";
byte[] Salt = new byte[16];
System.Random rnd = new System.Random();
rnd.NextBytes(Salt);
byte[] rawPlaintext = System.Text.Encoding.Unicode.GetBytes("This is all clear now!");
byte[] cipherText= null;
byte[] plainText= null;
using (Aes aes = new AesManaged())
{
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = AesKeySizeInBits;
int KeyStrengthInBytes= aes.KeySize/8;
System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 =
new System.Security.Cryptography.Rfc2898DeriveBytes(Password, Salt, Rfc2898KeygenIterations);
aes.Key = rfc2898.GetBytes(KeyStrengthInBytes);
aes.IV = rfc2898.GetBytes(KeyStrengthInBytes);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(rawPlaintext, 0, rawPlaintext.Length);
}
cipherText= ms.ToArray();
}
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherText, 0, cipherText.Length);
}
plainText = ms.ToArray();
}
}
string s = System.Text.Encoding.Unicode.GetString(plainText);
Console.WriteLine(s);
这篇关于.NET中的对称加密/解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文