예제 #1
0
        private static byte[] Encrypt(byte[] input, byte[] key, int keySize, System.Security.Cryptography.CipherMode mode, byte[] iv = null)
        {
            using (var aesEncryption = new RijndaelManaged())
            {
                if (iv == null)
                {
                    aesEncryption.GenerateIV();
                }
                else
                {
                    aesEncryption.IV = iv;
                }

                aesEncryption.KeySize   = keySize;
                aesEncryption.BlockSize = Crypto.DefaultBlocklength * 8;
                aesEncryption.Mode      = mode;
                aesEncryption.Padding   = PaddingMode.PKCS7;
                aesEncryption.Key       = key;
                ICryptoTransform crypto = aesEncryption.CreateEncryptor();

                // The result of the encryption and decryption
                byte[] cipherText = crypto.TransformFinalBlock(input, 0, input.Length);
                var    result     = new byte[cipherText.Length + aesEncryption.IV.Length];
                Buffer.BlockCopy(aesEncryption.IV, 0, result, 0, aesEncryption.IV.Length);
                Buffer.BlockCopy(cipherText, 0, result, aesEncryption.IV.Length, cipherText.Length);
                return(result);
            }
        }
예제 #2
0
        static byte[] Decrypt(byte[] input, byte[] key, int keySize, System.Security.Cryptography.CipherMode mode)
        {
            byte[] iv = input.Take(Crypto.DefaultBlocklength).ToArray();
            using (var aesEncryption = new RijndaelManaged())
            {
                aesEncryption.KeySize   = keySize;
                aesEncryption.BlockSize = Crypto.DefaultBlocklength * 8;
                aesEncryption.Mode      = mode;
                aesEncryption.Padding   = PaddingMode.PKCS7;
                aesEncryption.IV        = iv;
                aesEncryption.Key       = key;

                ICryptoTransform decrypt = aesEncryption.CreateDecryptor();
                var encryptedBuffer      = input.Skip(Crypto.DefaultBlocklength).ToArray();
                return(decrypt.TransformFinalBlock(encryptedBuffer, 0, encryptedBuffer.Length));
            }
        }
예제 #3
0
        void CipherModeChanged(object sender, RoutedEventArgs e)
        {
            cipherModeNumber = CipherMode.SelectedIndex;
            System.Security.Cryptography.CipherMode mode = System.Security.Cryptography.CipherMode.CBC;

            switch (cipherModeNumber)
            {
            case 0: { mode = System.Security.Cryptography.CipherMode.CBC; break; }

            case 1: { mode = System.Security.Cryptography.CipherMode.ECB; break; }

            case 2: { mode = System.Security.Cryptography.CipherMode.OFB; break; }

            case 3: { mode = System.Security.Cryptography.CipherMode.CFB; break; }

            case 4: { mode = System.Security.Cryptography.CipherMode.CTS; break; }
            }
        }
예제 #4
0
        /// <summary>
        /// 3DES解密
        /// </summary>
        /// <param name="decryptSource">解密的数据源</param>
        /// <param name="decryptKey">解密的密匙</param>
        /// <param name="decryptIV">解密的矢量</param>
        /// <param name="paddingMode">填充模式</param>
        /// <param name="byteNum">字节</param>
        ///  <param name="outType">解密字节形式ToHex16,ToBase64</param>
        /// <returns>解密串</returns>
        public static string TripleDESDecrypt(string decryptSource, string decryptKey, string decryptIV, System.Security.Cryptography.PaddingMode paddingMode, int byteNum, string outType, System.Security.Cryptography.CipherMode mode = System.Security.Cryptography.CipherMode.ECB)
        {
            //构造一个对称算法
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

            ICryptoTransform ct;
            MemoryStream     ms;
            CryptoStream     cs;

            byte[] byt = new byte[0];

            if (string.IsNullOrEmpty(outType))
            {
                return("解密配置不能空");
            }
            if ((decryptKey.Trim().Length) != byteNum)
            {
                return("解密字节错误");
            }

            byte[] Key = System.Text.Encoding.Default.GetBytes(decryptKey.Trim());

            mCSP.Key = Key;
            //默认矢量
            if (String.IsNullOrEmpty(decryptIV))
            {
                decryptIV = decryptKey.Substring(0, 8);
            }
            mCSP.IV = System.Text.Encoding.Default.GetBytes(decryptIV);

            mCSP.Mode    = mode;
            mCSP.Padding = paddingMode;
            ct           = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);

            //输出16进制字符
            if (outType == "ToHex16")
            {
                byt = ConvertHexToBytes(decryptSource);
            }
            //输出ToBase64字符
            if (outType == "ToBase64")
            {
                byt = Convert.FromBase64String(decryptSource);
            }
            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();

            return(Encoding.Default.GetString(ms.ToArray()).Replace("\0", "").Trim());
        }
예제 #5
0
        /// <summary>
        /// 3DES加密自定义字节位填充类型
        /// </summary>
        /// <param name="encryptSource">加密的数据源</param>
        /// <param name="encryptKey">加密的密匙</param>
        /// <param name="encryptIV">加密的矢量</param>
        /// <param name="paddingMode">自定义填充类型</param>
        /// <param name="byteNum">自定义字节数</param>
        /// <param name="outType">输出字节形式ToHex16,ToBase64</param>
        /// <returns>加密串</returns>
        public static string TripleDESEncrypt(string encryptSource, string encryptKey, string encryptIV, System.Security.Cryptography.PaddingMode paddingMode, int byteNum, string outType, System.Security.Cryptography.CipherMode mode = System.Security.Cryptography.CipherMode.ECB)
        {
            //构造一个对称算法
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

            ICryptoTransform ct;
            MemoryStream     ms;
            CryptoStream     cs;

            byte[] byt;

            if (string.IsNullOrEmpty(outType))
            {
                return("输出配置不能空");
            }
            if ((encryptKey.Trim().Length) != byteNum)
            {
                return("加密字节");
            }
            byte[] Key = System.Text.Encoding.Default.GetBytes(encryptKey);
            mCSP.Key = Key;
            //默认矢量
            if (String.IsNullOrEmpty(encryptIV))
            {
                encryptIV = encryptKey.Substring(0, 8);
            }
            mCSP.IV = System.Text.Encoding.Default.GetBytes(encryptIV);

            //指定加密的运算模式
            mCSP.Mode = mode;
            //获取或设置加密算法的填充模式
            mCSP.Padding = paddingMode;

            ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);

            byt = System.Text.Encoding.Default.GetBytes(encryptSource.Trim());

            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();

            string mToString = string.Empty;

            //输出16进制字符
            if (outType == "ToHex16")
            {
                mToString = ToHexString(ms.ToArray());
            }
            //输出ToBase64字符
            if (outType == "ToBase64")
            {
                mToString = Convert.ToBase64String(ms.ToArray()).ToString().Replace("\0", "");
            }

            return(mToString);
        }