private static string encryptDecrypt(string _inputText, string _encryptionKey, EncryptMode _mode, string _initVector)
        {
            string _out = "";            // output string

            //_encryptionKey = MD5Hash (_encryptionKey);

            using (RijndaelManaged _rcipher = GetProvider(_encryptionKey, _initVector))
            {
                UTF8Encoding _enc = new UTF8Encoding();
                try
                {
                    if (_mode.Equals(EncryptMode.ENCRYPT))
                    {
                        //encrypt
                        byte[] plainText = _rcipher.CreateEncryptor().TransformFinalBlock(_enc.GetBytes(_inputText), 0, _inputText.Length);
                        _out = Convert.ToBase64String(plainText);
                    }
                    if (_mode.Equals(EncryptMode.DECRYPT))
                    {
                        //decrypt
                        byte[] plainText = _rcipher.CreateDecryptor().TransformFinalBlock(Convert.FromBase64String(_inputText), 0, Convert.FromBase64String(_inputText).Length);
                        _out = _enc.GetString(plainText);
                    }
                }
                catch (Exception ex) { throw; }
                finally { _rcipher.Dispose(); }
            }

            return(_out);           // return encrypted/decrypted string
        }
示例#2
0
            private String encryptDecrypt(string _inputText, string _encryptionKey, EncryptMode _mode, string _initVector)
            {
                string _out = "";

                //get hashed pin code to bytes array
                _pwd = Encoding.UTF8.GetBytes(_encryptionKey);
                //get IV to byte array
                _ivBytes = Encoding.UTF8.GetBytes(_initVector);

                //check that the IV and hashed key are the correct size in bytes
                int len = _pwd.Length;

                if (len > _key.Length)
                {
                    len = _key.Length;
                }
                int ivLenth = _ivBytes.Length;

                if (ivLenth > _iv.Length)
                {
                    ivLenth = _iv.Length;
                }

                //copy hashed pincode and IV bytes to AES key and IV
                Array.Copy(_pwd, _key, len);
                Array.Copy(_ivBytes, _iv, ivLenth);
                _rcipher.Key = _key;
                _rcipher.IV  = _iv;

                //encrypt mode
                if (_mode.Equals(EncryptMode.ENCRYPT))
                {
                    //encrypt the password
                    byte[] plainText = _rcipher.CreateEncryptor().TransformFinalBlock(_enc.GetBytes(_inputText),
                                                                                      0, _inputText.Length);
                    //combine the ciphertext and the IV so we can use it later
                    byte[] combined = new byte[_iv.Length + plainText.Length];
                    System.Buffer.BlockCopy(_iv, 0, combined, 0, _iv.Length);
                    System.Buffer.BlockCopy(plainText, 0, combined, _iv.Length, plainText.Length);
                    //convert ciphertext bytes to hex
                    _out = Convert.ToBase64String(combined);
                }

                //decrypt mode
                if (_mode.Equals(EncryptMode.DECRYPT))
                {
                    //decrypt the ciphertext back to plaintext using the same IV
                    byte[] plainText = _rcipher.CreateDecryptor().TransformFinalBlock(Convert.FromBase64String(_inputText),
                                                                                      0, Convert.FromBase64String(_inputText).Length);
                    //convert plaintext bytes back to hex
                    String outputToString = _enc.GetString(plainText);
                    //remove IV from plaintext
                    String trimmed = outputToString.Remove(0, _iv.Length - 1);
                    _out = trimmed;
                }
                _rcipher.Dispose();
                return(_out);
            }
        /***
         * This function decrypts the encrypted text to plain text using the key
         * provided. You'll have to use the same key which you used during
         * encryption
         *
         * @param _encryptedText
         *            Encrypted/Cipher text to be decrypted
         * @param _key
         *            Encryption key which you used during encryption
         */

        private string encryptDecrypt(string _inputText, string _encryptionKey, EncryptMode _mode, string _initVector)
        {
            SymmetricKeyAlgorithmProvider aesCbcPkcs7 = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);

            // Creata a 16 byte initialization vector
            IBuffer iv = CryptographicBuffer.ConvertStringToBinary(_initVector, BinaryStringEncoding.Utf8);

            // Create an AES 128-bit (16 byte) key
            IBuffer          keyMaterial = CryptographicBuffer.ConvertStringToBinary(_encryptionKey, BinaryStringEncoding.Utf8);
            CryptographicKey key         = aesCbcPkcs7.CreateSymmetricKey(keyMaterial);

            if (_mode.Equals(EncryptMode.ENCRYPT))
            {
                // Encrypt the data
                IBuffer plainText      = CryptographicBuffer.ConvertStringToBinary(_inputText, BinaryStringEncoding.Utf8);
                IBuffer cipherText     = CryptographicEngine.Encrypt(key, plainText, iv);
                string  strEncrypted64 = CryptographicBuffer.EncodeToBase64String(cipherText);
                return(strEncrypted64);
            }
            else
            {
                // Decrypt the data
                IBuffer cipherText    = CryptographicBuffer.DecodeFromBase64String(_inputText);
                IBuffer decryptedText = CryptographicEngine.Decrypt(key, cipherText, iv);
                return(CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decryptedText));
            }
        }
示例#4
0
        /**
         *
         * @param _inputText
         *            Text to be encrypted or decrypted
         * @param _encryptionKey
         *            Encryption key to used for encryption / decryption
         * @param _mode
         *            specify the mode encryption / decryption
         * @param _initVector
         *            initialization vector
         * @return encrypted or decrypted string based on the mode
         */
        private String encryptDecrypt(string _inputText, string _encryptionKey, EncryptMode _mode, string _initVector)
        {
            string _out = "";// output string

            _pwd     = Encoding.UTF8.GetBytes(_encryptionKey);
            _ivBytes = Encoding.UTF8.GetBytes(_initVector);


            int len = _pwd.Length;

            if (len > _key.Length)
            {
                len = _key.Length;
            }
            int ivLenth = _ivBytes.Length;

            if (ivLenth > _iv.Length)
            {
                ivLenth = _iv.Length;
            }


            Array.Copy(_pwd, _key, len);
            Array.Copy(_ivBytes, _iv, ivLenth);
            _rcipher.Key = _key;
            _rcipher.IV  = _iv;


            if (_mode.Equals(EncryptMode.ENCRYPT))
            {
                //encrypt
                byte[] plainText = _rcipher.CreateEncryptor().TransformFinalBlock(_enc.GetBytes(_inputText), 0, _inputText.Length);
                _out = Convert.ToBase64String(plainText);
            }
            if (_mode.Equals(EncryptMode.DECRYPT))
            {
                //decrypt
                byte[] plainText = _rcipher.CreateDecryptor().TransformFinalBlock(Convert.FromBase64String(_inputText), 0, Convert.FromBase64String(_inputText).Length);
                _out = _enc.GetString(plainText);
            }
            _rcipher.Dispose();
            return(_out);// return encrypted/decrypted string
        }
示例#5
0
        /**
         *
         * @param inputText
         *            Text to be encrypted or decrypted
         * @param encryptionKey
         *            Encryption key to used for encryption / decryption
         * @param mode
         *            specify the mode encryption / decryption
         * @param initVector
         *            initialization vector
         * @return encrypted or decrypted string based on the mode
         */

        private string EncryptDecrypt(string inputText, string encryptionKey, EncryptMode mode, string initVector)
        {
            string outputString = "";   // output string

            pwd     = Encoding.UTF8.GetBytes(encryptionKey);
            ivBytes = Encoding.UTF8.GetBytes(initVector);

            int len = pwd.Length;

            if (len > key.Length)
            {
                len = key.Length;
            }
            int ivLenth = ivBytes.Length;

            if (ivLenth > iv.Length)
            {
                ivLenth = iv.Length;
            }

            Array.Copy(pwd, key, len);
            Array.Copy(ivBytes, iv, ivLenth);
            rcipher.Key = key;
            rcipher.IV  = iv;

            if (mode.Equals(EncryptMode.ENCRYPT))
            {
                //encrypt
                byte[] plainText = rcipher.CreateEncryptor().TransformFinalBlock(enc.GetBytes(inputText), 0, inputText.Length);
                outputString = Convert.ToBase64String(plainText);
            }
            if (mode.Equals(EncryptMode.DECRYPT))
            {
                //decrypt
                byte[] plainText = rcipher.CreateDecryptor().TransformFinalBlock(Convert.FromBase64String(inputText), 0, Convert.FromBase64String(inputText).Length);
                outputString = enc.GetString(plainText);
            }
            rcipher.Dispose();
            return(outputString);// return encrypted/decrypted string
        }
示例#6
0
        /// <summary>
        /// 解密字符串(失败返回空)
        /// </summary>
        /// <param name="DecryptString">待解密字符串</param>
        /// <param name="Mode">加密模式</param>
        /// <returns></returns>
        public static string Decode(string DecryptString, EncryptMode Mode)
        {
            string ReturnString = "";

            if (Mode == EncryptMode.Cipher)
            {
                return(ReturnString);
            }

            List <EncryptInfo> EncryptList = EncryptConfigs.GetConfig().EncryptInfo;

            try {
                foreach (EncryptInfo el in EncryptList)
                {
                    EncryptMode TempMode = Utils.GetEnum <EncryptMode>(el.EncryptName.Trim(), EncryptMode.Default);

                    if (Mode.Equals(TempMode))
                    {
                        ReturnString = DecryptString;

                        //反向解密
                        for (int i = el.EncryptType.Count - 1; i >= 0; i--)
                        {
                            EncryptType ei = el.EncryptType[i];

                            switch (ei.Type)
                            {
                            case EncryptEnum.AES:
                                ReturnString = AES.Decode(ReturnString, ei.Key);
                                break;

                            case EncryptEnum.DES:
                                ReturnString = DES.Decode(ReturnString, ei.Key);
                                break;

                            default:
                                break;
                            }
                        }

                        break;
                    }
                }
            }
            catch { ReturnString = ""; }

            return(ReturnString);
        }
示例#7
0
        /// <summary>
        /// 加密字符串(失败返回空)
        /// </summary>
        /// <param name="EncryptString">待加密字符串</param>
        /// <param name="Mode">加密模式</param>
        /// <returns></returns>
        public static string Encode(string EncryptString, EncryptMode Mode)
        {
            string             ReturnString = "";
            List <EncryptInfo> EncryptList  = EncryptConfigs.GetConfig().EncryptInfo;

            try {
                foreach (EncryptInfo el in EncryptList)
                {
                    EncryptMode TempMode = Utils.GetEnum <EncryptMode>(el.EncryptName.Trim(), EncryptMode.Default);

                    if (Mode.Equals(TempMode))
                    {
                        ReturnString = EncryptString;

                        //正向加密
                        foreach (EncryptType ei in el.EncryptType)
                        {
                            switch (ei.Type)
                            {
                            case EncryptEnum.AES:
                                ReturnString = AES.Encode(ReturnString, ei.Key);
                                break;

                            case EncryptEnum.DES:
                                ReturnString = DES.Encode(ReturnString, ei.Key);
                                break;

                            case EncryptEnum.MD5:
                                ReturnString = MD5.Encode(ReturnString);
                                break;

                            default:
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            catch { ReturnString = ""; }

            return(ReturnString);
        }
示例#8
0
        /**
         *
         * @param _inputText
         *            Text to be encrypted or decrypted
         * @param _encryptionKey
         *            Encryption key to used for encryption / decryption
         * @param _mode
         *            specify the mode encryption / decryption
         * @param _initVector
         * 			  initialization vector
         * @return encrypted or decrypted string based on the mode
        */
        private byte[] doEncryptDecrypt(byte[] _inputText, string _encryptionKey, EncryptMode _mode, string _initVector)
        {
            byte[] _out = _inputText;// output string
            //_encryptionKey = MD5Hash (_encryptionKey);
            _pwd = System.Text.Encoding.UTF8.GetBytes(_encryptionKey);
            _ivBytes = System.Text.Encoding.UTF8.GetBytes(_initVector);

            int len = _pwd.Length;
            if (len > _key.Length)
            {
                len = _key.Length;
            }
            int ivLenth = _ivBytes.Length;
            if (ivLenth > _iv.Length)
            {
                ivLenth = _iv.Length;
            }

            Array.Copy(_pwd, _key, len);
            Array.Copy(_ivBytes, _iv, ivLenth);
            _rcipher.Key = _key;
            _rcipher.IV = _iv;

            if (_mode.Equals(EncryptMode.ENCRYPT))
            {
                //encrypt
                //byte[] plainText = _rcipher.CreateEncryptor().TransformFinalBlock(_enc.GetBytes(_inputText), 0, _inputText.Length);
                _out = _rcipher.CreateEncryptor().TransformFinalBlock(_inputText, 0, _inputText.Length);
                //_out = Convert.ToBase64String(plainText);
            }
            if (_mode.Equals(EncryptMode.DECRYPT))
            {
                //decrypt
                //byte[] plainText = _rcipher.CreateDecryptor().TransformFinalBlock(Convert.FromBase64String(_inputText), 0, Convert.FromBase64String(_inputText).Length);
                _out = _rcipher.CreateDecryptor().TransformFinalBlock(_inputText, 0, _inputText.Length);
                //_out = _enc.GetString(plainText);
            }
            _rcipher.Dispose();
            return _out;// return encrypted/decrypted string
        }
示例#9
0
        /**
         *
         * @param _inputText
         *            Text to be encrypted or decrypted
         * @param _encryptionKey
         *            Encryption key to used for encryption / decryption
         * @param _mode
         *            specify the mode encryption / decryption
         * @param _initVector
         *            initialization vector
         * @return encrypted or decrypted string based on the mode
         */
        private String encryptDecrypt(byte[] _encryptDecryptBytes, string _encryptionKey, EncryptMode _mode, string _initVector)
        {
            // initialization
            _aes           = Aes.Create();
            _aes.Mode      = CipherMode.CBC;
            _aes.Padding   = PaddingMode.PKCS7;
            _aes.KeySize   = 256;
            _aes.BlockSize = 128;

            byte[] _key     = new byte[32];
            byte[] _iv      = new byte[_aes.BlockSize / 8]; //128 bit / 8 = 16 bytes
            byte[] _pwd     = Encoding.UTF8.GetBytes(_encryptionKey);
            byte[] _ivBytes = Encoding.UTF8.GetBytes(_initVector);

            try
            {
                string _out = string.Empty; // output string
                                            //_encryptionKey = MD5Hash (_encryptionKey);

                int len = _pwd.Length;
                if (len > _key.Length)
                {
                    len = _key.Length;
                }
                int ivLenth = _ivBytes.Length;
                if (ivLenth > _iv.Length)
                {
                    ivLenth = _iv.Length;
                }

                Array.Copy(_pwd, _key, len);
                Array.Copy(_ivBytes, _iv, ivLenth);
                _aes.Key = _key;
                _aes.IV  = _iv;

                if (_mode.Equals(EncryptMode.ENCRYPT))
                {
                    //encrypt
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var encryptor = _aes.CreateEncryptor())
                        {
                            using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                            {
                                cryptoStream.Write(_encryptDecryptBytes, 0, _encryptDecryptBytes.Length);
                                cryptoStream.Close();
                                _out = Convert.ToBase64String(memoryStream.ToArray());
                            }
                        }
                    }
                }
                if (_mode.Equals(EncryptMode.DECRYPT))
                {
                    //decrypt
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var decryptor = _aes.CreateDecryptor())
                        {
                            using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Write))
                            {
                                cryptoStream.Write(_encryptDecryptBytes, 0, _encryptDecryptBytes.Length);
                                cryptoStream.Close();
                                _out = Encoding.UTF8.GetString(memoryStream.ToArray());
                            }
                        }
                    }
                }
                return(_out);// return encrypted/decrypted string
            }
            finally
            {
                Array.Clear(_key, 0, _key.Length);
                Array.Clear(_pwd, 0, _pwd.Length);
                Array.Clear(_ivBytes, 0, _ivBytes.Length);
                Array.Clear(_iv, 0, _iv.Length);

                _aes.Dispose();
            }
        }
        /***
         * This function decrypts the encrypted text to plain text using the key
         * provided. You'll have to use the same key which you used during
         * encryption
         *
         * @param _encryptedText
         *            Encrypted/Cipher text to be decrypted
         * @param _key
         *            Encryption key which you used during encryption
         */
        private string encryptDecrypt(string _inputText, string _encryptionKey, EncryptMode _mode, string _initVector)
        {
            SymmetricKeyAlgorithmProvider aesCbcPkcs7 = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);

            // Creata a 16 byte initialization vector
            IBuffer iv = CryptographicBuffer.ConvertStringToBinary(_initVector, BinaryStringEncoding.Utf8);

            // Create an AES 128-bit (16 byte) key
            IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(_encryptionKey, BinaryStringEncoding.Utf8);
            CryptographicKey key = aesCbcPkcs7.CreateSymmetricKey(keyMaterial);

            if (_mode.Equals(EncryptMode.ENCRYPT))
            {
                // Encrypt the data
                IBuffer plainText = CryptographicBuffer.ConvertStringToBinary(_inputText, BinaryStringEncoding.Utf8);
                IBuffer cipherText = CryptographicEngine.Encrypt(key, plainText, iv);
                string strEncrypted64 = CryptographicBuffer.EncodeToBase64String(cipherText);
                return strEncrypted64;
            }
            else
            {
                // Decrypt the data
                IBuffer cipherText = CryptographicBuffer.DecodeFromBase64String(_inputText);
                IBuffer decryptedText = CryptographicEngine.Decrypt(key, cipherText, iv);
                return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decryptedText);
            }
        }