求助!需要AES 128 对称算法加密程序例子,要求CTR模式的。哪位好心人肯帮小弟一把?不胜感激呀!

解决方案 »

  1.   

    微软有个RijndaelManaged类,就是AES加密的 
    http://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rijndaelmanaged.aspx简单实现代码如下: /// <summary>
            /// 获取密钥
            /// </summary>
            public static string Key
            {
                get { return @"aabbccddd"; }
            }        /// <summary>
            /// 获取向量
            /// </summary>
            public static string IV
            {
                get { return @"aabbccddeeffgghh"; }
            }
            public static string encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
            {
                MemoryStream msEncrypt = null;
                RijndaelManaged aesAlg = null;            try
                {
                    aesAlg = new RijndaelManaged();
                    //aesAlg.Key = Key;
                    //aesAlg.Key = Key;
                    if (Key.Length < 32)
                    {
                        byte[] tt = new byte[32];
                        for (int i = 0; i < 32; i++)
                        {
                            tt[i] = 0;
                            if (i < Key.Length)
                                tt[i] = Key[i];                    }
                        aesAlg.Key = tt;
                    }
                    aesAlg.IV = IV;                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);                msEncrypt = new MemoryStream();
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(plainText);
                        }
                    }
                    byte[] enstr = msEncrypt.ToArray();
                    msEncrypt.Flush();
                    msEncrypt.Close();
                    return Convert.ToBase64String(enstr);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (aesAlg != null)
                        aesAlg.Clear();
                }
            }        public static string decryptStringFromBytes_AES(string encryptStr, byte[] Key, byte[] IV)
            {
                RijndaelManaged aesAlg = null;
                string plaintext = null;            try
                {
                    aesAlg = new RijndaelManaged();
                    byte[] cipherText = Convert.FromBase64String(encryptStr);
                    //aesAlg.Key = Key;
                    if (Key.Length < 32)
                    {
                        byte[] tt = new byte[32];
                        for (int i = 0; i < 32; i++)
                        {
                            tt[i] = 0;
                            if (i < Key.Length)
                                tt[i] = Key[i];                    }
                        aesAlg.Key = tt;
                    }
                    aesAlg.IV = IV;                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                                plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (aesAlg != null)
                        aesAlg.Clear();
                }            return plaintext;
            }
      

  2.   

    我也看过RijndaelManaged类,但怎么在里面找不到CTR模式的啊?只有CBC、CFB、CTS、ECB、OFB五种模式
    。我该怎么办呢?
      

  3.   

    之前搞错了,现在确定是要ECB模式的,我的代码如下:
                RijndaelManaged aes = new RijndaelManaged();
                ICryptoTransform transform = aes.CreateEncryptor();
                aes.Mode = _cipherMode;//ECB
                aes.Padding = _paddingMode;//PKCS7
                aes.KeySize = 128;
                aes.Key = GetKeyArray(password);//16字节
                byte[] data = transform.TransformFinalBlock(inputData, 0, inputData.Length);
                
                aes.Clear();
                return data;为何返回的data 是32字节的?我第一次接触加密,请高手指教啊!