使用哪种C#AES加密选项可以在公共网站上解密?

时间:2015-01-29 15:06:41

标签: c# aes

我想使用类似于下面的代码对字符串进行加密,并且能够使用公开的网站(例如其中一个)对其进行解密(但也可以在其他网站上开放):

http://aesencryption.net/

http://www.nakov.com/blog/2011/12/26/online-aes-encryptor-decryptor-javascript/

以下是我目前正在玩这些网站并尝试使用这些网站的代码,我还没有找到能够让这项工作成功的选项。有什么想法吗?

        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;
                            }
                        }
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

以下代码适用于使用“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;
    }