使用什么 C# AES 加密选项可以在 public 网站上解密结果?
What C# AES encryption options to use so result can be decrypted on a public web site?
我想使用类似于下面的代码加密字符串,并能够使用 public 仅可用的网站(例如其中之一)对其进行解密(但我对其他网站开放,因为嗯):
http://www.nakov.com/blog/2011/12/26/online-aes-encryptor-decryptor-javascript/
这是我目前正在使用并尝试在这些网站上使用的代码,但我一直无法找到使这项工作可行的选项。有什么想法吗?
public class AES
{
私人只读 int _saltSize = 32;
public 字符串加密(字符串纯文本,字符串密钥)
{
如果 (string.IsNullOrEmpty(纯文本))
{
抛出新的 ArgumentNullException("plainText");
}
如果(string.IsNullOrEmpty(键))
{
抛出新的 ArgumentNullException("key");
}
使用 (var keyDerivationFunction = new Rfc2898DeriveBytes(key, _saltSize))
{
byte[] saltBytes = keyDerivationFunction.Salt;
byte[] keyBytes = keyDerivationFunction.GetBytes(32);
byte[] ivBytes = keyDerivationFunction.GetBytes(16);
使用 (var aesManaged = new AesManaged())
{
aesManaged.KeySize = 256;
使用 (var encryptor = aesManaged.CreateEncryptor(keyBytes, ivBytes))
{
内存流 memoryStream = null;
CryptoStream cryptoStream = null;
return WriteMemoryStream(纯文本,ref saltBytes,加密器,ref memoryStream,ref cryptoStream);
}
}
}
}
public string Decrypt(字符串密文,字符串密钥)
{
如果(string.IsNullOrEmpty(密文))
{
抛出新的 ArgumentNullException("ciphertext");
}
如果(string.IsNullOrEmpty(键))
{
抛出新的 ArgumentNullException("key");
}
var allTheBytes = Convert.FromBase64String(密文);
var saltBytes = allTheBytes.Take(_saltSize).ToArray();
var ciphertextBytes = allTheBytes.Skip(_saltSize).Take(allTheBytes.Length - _saltSize).ToArray();
使用 (var keyDerivationFunction = new Rfc2898DeriveBytes(key, saltBytes))
{
var keyBytes = keyDerivationFunction.GetBytes(32);
var ivBytes = keyDerivationFunction.GetBytes(16);
return DecryptWithAES(ciphertextBytes, keyBytes, ivBytes);
}
}
私有字符串 WriteMemoryStream(字符串纯文本,ref byte [] saltBytes,ICryptoTransform 加密器,ref MemoryStream memoryStream,ref CryptoStream cryptoStream)
{
尝试
{
内存流 = 新内存流 ();
尝试
{
cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
使用 (var streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(纯文本);
}
}
最后
{
如果(加密流!= 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);
}
最后
{
如果(内存流!=空)
{
memoryStream.Dispose();
}
}
}
私有静态字符串 DecryptWithAES(byte[] ciphertextBytes, byte[] keyBytes, byte[] ivBytes)
{
使用 (var aesManaged = new AesManaged())
{
使用 (var decryptor = aesManaged.CreateDecryptor(keyBytes, ivBytes))
{
内存流 memoryStream = null;
CryptoStream cryptoStream = null;
StreamReader streamReader = null;
尝试
{
memoryStream = new MemoryStream(ciphertextBytes);
cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
streamReader = new StreamReader(cryptoStream);
return streamReader.ReadToEnd();
}
最后
{
如果(内存流!=空)
{
memoryStream.Dispose();
内存流=空;
}
}
}
}
}
}
下面的代码用于使用密钥 "abcdefghijklmnop" 加密字符串 "this is a test",然后在 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;
}
我想使用类似于下面的代码加密字符串,并能够使用 public 仅可用的网站(例如其中之一)对其进行解密(但我对其他网站开放,因为嗯):
http://www.nakov.com/blog/2011/12/26/online-aes-encryptor-decryptor-javascript/
这是我目前正在使用并尝试在这些网站上使用的代码,但我一直无法找到使这项工作可行的选项。有什么想法吗?
public class AES { 私人只读 int _saltSize = 32; public 字符串加密(字符串纯文本,字符串密钥) { 如果 (string.IsNullOrEmpty(纯文本)) { 抛出新的 ArgumentNullException("plainText"); } 如果(string.IsNullOrEmpty(键)) { 抛出新的 ArgumentNullException("key"); } 使用 (var keyDerivationFunction = new Rfc2898DeriveBytes(key, _saltSize)) { byte[] saltBytes = keyDerivationFunction.Salt; byte[] keyBytes = keyDerivationFunction.GetBytes(32); byte[] ivBytes = keyDerivationFunction.GetBytes(16); 使用 (var aesManaged = new AesManaged()) { aesManaged.KeySize = 256; 使用 (var encryptor = aesManaged.CreateEncryptor(keyBytes, ivBytes)) { 内存流 memoryStream = null; CryptoStream cryptoStream = null; return WriteMemoryStream(纯文本,ref saltBytes,加密器,ref memoryStream,ref cryptoStream); } } } } public string Decrypt(字符串密文,字符串密钥) { 如果(string.IsNullOrEmpty(密文)) { 抛出新的 ArgumentNullException("ciphertext"); } 如果(string.IsNullOrEmpty(键)) { 抛出新的 ArgumentNullException("key"); } var allTheBytes = Convert.FromBase64String(密文); var saltBytes = allTheBytes.Take(_saltSize).ToArray(); var ciphertextBytes = allTheBytes.Skip(_saltSize).Take(allTheBytes.Length - _saltSize).ToArray(); 使用 (var keyDerivationFunction = new Rfc2898DeriveBytes(key, saltBytes)) { var keyBytes = keyDerivationFunction.GetBytes(32); var ivBytes = keyDerivationFunction.GetBytes(16); return DecryptWithAES(ciphertextBytes, keyBytes, ivBytes); } } 私有字符串 WriteMemoryStream(字符串纯文本,ref byte [] saltBytes,ICryptoTransform 加密器,ref MemoryStream memoryStream,ref CryptoStream cryptoStream) { 尝试 { 内存流 = 新内存流 (); 尝试 { cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); 使用 (var streamWriter = new StreamWriter(cryptoStream)) { streamWriter.Write(纯文本); } } 最后 { 如果(加密流!= 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); } 最后 { 如果(内存流!=空) { memoryStream.Dispose(); } } } 私有静态字符串 DecryptWithAES(byte[] ciphertextBytes, byte[] keyBytes, byte[] ivBytes) { 使用 (var aesManaged = new AesManaged()) { 使用 (var decryptor = aesManaged.CreateDecryptor(keyBytes, ivBytes)) { 内存流 memoryStream = null; CryptoStream cryptoStream = null; StreamReader streamReader = null; 尝试 { memoryStream = new MemoryStream(ciphertextBytes); cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); streamReader = new StreamReader(cryptoStream); return streamReader.ReadToEnd(); } 最后 { 如果(内存流!=空) { memoryStream.Dispose(); 内存流=空; } } } } } }
下面的代码用于使用密钥 "abcdefghijklmnop" 加密字符串 "this is a test",然后在 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;
}