SQL 128中的AES 128 Encription或ASP.Net

时间:2017-11-03 10:28:37

标签: c# asp.net sql-server-2008

我正在尝试创建一个需要以AES 128加密格式返回数据的Web服务。有人请帮助我,我怎样才能做到这一点。

提前感谢。

1 个答案:

答案 0 :(得分:-1)

public class EncryptionAES128
    {
     static public string EncryptString(string inputString, string key)
            {
                string output = "";


                Rijndael encryption = new RijndaelManaged();

                try
                {
                    encryption.IV = GenerateIV();
                    encryption.Key = StringToByte(key);

                    ICryptoTransform encryptor = encryption.CreateEncryptor(encryption.Key, encryption.IV);

                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                swEncrypt.Write(inputString);
                            }

                            byte[] cipherText = msEncrypt.ToArray();
                            byte[] encrypted = new byte[cipherText.Length + encryption.IV.Length];

                            encryption.IV.CopyTo(encrypted, 0);
                            cipherText.CopyTo(encrypted, IV_LENGTH);

                            output = ByteToString(encrypted);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }


                return output;