예제 #1
0
        public MFTestResults TDesTest_EncryptUpdate()
        {
            MFTestResults res;

            try
            {
                using (TripleDESCryptoServiceProvider csp = new TripleDESCryptoServiceProvider())
                {
                    res = SymmetricTestHelper.Test_EncryptUpdate(csp);
                }

                if (res == MFTestResults.Pass && m_isEmulator)
                {
                    using (TripleDESCryptoServiceProvider csp = new TripleDESCryptoServiceProvider("Emulator_Crypto"))
                    {
                        res = SymmetricTestHelper.Test_EncryptUpdate(csp);
                    }
                }
            }
            catch
            {
                return MFTestResults.Fail;
            }

            return res;
        }
예제 #2
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();
            }
        }
예제 #3
0
        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;
            }
        }
예제 #4
0
        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));
        }
        public msgMaid cooking(byte[] otama)
        {
            //メイドオブジェクト
            msgMaid m = new msgMaid();

            //DESC
            TripleDESCryptoServiceProvider frill = new TripleDESCryptoServiceProvider();

            // Triple DES のサービス プロバイダを生成します
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

            //取得
            m.houshinokokoro = frill.Key;
            m.zettaifukujyu = frill.IV;

            // source 配列から cryptData 配列へ変換
            // 文字列を byte 配列に変換します
            //byte[] source = Encoding.Unicode.GetBytes(cachusha);

            // 入出力用のストリームを生成します
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(m.houshinokokoro, m.zettaifukujyu), CryptoStreamMode.Write))
                {
                    // ストリームに暗号化するデータを書き込みます
                    cs.Write(otama, 0, otama.Length);
                }

                // 暗号化されたデータを byte 配列で取得します
                m.zenryokushugo = ms.ToArray();
            }

            // byte 配列を文字列に変換して表示します
            return m;
        }
예제 #6
0
        public static string Encrypt(String plainText )
        {
            string encrypted = null;
            try
            {
                byte[] inputBytes = ASCIIEncoding.ASCII.GetBytes(plainText);
                byte[] pwdhash = null;
                MD5CryptoServiceProvider hashmd5;

                //generate an MD5 hash from the password.
                //a hash is a one way encryption meaning once you generate
                //the hash, you cant derive the password back from it.
                hashmd5 = new MD5CryptoServiceProvider();
                pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
                hashmd5 = null;

                // Create a new TripleDES service provider
                TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
                tdesProvider.Key = pwdhash;
                tdesProvider.Mode = CipherMode.ECB;

                encrypted = Convert.ToBase64String(
                    tdesProvider.CreateEncryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length));
            }
            catch(Exception e)
            {
                string str = e.Message;
                throw ;
            }
            return encrypted;
        }
예제 #7
0
        public static string Decodificar(string entrada)
        {
            TripleDESCryptoServiceProvider tripledescryptoserviceprovider = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider md5cryptoserviceprovider = new MD5CryptoServiceProvider();

            try
            {
                if (entrada.Trim() != "")
                {
                    string chave = "asdfg";
                    tripledescryptoserviceprovider.Key = md5cryptoserviceprovider.ComputeHash(Encoding.Default.GetBytes(chave));
                    tripledescryptoserviceprovider.Mode = CipherMode.ECB;
                    ICryptoTransform desdencrypt = tripledescryptoserviceprovider.CreateDecryptor();
                    byte[] buff = Convert.FromBase64String(entrada);

                    return Encoding.Default.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
                }
                else
                {
                    return "";
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show("Erro" + exception.Message);
                throw exception;
            }
            finally
            {
                tripledescryptoserviceprovider = null;
                md5cryptoserviceprovider = null;
            }
        }
        /// <summary>
        /// Encrypts to provided string parameter.
        /// </summary>
        public static string Encrypt(string s)
        {
            if (s == null || s.Length == 0) return string.Empty;

            string result = string.Empty;

            try
            {
                byte[] buffer = Encoding.ASCII.GetBytes(s);

                TripleDESCryptoServiceProvider des =
                    new TripleDESCryptoServiceProvider();

                MD5CryptoServiceProvider MD5 =
                    new MD5CryptoServiceProvider();

                des.Key =
                    MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));

                des.IV = IV;
                result = Convert.ToBase64String(
                    des.CreateEncryptor().TransformFinalBlock(
                        buffer, 0, buffer.Length));
            }
            catch
            {
                throw;
            }

            return result;
        }
예제 #9
0
파일: Text.cs 프로젝트: sameesh-s/stego-app
        /// <summary>
        /// Decrypt the given string using the specified key.
        /// </summary>
        /// <param name="strEncrypted">The string to be decrypted.</param>
        /// <param name="key">The decryption key.</param>
        /// <returns>The decrypted string.</returns>
        /// <exception cref="Exception">Unexpected Exception</exception>
        public static string Decrypt(string strEncrypted, string key)
        {
            try
            {
                TripleDESCryptoServiceProvider decrypto = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider hash = new MD5CryptoServiceProvider();

                byte[] byteHash, byteBuff;

                string tempKey = key;

                byteHash = hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(tempKey));

                hash = null;

                decrypto.Key = byteHash;
                decrypto.Mode = CipherMode.ECB; //CBC, CFB

                byteBuff = Convert.FromBase64String(strEncrypted);

                string strDecrypted = ASCIIEncoding.ASCII.GetString(decrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
                decrypto = null;

                return strDecrypted;
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message + ".\n Decryption Failed. Please start over..!");
            }
        }
예제 #10
0
        public static string EncryptString(string needToEncrypt)
        {
            string encryptedstring;
            try
            {

                // Create a new instance of the TripleDESCryptoServiceProvider
                // class.  This generates a new key and initialization
                // vector (IV).
                using (TripleDESCryptoServiceProvider myTripleDES = new TripleDESCryptoServiceProvider())
                {
                    myTripleDES.Key = StringToByteArray(HttpContext.GetGlobalResourceObject("Global", "Key").ToString());
                    myTripleDES.IV = StringToByteArray(HttpContext.GetGlobalResourceObject("Global", "IV").ToString());
                    // Encrypt the string to an array of bytes.
                    byte[] encrypted = EncryptStringToBytes(needToEncrypt, myTripleDES.Key, myTripleDES.IV);
                    encryptedstring = ByteArrayToString(encrypted);

                    // Decrypt the bytes to a string.

                    string roundtrip2 = DecryptStringFromBytes(StringToByteArray(encryptedstring), myTripleDES.Key, myTripleDES.IV);

                }
                return encryptedstring;

            }
            catch (Exception ex)
            {
                return null;
            }
        }
예제 #11
0
 public static string Decrypt(string cipherText, string passPhrase)
 {
     try
     {
         TripleDESCryptoServiceProvider objDESCrypto =
             new TripleDESCryptoServiceProvider();
         MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
         byte[] byteHash, byteBuff;
         string strTempKey = passPhrase;
         byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
         objHashMD5 = null;
         objDESCrypto.Key = byteHash;
         objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
         byteBuff = Convert.FromBase64String(cipherText);
         string strDecrypted = ASCIIEncoding.ASCII.GetString
         (objDESCrypto.CreateDecryptor().TransformFinalBlock
         (byteBuff, 0, byteBuff.Length));
         objDESCrypto = null;
         return strDecrypted;
     }
     catch (Exception ex)
     {
         return null;
     }
 }
예제 #12
0
        /// <summary>
        /// Décrypte une chaine cryptée à partir d'un chiffreur symétrique
        /// </summary>
        /// <param name="base64String">chaine cryptée</param>
        /// <param name="pass">Mot de passe utilisé pour dériver la clé</param>
        /// <returns>Chaine décryptée</returns>
        private static string Decrypt(string base64String, string pass)
        {
            string result = string.Empty;

            System.Security.Cryptography.TripleDESCryptoServiceProvider des =
                new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            des.IV = new byte[8];
            System.Security.Cryptography.PasswordDeriveBytes pdb =
                new System.Security.Cryptography.PasswordDeriveBytes(pass, new byte[0]);
            des.Key = pdb.CryptDeriveKey("RC2", "SHA1", 128, new byte[8]);
            byte[] encryptedBytes = Convert.FromBase64String(base64String);

            using (MemoryStream ms = new MemoryStream(base64String.Length))
            {
                using (System.Security.Cryptography.CryptoStream decStream =
                           new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(),
                                                                         System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                    decStream.FlushFinalBlock();
                    byte[] plainBytes = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(plainBytes, 0, (int)ms.Length);
                    result = Encoding.UTF8.GetString(plainBytes);
                }
            }
            return(result);
        }
예제 #13
0
 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;
     }
 }
예제 #14
0
        /// <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);
        }
예제 #15
0
        /// <summary>
        /// A chave deve possuir 16 com caracteres "1234567890123456"
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetDescriptografiaSimetrica(this string str, string key)
        {
            using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
            {


                provider.Mode = CipherMode.CFB;
                provider.Padding = PaddingMode.PKCS7;
                byte[] inputEquivalent = Convert.FromBase64String(str);

                MemoryStream msDecrypt = new MemoryStream();

                CryptoStream csDecrypt = new CryptoStream(msDecrypt, provider.CreateDecryptor(Encoding.UTF8.GetBytes(key), new byte[] { 138, 154, 251, 188, 64, 108, 167, 121 }), CryptoStreamMode.Write);
                csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length);
                csDecrypt.FlushFinalBlock();

                csDecrypt.Close();

                str = Encoding.UTF8.GetString(msDecrypt.ToArray());
                msDecrypt.Close();
            }


            return str;
        }
예제 #16
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);
        }
예제 #17
0
        // ************************************** DEBUT 1 *****************************

        //public static string Encrypt(string original)
        //{
        //    MD5CryptoServiceProvider hashMd5 = new MD5CryptoServiceProvider();
        //    byte[] passwordHash = hashMd5.ComputeHash(
        //    UnicodeEncoding.Unicode.GetBytes(clefDuCryptage));

        //    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
        //    des.Key = passwordHash;

        //    des.Mode = CipherMode.ECB;

        //    byte[] buffer = UnicodeEncoding.Unicode.GetBytes(original);

        //    return UnicodeEncoding.Unicode.GetString(
        //    des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));

        //}

        //public static String Decrypt(String StringToDecrypt)
        //{

        //    String StringDecrypted = "";
        //    MD5CryptoServiceProvider hashMd5 = new MD5CryptoServiceProvider();
        //    byte[] passwordHash = hashMd5.ComputeHash(
        //    UnicodeEncoding.Unicode.GetBytes(clefDuCryptage));

        //    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
        //    des.Key = passwordHash;
        //    des.Mode = CipherMode.ECB;

        //    byte[] buffer = UnicodeEncoding.Unicode.GetBytes(StringToDecrypt);
        //    StringDecrypted = UnicodeEncoding.Unicode.GetString(
        //    des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));

        //    return StringDecrypted;
        //}

        // ************************************** FIN 1 *****************************



        // ************************************** DEBUT 2 *****************************
        //public static string Encrypt(string input, string key)
        //{
        //    byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);
        //    TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
        //    tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
        //    tripleDES.Mode = CipherMode.ECB;
        //    tripleDES.Padding = PaddingMode.PKCS7;
        //    ICryptoTransform cTransform = tripleDES.CreateEncryptor();
        //    byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
        //    tripleDES.Clear();
        //    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        //}

        //public static string Decrypt(string input, string key)
        //{
        //    byte[] inputArray = Convert.FromBase64String(input);
        //    TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
        //    tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
        //    tripleDES.Mode = CipherMode.ECB;
        //    tripleDES.Padding = PaddingMode.PKCS7;
        //    ICryptoTransform cTransform = tripleDES.CreateDecryptor();
        //    byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
        //    tripleDES.Clear();
        //    return UTF8Encoding.UTF8.GetString(resultArray);
        //}
        // **************************************  FIN 2 *****************************

        /// <summary>
        /// Crypte une chaine en utilisant un chiffreur symétrique
        /// </summary>
        /// <param name="plainText">Chaine à crypter</param>
        /// <param name="pass">Mot de passe utilisé pour dériver la clé</param>
        /// <returns>Chaine cryptée</returns>
        public static string Encrypt(string plainText, string pass)
        {
            string result = string.Empty;

            System.Security.Cryptography.TripleDESCryptoServiceProvider des =
                new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            des.IV = new byte[8];

            System.Security.Cryptography.PasswordDeriveBytes pdb =
                new System.Security.Cryptography.PasswordDeriveBytes(pass, new byte[0]);

            des.Key = pdb.CryptDeriveKey("RC2", "SHA1", 128, new byte[8]);

            using (MemoryStream ms = new MemoryStream(plainText.Length * 2))
            {
                using (System.Security.Cryptography.CryptoStream encStream = new
                                                                             System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(),
                                                                                                                       System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
                    encStream.Write(plainBytes, 0, plainBytes.Length);
                    encStream.FlushFinalBlock();
                    byte[] encryptedBytes = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(encryptedBytes, 0, (int)ms.Length);
                    encStream.Close();
                    ms.Close();
                    result = Convert.ToBase64String(encryptedBytes);
                }
            }
            return(result);
        }
예제 #18
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);
        }
예제 #19
0
        public static string TripleDESDecrypt(string pToDecrypt, string key = "")
        {
            string result;

            try
            {
                key = (string.IsNullOrWhiteSpace(key) ? EncryptHelper.strKey : key);
                byte[] array = new byte[pToDecrypt.Length / 2];
                for (int i = 0; i < pToDecrypt.Length / 2; i++)
                {
                    int num = System.Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 16);
                    array[i] = (byte)num;
                }
                TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
                tripleDESCryptoServiceProvider.Key  = System.Text.Encoding.ASCII.GetBytes(key);
                tripleDESCryptoServiceProvider.Mode = System.Security.Cryptography.CipherMode.ECB;
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, tripleDESCryptoServiceProvider.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
                cryptoStream.Write(array, 0, array.Length);
                cryptoStream.FlushFinalBlock();
                cryptoStream.Close();
                memoryStream.Close();
                result = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
            }
            catch (Exception ex)
            {
                result = ex.ToString();
            }
            return(result);
        }
예제 #20
0
        /// <summary>
        /// A chave deve possuir 16 com caracteres "1234567890123456"
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetCriptografiaSimetrica(this string str, string key)
        {
            using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
            {

                provider.Mode = CipherMode.CFB;
                provider.Padding = PaddingMode.PKCS7;

                MemoryStream mStream = new MemoryStream();

                CryptoStream cs = new CryptoStream(mStream, provider.CreateEncryptor(Encoding.UTF8.GetBytes(key), new byte[] { 138, 154, 251, 188, 64, 108, 167, 121 }), CryptoStreamMode.Write);

                byte[] toEncrypt = new UTF8Encoding().GetBytes(str);

                cs.Write(toEncrypt, 0, toEncrypt.Length);
                cs.FlushFinalBlock();
                byte[] ret = mStream.ToArray();

                mStream.Close();
                cs.Close();

                str = Convert.ToBase64String(ret);

            }


            return str;
        }
예제 #21
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);
        }
예제 #22
0
        internal static byte[] TripleDESKeyWrapDecrypt (byte[] rgbKey, byte[] rgbEncryptedWrappedKeyData) {
            // Check to see whether the length of the encrypted key is reasonable
            if (rgbEncryptedWrappedKeyData.Length != 32 && rgbEncryptedWrappedKeyData.Length != 40 
                && rgbEncryptedWrappedKeyData.Length != 48) 
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_KW_BadKeySize"));

            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            // Assume no padding, use CBC mode
            tripleDES.Padding = PaddingMode.None;
            ICryptoTransform dec1 = tripleDES.CreateDecryptor(rgbKey, s_rgbTripleDES_KW_IV);
            byte[] temp2 = dec1.TransformFinalBlock(rgbEncryptedWrappedKeyData, 0, rgbEncryptedWrappedKeyData.Length);
            Array.Reverse(temp2);
            // Get the IV and temp1
            byte[] rgbIV = new byte[8];
            Buffer.BlockCopy(temp2, 0, rgbIV, 0, 8);
            byte[] temp1 = new byte[temp2.Length - rgbIV.Length];
            Buffer.BlockCopy(temp2, 8, temp1, 0, temp1.Length);

            ICryptoTransform dec2 = tripleDES.CreateDecryptor(rgbKey, rgbIV);
            byte[] rgbWKCKS = dec2.TransformFinalBlock(temp1, 0, temp1.Length);

            // checksum the key
            byte[] rgbWrappedKeyData = new byte[rgbWKCKS.Length - 8];
            Buffer.BlockCopy(rgbWKCKS, 0, rgbWrappedKeyData, 0, rgbWrappedKeyData.Length);
            SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
            byte[] rgbCKS = sha.ComputeHash(rgbWrappedKeyData);
            for (int index = rgbWrappedKeyData.Length, index1 = 0; index < rgbWKCKS.Length; index++, index1++)
                if (rgbWKCKS[index] != rgbCKS[index1])
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_BadWrappedKeySize"));
            return rgbWrappedKeyData;
        }
예제 #23
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);
        }
예제 #24
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);
        }
예제 #25
0
        //
        // internal static methods
        //

        // CMS TripleDES KeyWrap as described in "http://www.w3.org/2001/04/xmlenc#kw-tripledes"
        internal static byte[] TripleDESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData) {
            // checksum the key
            SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
            byte[] rgbCKS = sha.ComputeHash(rgbWrappedKeyData);

            // generate a random IV
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] rgbIV = new byte[8];
            rng.GetBytes(rgbIV);

            // rgbWKCS = rgbWrappedKeyData | (first 8 bytes of the hash)
            byte[] rgbWKCKS = new byte[rgbWrappedKeyData.Length + 8];
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            // Don't add padding, use CBC mode: for example, a 192 bits key will yield 40 bytes of encrypted data
            tripleDES.Padding = PaddingMode.None;
            ICryptoTransform enc1 = tripleDES.CreateEncryptor(rgbKey, rgbIV);
            Buffer.BlockCopy(rgbWrappedKeyData, 0, rgbWKCKS, 0, rgbWrappedKeyData.Length);
            Buffer.BlockCopy(rgbCKS, 0, rgbWKCKS, rgbWrappedKeyData.Length, 8);
            byte[] temp1 = enc1.TransformFinalBlock(rgbWKCKS, 0, rgbWKCKS.Length);
            byte[] temp2 = new byte[rgbIV.Length + temp1.Length];
            Buffer.BlockCopy(rgbIV, 0, temp2, 0, rgbIV.Length);
            Buffer.BlockCopy(temp1, 0, temp2, rgbIV.Length, temp1.Length);
            // temp2 = REV (rgbIV | E_k(rgbWrappedKeyData | rgbCKS))
            Array.Reverse(temp2);

            ICryptoTransform enc2 = tripleDES.CreateEncryptor(rgbKey, s_rgbTripleDES_KW_IV);
            return enc2.TransformFinalBlock(temp2, 0, temp2.Length);
        }
예제 #26
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);            
        }
예제 #27
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;
            }
        }
예제 #28
0
        //Decryption Method

        public string DecryptTripleDES(string base64Text)
        {
            try
            {
                string Key    = Convert.ToString(configuration.GetSection("appSettings").GetSection("EncryptKey").Value);
                byte[] Buffer = new byte[0];

                System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();

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

                DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(Key));

                DES.Mode = System.Security.Cryptography.CipherMode.ECB;

                System.Security.Cryptography.ICryptoTransform DESDecrypt = DES.CreateDecryptor();

                Buffer = Convert.FromBase64String(base64Text);

                string DecTripleDES = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

                return(DecTripleDES);
            }
            catch (Exception)
            {
                return(string.Empty);
            }
        }
예제 #29
0
        }//end method

        /// <summary>
        /// 3des解密字符6
        /// </summary>
        /// <param name="a_strString">要解密的字符串</param>
        /// <param name="a_strKey">密钥</param>
        /// <returns>解密后的字符串</returns>
        /// <exception cref="">密钥错误</exception>
        /// <remarks>静态方法,采用默认utf8编码</remarks>
        public static string Decrypt3DES(this string a_strString, string a_strKey)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new
                                                                              System.Security.Cryptography.TripleDESCryptoServiceProvider();
            System.Security.Cryptography.MD5CryptoServiceProvider hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            DES.Key  = hashMD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(a_strKey));
            DES.Mode = System.Security.Cryptography.CipherMode.ECB;

            System.Security.Cryptography.ICryptoTransform DESDecrypt = DES.CreateDecryptor();

            string result = "";

            try
            {
                byte[] Buffer = Convert.FromBase64String(a_strString);
                result = System.Text.Encoding.UTF8.GetString(DESDecrypt.TransformFinalBlock
                                                                 (Buffer, 0, Buffer.Length));
            }
            catch (Exception e)
            {
#if DEBUG
                Console.WriteLine("错误:{0}", e);
#endif//DEBUG
                throw (new Exception("Invalid Key or input string is not a valid base64 string", e));
            }

            return(result);
        }//end method
예제 #30
0
        public static string EncryptString(string Message)
        {
            byte[] results;

            var hashProvider = new MD5CryptoServiceProvider();
            var tripleDESAlgorithm = new TripleDESCryptoServiceProvider() { Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
            tripleDESAlgorithm.Key = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(customPassPhrase));

            byte[] dataToEncrypt = Encoding.UTF8.GetBytes(Message);

            try
            {
                ICryptoTransform encryptor = tripleDESAlgorithm.CreateEncryptor();
                results = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
                encryptor.Dispose();
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                tripleDESAlgorithm.Clear();
                hashProvider.Clear();
            }

            return Convert.ToBase64String(results);
        }
예제 #31
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, 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);
        }
예제 #32
0
파일: Text.cs 프로젝트: sameesh-s/stego-app
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strToEncrypt"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static string Encrypt(string strToEncrypt, string key)
        {
            try
            {
                TripleDESCryptoServiceProvider crypto = new TripleDESCryptoServiceProvider();
                MD5CryptoServiceProvider hash = new MD5CryptoServiceProvider();

                byte[] byteHash, byteBuff;

                string strTempKey = key;

                byteHash = hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
                hash = null;

                crypto.Key = byteHash;
                crypto.Mode = CipherMode.ECB; //CBC, CFB

                byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);

                return Convert.ToBase64String(crypto.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
            }
            catch (Exception ex)
            {
                throw new Exception("Error: " + ex.Message + ".\n Encryption Failed. Please try again.");
            }
        }
예제 #33
0
        /// <summary>
        /// Ghi license ra file
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string ReadFile(string FilePath)
        {
            try
            {
                FileInfo fi = new FileInfo(FilePath);
                if (fi.Exists == false)
                    return string.Empty;

                FileStream fin = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
                TripleDES tdes = new TripleDESCryptoServiceProvider();
                CryptoStream cs = new CryptoStream(fin, tdes.CreateDecryptor(key, iv), CryptoStreamMode.Read);

                StringBuilder SB = new StringBuilder();
                int ch;
                for (int i = 0; i < fin.Length; i++)
                {
                    ch = cs.ReadByte();
                    if (ch == 0)
                        break;
                    SB.Append(Convert.ToChar(ch));
                }

                cs.Close();
                fin.Close();
                return SB.ToString();
            }
            catch(Exception ex)
            {
                return "";
            }
        }
        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);
        }
예제 #35
0
 /// <summary>
 /// constructor
 /// </summary>
 /// <param name="Text"></param>
 /// <param name="Icon"></param>
 public TSecureData(string[] Text, Image Icon) : base()
 {
     TDES = new TripleDESCryptoServiceProvider();
     FEncoding = new UTF8Encoding();
     FText = Text;
     FItem = Icon;
 }
 public static string CreateNewKey()
 {
     using (var des = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
     {
         des.GenerateKey();
         return(Convert.ToBase64String(des.Key));
     }
 }
예제 #37
0
        private static string Encrypt(string text, System.Security.Cryptography.TripleDESCryptoServiceProvider Des)
        {
            System.Security.Cryptography.ICryptoTransform desdencrypt = Des.CreateEncryptor();
            dynamic MyASCIIEncoding = new System.Text.ASCIIEncoding();

            byte[] buff = System.Text.ASCIIEncoding.ASCII.GetBytes(text);
            return(Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length)));
        }
예제 #38
0
 public static System.Security.Cryptography.ICryptoTransform GetEncServiceProvider(out byte[] key, out byte[] IV)
 {
     System.Security.Cryptography.TripleDESCryptoServiceProvider obj = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     obj.Mode = System.Security.Cryptography.CipherMode.CBC;
     key      = obj.Key;
     IV       = obj.IV;
     return(obj.CreateEncryptor());
 }
 //加密
 private static string DES3Encrypt(string data, string key)
 {
     System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     DES.Key     = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
     DES.Mode    = System.Security.Cryptography.CipherMode.ECB;
     DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor();
     byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(data);
     return(Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)));
 }
예제 #40
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);
 }
        public void PasswordDerivedBytes_Test()
        {
            byte[] randBytes = new byte[5];
            new Random(10032010).NextBytes(randBytes);


            var tdes  = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            var pwddb = new System.Security.Cryptography.PasswordDeriveBytes("1", new byte[] { 1 });

            tdes.Key = pwddb.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV);
            //string s = Convert.ToBase64String(tdes.Key);
        }
예제 #42
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);
 }
예제 #43
0
        /// <summary>
        /// 3des加密字符串
        /// </summary>
        /// <param name="a_strString">要加密的字符串</param>
        /// <param name="a_strKey">密钥</param>
        /// <returns>加密后并经base64编码的字符串</returns>
        /// <remarks>重载,指定编码方式</remarks>
        public static string Encrypt3DES(this string a_strString, string a_strKey)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new
                                                                              System.Security.Cryptography.TripleDESCryptoServiceProvider();
            System.Security.Cryptography.MD5CryptoServiceProvider hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            DES.Key  = hashMD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(a_strKey));
            DES.Mode = System.Security.Cryptography.CipherMode.ECB;

            System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            byte[] Buffer = System.Text.Encoding.UTF8.GetBytes(a_strString);
            return(Convert.ToBase64String(DESEncrypt.TransformFinalBlock
                                              (Buffer, 0, Buffer.Length)));
        }//end method
예제 #44
0
 /// <summary>
 /// DES多重加密
 /// </summary>
 /// <param name="str"></param>
 /// <returns></returns>
 public static String Encrypt_DES(String str)
 {
     System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     Byte[] inputByteArray = System.Text.Encoding.Unicode.GetBytes(str);
     des.Key = Convert.FromBase64String("uwniTq6wza2nU3/cCVxTScpjhlv1Tl5s");
     des.IV  = Convert.FromBase64String("ld6Et92CmbQ=");
     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.Text.StringBuilder sb = new System.Text.StringBuilder();
     foreach (Byte b in ms.ToArray())
     {
         sb.AppendFormat("{0:X2}", b);
     }
     return(sb.ToString());
 }
예제 #45
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);
        }
    public string DecryptTripleDES(string base64Text, string Key)
    {
        System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();

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

        DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(Key));

        DES.Mode = System.Security.Cryptography.CipherMode.ECB;

        System.Security.Cryptography.ICryptoTransform DESDecrypt = DES.CreateDecryptor();

        Buffer = Convert.FromBase64String(base64Text);

        string DecTripleDES = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

        return(DecTripleDES);
    }
예제 #47
0
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Decrypt_DES(string str)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            int x;

            byte[] inputByteArray = new byte[str.Length / 2];
            for (x = 0; x < str.Length / 2; x++)
            {
                inputByteArray[x] = (byte)(Convert.ToInt32(str.Substring(x * 2, 2), 16));
            }
            des.Key = Convert.FromBase64String(GetMD5Hash(key));
            des.IV  = Convert.FromBase64String("mjyxT92CmbQ=");
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            return(System.Text.Encoding.Unicode.GetString(ms.ToArray()));
        }
예제 #48
0
        public static string Operation(OperationType type, string Key, string text)
        {
            string _result = null;

            System.Security.Cryptography.MD5CryptoServiceProvider       hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            System.Security.Cryptography.TripleDESCryptoServiceProvider des     = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            des.Key  = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(Key));
            des.Mode = System.Security.Cryptography.CipherMode.ECB;
            if ((type == OperationType.Encrypt))
            {
                _result = Encrypt(text, des);
            }
            else
            {
                _result = Decrypt(text, des);
            }

            return(_result);
        }
        static byte[] TestEncrypt(string data)
        {
            byte[]    plainText = System.Text.Encoding.ASCII.GetBytes(data);
            TripleDES des3      = new System.Security.Cryptography.TripleDESCryptoServiceProvider();

            des3.Mode = CipherMode.CBC;
            des3.Key  = System.Text.Encoding.ASCII.GetBytes("12656b2e4ba2f22e");
            des3.IV   = System.Text.Encoding.ASCII.GetBytes("d566gdbc");
            ICryptoTransform transform = des3.CreateEncryptor();
            MemoryStream     memStreamEncryptedData = new MemoryStream();
            CryptoStream     encStream = new CryptoStream(memStreamEncryptedData,
                                                          transform, CryptoStreamMode.Write);

            encStream.Write(plainText, 0, plainText.Length);
            encStream.FlushFinalBlock();
            encStream.Close();
            byte[] cipherText = memStreamEncryptedData.ToArray();
            return(cipherText);
        }
예제 #50
0
        public static string EncryptTripleDES(string Plaintext, string Key)
        {
            byte[] Buffer = new byte[0];
            System.Security.Cryptography.TripleDESCryptoServiceProvider DES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();

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

            DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(Key));

            DES.Mode = System.Security.Cryptography.CipherMode.ECB;

            System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Plaintext);

            string TripleDES = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

            return(TripleDES);
        }
예제 #51
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);
        }
예제 #52
0
        /// <summary>
        /// DES多重加密解密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static String Decrypt_DES(String str)
        {
            str = str.Trim();
            System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            Int32 x;

            Byte[] inputByteArray = new Byte[str.Length / 2];
            for (x = 0; x < str.Length / 2; x++)
            {
                inputByteArray[x] = (Byte)(Convert.ToInt32(str.Substring(x * 2, 2), 16));
            }
            des.Key = Convert.FromBase64String("uwniTq6wza2nU3/cCVxTScpjhlv1Tl5s");
            des.IV  = Convert.FromBase64String("ld6Et92CmbQ=");
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            System.Text.StringBuilder ret = new System.Text.StringBuilder();
            return(System.Text.Encoding.Unicode.GetString(ms.ToArray()));
        }
예제 #53
0
 /// <summary>
 /// Giải mã chuỗi mã hóa
 /// </summary>
 /// <param name="toDecrypt">Chuỗi cần giải mã</param>
 /// <param name="key">khóa giải mã</param>
 /// <param name="useHashing">Giải mã chuỗi mã hóa MD5 hay mã hóa UTF8 bình thường</param>
 /// <returns>Chuỗi giải mã</returns>
 public static string _mDecrypt(string toDecrypt, string key, bool useHashing)
 {
     byte[] keyArray;
     byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
     if (useHashing)
     {
         System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
         keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
     }
     else
     {
         keyArray = UTF8Encoding.UTF8.GetBytes(key);
     }
     System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     tdes.Key     = keyArray;
     tdes.Mode    = System.Security.Cryptography.CipherMode.ECB;
     tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateDecryptor();
     byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
     return(UTF8Encoding.UTF8.GetString(resultArray));
 }
예제 #54
0
 private static string Decrypt(string text, System.Security.Cryptography.TripleDESCryptoServiceProvider Des)
 {
     System.Security.Cryptography.ICryptoTransform desdencrypt = Des.CreateDecryptor();
     byte[] buff = Convert.FromBase64String(text);
     return(System.Text.ASCIIEncoding.ASCII.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length)));
 }
예제 #55
0
 public static System.Security.Cryptography.ICryptoTransform GetDecServiceProvier(byte[] key, byte[] IV)
 {
     System.Security.Cryptography.TripleDESCryptoServiceProvider loc0 = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     loc0.Mode = System.Security.Cryptography.CipherMode.CBC;
     return(loc0.CreateDecryptor(key, IV));
 }