在c#

时间:2015-07-22 05:52:59

标签: c# encryption aes

我想使用带有ECB和PKCS7Padding的AES 256位加密算法加密字符串。我经历了很多网站,但没有一个适合。 请提出解决方案

2 个答案:

答案 0 :(得分:1)

public static string Encrypt(string PlainText, string Password,
               string Salt = "Kosher", string HashAlgorithm = "SHA1",
               int PasswordIterations = 2, string InitialVector = "OFRna73m*aze01xY",
               int KeySize = 256)
           {
               if (string.IsNullOrEmpty(PlainText))
                   return "";
               byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
               byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
               byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);
               PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
               byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
               RijndaelManaged SymmetricKey = new RijndaelManaged();
               SymmetricKey.Mode = CipherMode.CBC;
               byte[] CipherTextBytes = null;
               using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes))
               {
                   using (MemoryStream MemStream = new MemoryStream())
                   {
                       using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
                       {
                           CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
                           CryptoStream.FlushFinalBlock();
                           CipherTextBytes = MemStream.ToArray();
                           MemStream.Close();
                           CryptoStream.Close();
                       }
                   }
               }
               SymmetricKey.Clear();
               return Convert.ToBase64String(CipherTextBytes);
           }

source

注意:您可以通过更改SymmetricKey.Mode = CipherMode.CBC;

来更改模式

您可以为填充添加SymmetricKey.Padding = PaddingMode.PKCS7;

答案 1 :(得分:0)

bounty castle你应该可以这样做:

{        
    region: 'east',        
    title: 'East Panel',        
    collapsible: true,        
    split: true,        
    width: 150,
    header: {
         items: [{
            xtype: 'button'
        }, {
            xtype: 'button'
        }]
    }
}