C#实现AES加解密,验证同Cryptopp561的一致性成功

    /*
     *  CFB模式的AES加解密,经测试同Crypt561带的AES加解密算法运算结果一致。
     *  经测试,可以正确加解密中文形式的明文
     *                                                --by kagula 2012-7
     */
    class CSSecurity
    {
        //默认密码
        private static String Key = "0123456789012345";

        //默认IV
        private static Byte[] IV = Enumerable.Repeat((byte)0x30, 16).ToArray();

        //产生含n个空格的字符串
        private static String padding(int n)
        {
            StringBuilder s = new StringBuilder();

            for (int i = 0; i < n; i++)
                s.Append(" ");
                

            return s.ToString();
        }

        //二进制数据转16进制字符串
        /*
        public static String Byte2Str(Byte[] arrayB)
        {
            StringBuilder sb = new StringBuilder();
            String s2;

            foreach (Byte b in arrayB)
            {
                s2 = b.ToString("X");
                if (s2.Length == 1)
                    sb.Append("0");

                sb.Append(s2);
            }
            return sb.ToString();
        }
        */

        //设置AES加密所需16/24/32位密匙
        /*
         * 密码太长则截取开头的32个字符,密码太短就用空格填充。
         */
        public static void setKey_AES(String key)
        {
            if (key.Length >= 32)
                key = key.Substring(0, 32);
            else if (key.Length >= 24)
                key = key.Substring(0, 24);
            else if (key.Length >= 16)
                key = key.Substring(0, 16);
            else
                key = key + padding(16-key.Length);
        }

        //输入明文返回密文
        public static String Encrypt_CFB_AES(String str)
        {
            Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(Key);
            Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str); 
            System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();

            rDel.Key = keyArray;
            rDel.Mode = System.Security.Cryptography.CipherMode.CFB;
            rDel.Padding = System.Security.Cryptography.PaddingMode.Zeros;
            rDel.IV = IV;

            System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();

            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        //输入密文返回明文
        public static String Decrypt_CFB_AES(String str)
        {
            Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(Key);
            Byte[] toEncryptArray = Convert.FromBase64String(str); 
            
            System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();

            rDel.Key = keyArray;
            rDel.Mode = System.Security.Cryptography.CipherMode.CFB;
            rDel.Padding = System.Security.Cryptography.PaddingMode.Zeros;
            rDel.IV = IV;
            
            System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return System.Text.UTF8Encoding.UTF8.GetString(resultArray);
        }

        //返回文摘(digest),SHA256算法
        public static string Digest_SHA256(string str)
        {
            System.Security.Cryptography.SHA256 s256 = new System.Security.Cryptography.SHA256Managed();
            byte[] byte1;
            byte1 = s256.ComputeHash(Encoding.Default.GetBytes(str));
            s256.Clear();
            return Convert.ToBase64String(byte1);
        }
    }


你可能感兴趣的:(加密,String,解密,C#,测试,byte)