Beispiel #1
0
        /// <summary>
        /// Desencripta un texto pasandole la KEY a usar.
        /// Se puede pasar la variable "useHashing" como true para usar un codigo hash en vez de la KEY.
        /// </summary>
        /// <param name="cipherString"></param>
        /// <param name="Key"></param>
        /// <param name="useHashing"></param>
        /// <returns></returns>
        public static string Decrypt(string cipherString, string Key, bool useHashing = false)
        {
            byte[] keyArray;
            byte[] toEncryptArray;
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

            try
            {
                // Get the byte code of the string
                toEncryptArray = Convert.FromBase64String(cipherString);

                if (useHashing)
                {
                    // If hashing was used get the hash code with regards to your key
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));

                    // Release any resource held by the MD5CryptoServiceProvider
                    hashmd5.Clear();

                }
                else
                {
                    // If hashing was not implemented get the byte code of the key
                    keyArray = UTF8Encoding.UTF8.GetBytes(Key);
                }

                // Set the secret key for the tripleDES algorithm
                tdes.Key = keyArray;

                // Mode of operation. there are other 4 modes (we choose ECB(Electronic code Book))
                tdes.Mode = CipherMode.ECB;

                // Padding mode(if any extra byte added)
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateDecryptor();

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

                // Release resources held by TripleDes Encryptor
                tdes.Clear();

                // Return the Clear decrypted TEXT
                return UTF8Encoding.UTF8.GetString(resultArray);

            }
            catch
            {
                // Release resources held by TripleDes Encryptor
                tdes.Clear();

                return "-1";
            }
        }
Beispiel #2
0
        /// <summary>
        /// Encripta un texto pasandole la KEY a usar.
        /// Se puede pasar la variable "useHashing" como true para usar un codigo hash en vez de la KEY.
        /// </summary>
        /// <param name="toEncrypt"></param>
        /// <param name="Key"></param>
        /// <param name="useHashing"></param>
        /// <returns></returns>
        public static string Encrypt(string toEncrypt, string Key, bool useHashing = false)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

            try
            {
                // If hashing use get hashcode regards to your key
                if (useHashing)
                {
                    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));

                    // Always release the resources and flush data of the Cryptographic service provide. Best Practice
                    hashmd5.Clear();

                }
                else
                {
                    keyArray = UTF8Encoding.UTF8.GetBytes(Key);
                }

                // Set the secret key for the tripleDES algorithm
                tdes.Key = keyArray;

                // Mode of operation. there are other 4 modes (we choose ECB(Electronic code Book))
                tdes.Mode = CipherMode.ECB;

                // Padding mode(if any extra byte added)
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateEncryptor();

                // Transform the specified region of bytes array to resultArray
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                // Release resources held by TripleDes Encryptor
                tdes.Clear();

                // Return the encrypted data into unreadable string format
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);

            }
            catch
            {
                // Release resources held by TripleDes Encryptor
                tdes.Clear();

                return "-1";
            }
        }
Beispiel #3
0
        public static string Encrypt3DES(string a_strString, string a_strKey, string a_strIV)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(a_strString);
            des.Key     = System.Text.Encoding.UTF8.GetBytes(a_strKey);
            des.IV      = System.Text.Encoding.UTF8.GetBytes(a_strIV);
            des.Mode    = System.Security.Cryptography.CipherMode.CBC;
            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            System.IO.StreamWriter swEncrypt             = new System.IO.StreamWriter(cs);
            swEncrypt.WriteLine(a_strString);
            swEncrypt.Close();

            //把内存流转换成字节数组,内存流现在已经是密文了
            byte[] bytesCipher  = ms.ToArray();
            string base64String = System.Convert.ToBase64String(bytesCipher);

            //加密流关闭
            cs.Close();
            des.Clear();
            ms.Close();


            return(base64String);
        }
Beispiel #4
0
 public static ICryptoTransform CreateEncryptor(string key, CryptionType type)
 {
     ICryptoTransform transform;
     SHA512 sha512 = new SHA512CryptoServiceProvider();
     var bytes = sha512.ComputeHash(Sha1(key).ToAsciiBytes());
     switch (type)
     {
         case CryptionType.Aes:
             var aes = Rijndael.Create();
             aes.Mode = CipherMode.CBC;
             transform = aes.CreateEncryptor(bytes.Skip(17).Take(32).ToArray(), bytes.Skip(17).Take(16).ToArray());
             aes.Clear();
             break;
         case CryptionType.Des:
             var des = new DESCryptoServiceProvider { Mode = CipherMode.CBC };
             transform = des.CreateEncryptor(bytes.Skip(17).Take(8).ToArray(), bytes.Skip(17).Take(16).ToArray());
             des.Clear();
             break;
         default:
             var tripleDes = new TripleDESCryptoServiceProvider { Mode = CipherMode.CBC };
             transform = tripleDes.CreateEncryptor(bytes.Skip(17).Take(24).ToArray(), bytes.Skip(17).Take(16).ToArray());
             tripleDes.Clear();
             break;
     }
     return transform;
 }
Beispiel #5
0
        internal string Decrypt(string value)
        {
            MD5CryptoServiceProvider hashProvider = null;
            TripleDESCryptoServiceProvider provider = null;

            try
            {
                hashProvider = new MD5CryptoServiceProvider();
                var hashPassPhrase = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(passPhrase));

                provider = new TripleDESCryptoServiceProvider();
                provider.Key = hashPassPhrase;
                provider.Mode = CipherMode.ECB;
                provider.Padding = PaddingMode.PKCS7;

                var dataToEncrypt = Convert.FromBase64String(value);
                var decryptor = provider.CreateDecryptor();
                var results = decryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
                return Encoding.UTF8.GetString(results);

            }
            finally
            {
                if (provider != null) provider.Clear();
                if (hashProvider != null) hashProvider.Clear();
            }
        }
        public string Encrypt(string text)
        {
            try
            {

                byte[] getBytes = UTF8Encoding.UTF8.GetBytes(text); //getting the string to encrypt

                System.Security.Cryptography.MD5CryptoServiceProvider cryptObj = new System.Security.Cryptography.MD5CryptoServiceProvider();

                byte[] computeHashArray = cryptObj.ComputeHash(UTF8Encoding.UTF8.GetBytes(secureKey));

                    cryptObj.Clear();  //Releasing the object

                TripleDESCryptoServiceProvider tripleDESC = new TripleDESCryptoServiceProvider();  //creating an object of TRIPLE DES service provider

                tripleDESC.Key = computeHashArray;   //securekey is being set

                tripleDESC.Mode = CipherMode.ECB;  //Electronic Code Book mode is being set

                tripleDESC.Padding = PaddingMode.PKCS7;   //Padding

                ICryptoTransform ICT = tripleDESC.CreateEncryptor();

                byte[] transform = ICT.TransformFinalBlock(getBytes, 0,getBytes.Length);  //Byte Array to Transform array

                tripleDESC.Clear();   //release object

                return Convert.ToBase64String(transform, 0, transform.Length);   //return encrypted string

            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
Beispiel #7
0
        public string Decrypt(string message)
        {
            byte[] results;
            UTF8Encoding utf8 = new UTF8Encoding();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] deskey = md5.ComputeHash(utf8.GetBytes(Passphrase));
            TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
            desalg.Key = deskey;
            desalg.Mode = CipherMode.ECB;
            desalg.Padding = PaddingMode.PKCS7;
            byte[] decryptData = Convert.FromBase64String(message);
            try
            {
                //To transform the utf binary code to md5 decrypt
                ICryptoTransform decryptor = desalg.CreateDecryptor();
                results = decryptor.TransformFinalBlock(decryptData, 0, decryptData.Length);
            }
            finally
            {
                desalg.Clear();
                md5.Clear();

            }
            //TO convert decrypted binery code to string
            return utf8.GetString(results);
        }
        private const string secureKey = "QaZ1wSx2Edc3RfV4"; //Important Security Key

        #endregion Fields

        #region Methods

        public string Decrypt(string encryptedString)
        {
            try
            {

                byte[] extractBytes = Convert.FromBase64String(encryptedString);   //getting the string to decrypt

                MD5CryptoServiceProvider descObj = new MD5CryptoServiceProvider();  //creating an instance of MD5CryptoServiceProvider
                   byte[] hashCompute = descObj.ComputeHash(UTF8Encoding.UTF8.GetBytes(secureKey));

                    descObj.Clear();  //releasing resourse

                TripleDESCryptoServiceProvider DESCD = new TripleDESCryptoServiceProvider();

                DESCD.Key = hashCompute;  //secure Key is being set

                DESCD.Mode = CipherMode.ECB;     //Electronic Code Book Mode

                DESCD.Padding = PaddingMode.PKCS7;  //Padding

                ICryptoTransform ICTransform = DESCD.CreateDecryptor();
                byte[] finalResult = ICTransform.TransformFinalBlock(                          //Transforming byte array to finalResult
                                     extractBytes, 0, extractBytes.Length);

                DESCD.Clear();

                return UTF8Encoding.UTF8.GetString(finalResult);  //Return Decrypted Text
            }
            catch (Exception ex)
            {
                return ex.ToString();

            }
        }
Beispiel #9
0
 public static string Encrypt(string text, bool useHash)
 {
     byte[] keyArray;
     byte[] toEcryptArray = UTF8Encoding.UTF8.GetBytes(text);
     System.Configuration.AppSettingsReader setingReader = new System.Configuration.AppSettingsReader();
     string key = (string)setingReader.GetValue("SecurityKey", typeof(string));
     if (useHash)
     {
         MD5CryptoServiceProvider hashMd5 = new MD5CryptoServiceProvider();
         keyArray = hashMd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
         hashMd5.Clear();
     }
     else
     {
         keyArray = UTF8Encoding.UTF8.GetBytes(key);
     }
     TripleDESCryptoServiceProvider crypto = new TripleDESCryptoServiceProvider();
     crypto.Key = keyArray;
     crypto.Mode = CipherMode.ECB;
     crypto.Padding = PaddingMode.PKCS7;
     ICryptoTransform transforme = crypto.CreateEncryptor();
     byte[] result = transforme.TransformFinalBlock(toEcryptArray, 0, toEcryptArray.Length);
     crypto.Clear();
     return Convert.ToBase64String(result, 0, result.Length);
 }
Beispiel #10
0
        public static string Encrypt(this string plainText, string encryptionKey)
        {
            var toEncryptArray = Encoding.ASCII.GetBytes(plainText);

            byte[] encryptionKeyArray;

            // get MD5 has for the key
            using (var hashmd5 = new MD5CryptoServiceProvider())
            {
                encryptionKeyArray = hashmd5.ComputeHash(Encoding.ASCII.GetBytes(encryptionKey));
                hashmd5.Clear();
            }

            byte[] resultArray;

            // Set the secret key for the tripleDES algorithm
            using (
                var tdes = new TripleDESCryptoServiceProvider
                {
                    Key = encryptionKeyArray,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                })
            {
                // Transform the specified region of bytes array to resultArray
                var cTransform = tdes.CreateEncryptor();
                resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                tdes.Clear();
            }

            // Return the encrypted data into unreadable string format
            return Convert.ToBase64String(resultArray);
        }
Beispiel #11
0
        public string Encrypt(string message)
        {
            byte[] results;
            UTF8Encoding utf8 = new UTF8Encoding();
            //to create the object for UTF8Encoding  class
            //TO create the object for MD5CryptoServiceProvider
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] deskey = md5.ComputeHash(utf8.GetBytes(Passphrase));
            //to convert to binary passkey
            //TO create the object for  TripleDESCryptoServiceProvider
            TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
            desalg.Key = deskey;//to  pass encode key
            desalg.Mode = CipherMode.ECB;
            desalg.Padding = PaddingMode.PKCS7;
            byte[] encryptData = utf8.GetBytes(message);
            //to convert the string to utf encoding binary

            try
            {

                //To transform the utf binary code to md5 encrypt
                ICryptoTransform encryptor = desalg.CreateEncryptor();
                results = encryptor.TransformFinalBlock(encryptData, 0, encryptData.Length);

            }
            finally
            {
                //to clear the allocated memory
                desalg.Clear();
                md5.Clear();
            }
            //to convert to 64 bit string from converted md5 algorithm binary code
            return Convert.ToBase64String(results);
        }
        public string Encrypt(string toEncrypt, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(Encoding.UTF8.GetBytes(SecurityKey));
                hashmd5.Clear();
            }
            else
                keyArray = Encoding.UTF8.GetBytes(SecurityKey);

            var tdes = new TripleDESCryptoServiceProvider
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tdes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        public static string Encrypt(string toEncrypt, string key)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
            // Get the key from config file


            System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            hashmd5.Clear();
            System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            //set the secret key for the tripleDES algorithm
            tdes.Key = keyArray;
            //mode of operation. there are other 4 modes.
            //We choose ECB(Electronic code Book)
            tdes.Mode = System.Security.Cryptography.CipherMode.ECB;
            //padding mode(if any extra byte added)

            tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateEncryptor();
            //transform the specified region of bytes array to resultArray
            byte[] resultArray =
                cTransform.TransformFinalBlock(toEncryptArray, 0,
                                               toEncryptArray.Length);
            //Release resources held by TripleDes Encryptor
            tdes.Clear();
            //Return the encrypted data into unreadable string format
            return(Convert.ToBase64String(resultArray, 0, resultArray.Length));
        }
Beispiel #14
0
        /// <summary>
        /// Returns a decrypted string by a key
        /// </summary>
        /// <remarks>
        /// If something is broken this method returns an empty string
        /// </remarks>
        /// <param name="text">Text to decrypt</param>
        /// <param name="key">Key to decrypt</param>
        /// <param name="encoding" >Encoding to get bytes. UTF8 by default.</ param >
        /// <returns></returns>
        public static String Decrypt(String textoEncriptado, String clave, Encoding encoding = null)
        {
            try
            {
                if (String.IsNullOrEmpty(textoEncriptado) || String.IsNullOrEmpty(clave))
                    return String.Empty;

                byte[] keyBytes;
                byte[] encryptedBytes = Convert.FromBase64String(textoEncriptado);

                //Create a MD5 object to obtain a hash
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();

                keyBytes = hashmd5.ComputeHash(encoding.GetBytes(clave));
                hashmd5.Clear();

                //Create a Triple DES object to decrypt
                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

                tdes.Key = keyBytes;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateDecryptor();

                byte[] resultArray = cTransform.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
                tdes.Clear();

                return encoding.GetString(resultArray);
            }
            catch (Exception)
            {
                return String.Empty;
            }
        }
Beispiel #15
0
        public static string Encrypt(string ToEncrypt, bool useHasing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(ToEncrypt);

            string Key = "malkit";
            if (useHasing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));
                hashmd5.Clear();
            }
            else
            {
                keyArray = UTF8Encoding.UTF8.GetBytes(Key);
            }
            TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
            tDes.Key = keyArray;
            tDes.Mode = CipherMode.ECB;
            tDes.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tDes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tDes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
Beispiel #16
0
        public static string Decrypt(string cipherString)
        {
            try
            {
                byte[] keyArray;
                //get the byte code of the string

                byte[] toEncryptArray = Convert.FromBase64String(cipherString);

                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));
                hashmd5.Clear();

                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(
                                     toEncryptArray, 0, toEncryptArray.Length);
                tdes.Clear();

                return UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
Beispiel #17
0
        public static AptimaLicenseInfo DecryptKey(string licenseKey, string securityKey, int inputStringLength)
        {
            ////Decrypt

            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(licenseKey);
 
            keyArray = UTF8Encoding.UTF8.GetBytes(securityKey);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            tdes.Clear();
            string outputString = UTF8Encoding.UTF8.GetString(resultArray);
            if (outputString.Length != inputStringLength)
            {//invalid output string! 
                return new AptimaLicenseInfo("Invalid license key! (Return string has incorrect length)");
            }

            ////populate

            return PopulateLicenseInfo(outputString);            
        }
        /// <summary>
        /// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
        /// </summary>
        /// <param name="cipherString">encrypted string</param>
        /// <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
        /// <returns></returns>
        public static string Decrypt(string cipherString, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = Convert.FromBase64String(cipherString);

            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            //Get your key from config file to open the lock!
            string key = SecurityKey;

            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            tdes.Clear();
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
        /// <summary>
        /// Encrypt a string using dual encryption method. Return a encrypted cipher Text
        /// </summary>
        /// <param name="toEncrypt">string to be encrypted</param>
        /// <param name="useHashing">use hashing? send to for extra secirity</param>
        /// <returns></returns>
        public static string Encrypt(string toEncrypt, bool useHashing)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            // Get the key from config file
            string key = SecurityKey;
            //System.Windows.Forms.MessageBox.Show(key);
            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tdes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        public static string maHoa(string strMaHoa)
        {
            byte[] arrKey;
            byte[] arrMahoa = UTF8Encoding.UTF8.GetBytes(strMaHoa);

            System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            //Lấy giá trị key từ file Web.config
            string keyConfig = (string)settingsReader.GetValue("keyMD5", typeof(String));

            MD5CryptoServiceProvider serviceMD5 = new MD5CryptoServiceProvider();
            arrKey = serviceMD5.ComputeHash(UTF8Encoding.UTF8.GetBytes(keyConfig));
            serviceMD5.Clear();//Giải phóng tài nguyên
            TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();

            //Thiết lập 1 key cho thuật toán mã hóa
            tdsp.Key = arrKey;

            //Chọn phương thức mã hóa. Ở đây chúng ta chọn ECB(Electronic code Book ) [1]
            tdsp.Mode = CipherMode.ECB;

            //Thêm phương thức mã hóa IS010126 (random chuỗi kết quả)  [2]
            tdsp.Padding = PaddingMode.ISO10126;

            ICryptoTransform cTransform = tdsp.CreateEncryptor();
            byte[] arrKQ = cTransform.TransformFinalBlock(arrMahoa, 0, arrMahoa.Length);
            tdsp.Clear(); //Giải phóng tài nguyên của TripleDES Encryptor

            //Kết quả mã hóa được trả về 1 giá trị kiểu String
            return Convert.ToBase64String(arrKQ, 0, arrKQ.Length);
        }
Beispiel #21
0
        public static string DecryptString(string message)
        {
            byte[] results;

            // Step 1. We hash the customPassPhrase using MD5
            // We use the MD5 hash generator as the result is a 128 bit byte array
            // which is a valid length for the TripleDES encoder we use below
            var hashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(customPassPhrase));

            // Step 3. Setup the decoder
            var tripleDESAlgorithm = new TripleDESCryptoServiceProvider() { Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
            tripleDESAlgorithm.Key = TDESKey;

            // Step 4. Convert the input string to a byte[]
            byte[] dataToDecrypt = Convert.FromBase64String(message);

            // Step 5. Attempt to decrypt the string
            try
            {
                ICryptoTransform decryptor = tripleDESAlgorithm.CreateDecryptor();
                results = decryptor.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length);
                decryptor.Dispose();
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                tripleDESAlgorithm.Clear();
                hashProvider.Clear();
            }

            // Step 6. Return the decrypted string in UTF8 format
            return Encoding.UTF8.GetString(results);
        }
Beispiel #22
0
        public static string Decrypt(string value)
        {
            byte[] toDecryptArray = Convert.FromBase64String( value );
            byte[] keyArray = Convert.FromBase64String( Config.Manager.Framework.Encryption.Key );
            byte[] ivArray = Convert.FromBase64String(Config.Manager.Framework.Encryption.IV);

            TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
            tDes.Key = keyArray;
            tDes.Mode = CipherMode.CBC;
            tDes.Padding = PaddingMode.PKCS7;
            tDes.IV = ivArray;

            ICryptoTransform cTransform = tDes.CreateDecryptor();
            try
            {
                byte[] resultArray = cTransform.TransformFinalBlock( toDecryptArray, 0, toDecryptArray.Length );

                tDes.Clear();
                return UTF8Encoding.UTF8.GetString( resultArray, 0, resultArray.Length );
            }
            catch( Exception ex )
            {
                throw ex;
            }
        }
        public static string EncryptData(string Message)
        {
            byte[] Results;
            string passphrase = ReadCert();

            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase));
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            byte[] DataToEncrypt = UTF8.GetBytes(Message);
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            return Convert.ToBase64String(Results);
        }
        /// <summary>
        /// EncryptString
        /// </summary>
        /// <param name="message"></param>
        /// <param name="passphrase"></param>
        /// <returns></returns>
        public static string EncryptString(string message)
        {
            byte[] results;
            var utf8 = new System.Text.UTF8Encoding();

            var hashProvider = new MD5CryptoServiceProvider();
            var tdesKey = hashProvider.ComputeHash(utf8.GetBytes(Pass));

            var tdesAlgorithm = new TripleDESCryptoServiceProvider
                {
                    Key = tdesKey,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };

            var dataToEncrypt = utf8.GetBytes(message);

            try
            {
                var encryptor = tdesAlgorithm.CreateEncryptor();
                results = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
            }
            finally
            {
                tdesAlgorithm.Clear();
                hashProvider.Clear();
            }

            return Convert.ToBase64String(results);
        }
        /// <summary>
        /// Decrypts a cipher sting
        /// </summary>
        /// <param name="cipherString">String to decrypt</param>
        /// <returns>Decrypted string</returns>
        public string DecryptString(string cipherString)
        {
            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = Convert.FromBase64String(cipherString);

                System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
                string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();

                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

                tdes.Clear();
                return UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception e)
            {
                Console.WriteLine("DecryptString exception : " + e.ToString());
                return null;
            }
        }
Beispiel #26
0
        //Giai ma  HashMD5 và TripleDES
        public string Decrypt(string cipherString, bool i_blUseHashing)
        {
            byte[] v_arrKey;
            byte[] v_arrEncrypt = new byte[(int)cipherString.Length];
            try
            {
                v_arrEncrypt = Convert.FromBase64String(cipherString);
            }
            catch (Exception v_ex)
            {
                throw v_ex;
            }
            string v_strKey = "fatherofbill";
            if (i_blUseHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                v_arrKey = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(v_strKey));
                hashmd5.Clear();
            }
            else
            {
                v_arrKey = UTF8Encoding.UTF8.GetBytes(v_strKey);
            }
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = v_arrKey;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(v_arrEncrypt, 0, v_arrEncrypt.Length);

            tdes.Clear();
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
        internal static string DesEncriptar(string clave)
        {
            String sPass = String.Empty;
            try
            {
                string key = "ABCDEFG54669525PQRSTUVWXYZabcdef852846opqrstuvwxyz";
                byte[] keyArray;

                byte[] Array_a_Descifrar = Convert.FromBase64String(clave);

                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();

                keyArray = hashmd5.ComputeHash(
                UTF8Encoding.UTF8.GetBytes(key));

                hashmd5.Clear();

                TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

                tdes.Key = keyArray;
                tdes.Mode = CipherMode.ECB;
                tdes.Padding = PaddingMode.PKCS7;

                ICryptoTransform cTransform = tdes.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(Array_a_Descifrar, 0, Array_a_Descifrar.Length);
                tdes.Clear();
                sPass = UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception) {}
            return sPass;
        }
Beispiel #28
0
        /// <summary>
        /// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
        /// </summary>
        /// <param name="cipherString">encrypted string</param>
        /// <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
        /// <returns></returns>
        public static string Decrypt(string cipherString)
        {
            byte[] keyArray;
            bool useHashing = true;
            byte[] toEncryptArray = Convert.FromBase64String(cipherString);
            string key = "ImgONE";
            if (useHashing)
            {
                MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            }
            else
                keyArray = UTF8Encoding.UTF8.GetBytes(key);

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = tdes.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            tdes.Clear();
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
 public static string Decrypt(string cypherString, bool useHasing)
 {
     byte[] keyArray;
     byte[] toDecryptArray = Convert.FromBase64String(cypherString);
     string key = "uzma";
     if (useHasing)
     {
         MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
         keyArray = hashmd.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
         hashmd.Clear();
     }
     else
     {
         keyArray = UTF8Encoding.UTF8.GetBytes(key);
     }
     TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
     tDes.Key = keyArray;
     tDes.Mode = CipherMode.ECB;
     tDes.Padding = PaddingMode.PKCS7;
     ICryptoTransform cTransform = tDes.CreateDecryptor();
     try
     {
         byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
         tDes.Clear();
         return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        public static string Decrypt(string cypherString)
        {
            byte[] keyArray;
            byte[] toDecryptArray = StringToByteArray(cypherString);

            MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
            keyArray = hashmd.ComputeHash(UTF8Encoding.UTF8.GetBytes(ENCRYPTION_KEY));
            hashmd.Clear();

            TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
            tDes.Key = keyArray;
            tDes.Mode = CipherMode.ECB;
            tDes.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tDes.CreateDecryptor();
            try
            {
                byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);

                tDes.Clear();
                return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #31
0
 public static byte[] smethod_10(byte[] byte_0, string string_0)
 {
     System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     tripleDESCryptoServiceProvider.Key     = System.Text.Encoding.UTF8.GetBytes(string_0);
     tripleDESCryptoServiceProvider.Mode    = System.Security.Cryptography.CipherMode.ECB;
     tripleDESCryptoServiceProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateEncryptor();
     byte[] result = cryptoTransform.TransformFinalBlock(byte_0, 0, byte_0.Length);
     tripleDESCryptoServiceProvider.Clear();
     return(result);
 }
Beispiel #32
0
 public static byte[] smethod_19(byte[] byte_0, string string_0)
 {
     byte[] array = byte_0;
     System.Array.Resize <byte>(ref array, array.Length + 1);
     array[array.Length - 1] = (byte)new System.Random().Next(0, 255);
     System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     tripleDESCryptoServiceProvider.Key     = System.Text.Encoding.UTF8.GetBytes(string_0);
     tripleDESCryptoServiceProvider.Mode    = System.Security.Cryptography.CipherMode.ECB;
     tripleDESCryptoServiceProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform cryptoTransform = tripleDESCryptoServiceProvider.CreateEncryptor();
     byte[] result = cryptoTransform.TransformFinalBlock(array, 0, array.Length);
     tripleDESCryptoServiceProvider.Clear();
     return(result);
 }
Beispiel #33
0
        public static string Encrypt3DES(string a_strString, string a_strKey, string a_strIV)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(a_strString);
            //des.Key = System.Text.Encoding.UTF8.GetBytes(a_strKey);
            byte[] _key = System.Text.Encoding.UTF8.GetBytes(a_strKey);
            //des.IV = System.Text.Encoding.UTF8.GetBytes(a_strIV);
            //des.IV = Keys;
            des.Mode    = System.Security.Cryptography.CipherMode.CBC;
            des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            //System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(cs);
            //swEncrypt.WriteLine(a_strString);
            //swEncrypt.Close();

            //把内存流转换成字节数组,内存流现在已经是密文了
            byte[] bytesCipher = ms.ToArray();
            //内存流关闭

            string base64String = System.Convert.ToBase64String(bytesCipher);

            //string by = "";
            //foreach (byte b in bytesCipher)
            //{
            //    by += b.ToString() + " ";
            //}
            //SbeLogger.info("【3DESBytes】" + by);
            //byte[] FromBase64String = Convert.FromBase64String(base64String);
            //ms = new MemoryStream(FromBase64String);
            //cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
            //StreamReader sr = new StreamReader(cs);
            ////输出解密后的内容
            //string DecryptString = sr.ReadLine();

            //加密流关闭
            cs.Close();
            des.Clear();
            ms.Close();


            return(base64String);
        }
Beispiel #34
0
        public static string decryptDES3(string strData, string strKey)
        {
            string strDecrypt = strData;

            byte[] arrData = hex2bytes(strData);

            System.Security.Cryptography.TripleDESCryptoServiceProvider provDES3
                             = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            provDES3.Key     = System.Text.UTF8Encoding.UTF8.GetBytes(strKey.Substring(0, 24));
            provDES3.Mode    = System.Security.Cryptography.CipherMode.CBC;
            provDES3.IV      = hex2bytes("0000000000000000");                  // this is important !!
            provDES3.Padding = System.Security.Cryptography.PaddingMode.PKCS7; // this is OK

            System.Security.Cryptography.ICryptoTransform decryptor = provDES3.CreateDecryptor();
            byte[] arrResult = decryptor.TransformFinalBlock(arrData, 0, arrData.Length);
            provDES3.Clear();

            strDecrypt = System.Text.UTF8Encoding.UTF8.GetString(arrResult);
            return(strDecrypt);
        }