可以在公共网站上解密使用哪些C#AES加密选项,以便将结果解密? [英] What C# AES encryption options to use so result can be decrypted on a public web site?
问题描述
我想使用类似于下面的代码对字符串进行加密,并能够使用诸如以下其中一个的可公开访问的网站对其进行解密(但也可以向其他网站开放):
I would like to encrypt a string using code similar to the below and be able to decrypt it using a publicly available website such as one of these (but am open to some other site out there as well):
http: //www.nakov.com/blog/2011/12/26/online-aes-encryptor-decryptor-javascript/
这是我的代码我目前正在这些网站上试用并尝试,但我找不到能够使这项工作成功的选项。有什么想法吗?
Here is the code I'm currently playing around with and trying on these sites, I haven't been able to find the options that will make this work. Any ideas?
public class AES
{
private readonly int _saltSize = 32;
public string Encrypt(string plainText, string key)
{
if (string.IsNullOrEmpty(plainText))
{
throw new ArgumentNullException("plainText");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key");
}
using (var keyDerivationFunction = new Rfc2898DeriveBytes(key, _saltSize))
{
byte[] saltBytes = keyDerivationFunction.Salt;
byte[] keyBytes = keyDerivationFunction.GetBytes(32);
byte[] ivBytes = keyDerivationFunction.GetBytes(16);
using (var aesManaged = new AesManaged())
{
aesManaged.KeySize = 256;
using (var encryptor = aesManaged.CreateEncryptor(keyBytes, ivBytes))
{
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
return WriteMemoryStream(plainText, ref saltBytes, encryptor, ref memoryStream, ref cryptoStream);
}
}
}
}
public string Decrypt(string ciphertext, string key)
{
if (string.IsNullOrEmpty(ciphertext))
{
throw new ArgumentNullException("ciphertext");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key");
}
var allTheBytes = Convert.FromBase64String(ciphertext);
var saltBytes = allTheBytes.Take(_saltSize).ToArray();
var ciphertextBytes = allTheBytes.Skip(_saltSize).Take(allTheBytes.Length - _saltSize).ToArray();
using (var keyDerivationFunction = new Rfc2898DeriveBytes(key, saltBytes))
{
var keyBytes = keyDerivationFunction.GetBytes(32);
var ivBytes = keyDerivationFunction.GetBytes(16);
return DecryptWithAES(ciphertextBytes, keyBytes, ivBytes);
}
}
private string WriteMemoryStream(string plainText, ref byte[] saltBytes, ICryptoTransform encryptor, ref MemoryStream memoryStream, ref CryptoStream cryptoStream)
{
try
{
memoryStream = new MemoryStream();
try
{
cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
using (var streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(plainText);
}
}
finally
{
if (cryptoStream != null)
{
cryptoStream.Dispose();
}
}
var cipherTextBytes = memoryStream.ToArray();
Array.Resize(ref saltBytes, saltBytes.Length + cipherTextBytes.Length);
Array.Copy(cipherTextBytes, 0, saltBytes, _saltSize, cipherTextBytes.Length);
return Convert.ToBase64String(saltBytes);
}
finally
{
if (memoryStream != null)
{
memoryStream.Dispose();
}
}
}
private static string DecryptWithAES(byte[] ciphertextBytes, byte[] keyBytes, byte[] ivBytes)
{
using (var aesManaged = new AesManaged())
{
using (var decryptor = aesManaged.CreateDecryptor(keyBytes, ivBytes))
{
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
StreamReader streamReader = null;
try
{
memoryStream = new MemoryStream(ciphertextBytes);
cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
streamReader = new StreamReader(cryptoStream);
return streamReader.ReadToEnd();
}
finally
{
if (memoryStream != null)
{
memoryStream.Dispose();
memoryStream = null;
}
}
}
}
}
}
推荐答案
以下代码适用于使用 abcdefghijklmnop密钥加密字符串 this is a test,然后在站点 http://aesencryption.net/
The below code works for encrypting the string "this is a test" with a key of "abcdefghijklmnop" and then decrypting at the site http://aesencryption.net/
static void test()
{
Console.WriteLine(Convert.ToBase64String(EncryptStringToBytes("this is a test", System.Text.Encoding.ASCII.GetBytes("abcdefghijklmnop"))));
}
static byte[] EncryptStringToBytes(string plainText, byte[] key)
{
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
byte[] encrypted;
using (var rijAlg = new RijndaelManaged())
{
rijAlg.BlockSize = 256;
rijAlg.Key = key;
rijAlg.Mode = CipherMode.ECB;
rijAlg.Padding = PaddingMode.Zeros;
rijAlg.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
using (var msEncrypt = new MemoryStream())
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
swEncrypt.Write(plainText);
encrypted = msEncrypt.ToArray();
}
}
return encrypted;
}
这篇关于可以在公共网站上解密使用哪些C#AES加密选项,以便将结果解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!