示例#1
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);
        }
示例#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 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);
        }
示例#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));
        }
        /// <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);
        }
示例#6
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;
            }
        }
示例#7
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);
        }
 public static string md5Hash(this Bitmap bitmap)
 {
     try
     {
         if (bitmap.isNull())
             return null;
         //based on code snippets from http://dotnet.itags.org/dotnet-c-sharp/85838/
         using (var strm = new MemoryStream())
         {
             var image = new Bitmap(bitmap);
             bitmap.Save(strm, System.Drawing.Imaging.ImageFormat.Bmp);
             strm.Seek(0, 0);
             byte[] bytes = strm.ToArray();
             var md5 = new MD5CryptoServiceProvider();
             byte[] hashed = md5.TransformFinalBlock(bytes, 0, bytes.Length);
             string hash = BitConverter.ToString(hashed).ToLower();
             md5.Clear();
             image.Dispose();
             return hash;
         }
     }
     catch (Exception ex)
     {
         ex.log("in bitmap.md5Hash");
         return "";
     }
 }
示例#9
0
    public static string MD5(this string value)
    {
        //MD5ハッシュ値を計算する文字列
        string s = value;

        //文字列をbyte型配列に変換する
        byte[] data = System.Text.Encoding.UTF8.GetBytes(s);

        //MD5CryptoServiceProviderオブジェクトを作成
        System.Security.Cryptography.MD5CryptoServiceProvider md5 =
            new System.Security.Cryptography.MD5CryptoServiceProvider();
        //または、次のようにもできる
        //System.Security.Cryptography.MD5 md5 =
        //    System.Security.Cryptography.MD5.Create();

        //ハッシュ値を計算する
        byte[] bs = md5.ComputeHash(data);

        //リソースを解放する
        md5.Clear();

        //byte型配列を16進数の文字列に変換
        System.Text.StringBuilder result = new System.Text.StringBuilder();
        foreach (byte b in bs)
        {
            result.Append(b.ToString("x2"));
        }
        //ここの部分は次のようにもできる
        //string result = BitConverter.ToString(bs).ToLower().Replace("-","");

        //結果を表示
        return(result.ToString());
    }
示例#10
0
 /// <summary>
 /// MD5加密 返回纯字节
 /// </summary>
 /// <param name="k"></param>
 /// <returns></returns>
 public static byte[] MD5Bytes(string sourceString)
 {
     MD5 md5 = new MD5CryptoServiceProvider();
     byte[] keyBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(sourceString));
     md5.Clear();
     return keyBytes;
 }
示例#11
0
        private static string generateKey(string str)
        {
            if (null == str)
            {
                str = "defaultpassword";
            }
            else if (str.Length < 1)
            {
                str = "emptypassword";
            }
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.GetEncoding(ENCODE).GetBytes(str);
            bytes = md5.ComputeHash(bytes);
            md5.Clear();

            String strret = Convert.ToBase64String(bytes);

            while (strret.Length < 16)
            {
                strret += "%";
            }
            if (strret.Length > 16)
            {
                int nbegin = (strret.Length - 16) / 2;
                strret = strret.Substring(nbegin, 16);
            }
            return(strret);
        }
示例#12
0
        public static string Decrypt(this string cipherText, string encryptionKey)
        {
            // if(cipherText.Contains(" "))
            cipherText = cipherText.Replace(" ", "+");

            byte[] resultArray;

            var toEncryptArray = Convert.FromBase64String(cipherText);

            byte[] keyArray;

            // If hashing was used get the hash code with regards to your key
            using (var hashmd5 = new MD5CryptoServiceProvider())
            {
                keyArray = hashmd5.ComputeHash(Encoding.ASCII.GetBytes(encryptionKey));
                hashmd5.Clear();
            }

            // Set the secret key for the tripleDES algorithm
            using (
                var tdes = new TripleDESCryptoServiceProvider
                {
                    Key = keyArray,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                })
            {
                var cTransform = tdes.CreateDecryptor();
                resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                tdes.Clear();
            }

            // Return the Clear decrypted TEXT
            return Encoding.ASCII.GetString(resultArray);
        }
        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);
        }
示例#14
0
        static byte[] CreateKeyDigest(String password, byte[] docIdData)
        {
            Check16Bytes(docIdData, "docId");
            int nChars = Math.Min(password.Length, 16);
            byte[] passwordData = new byte[nChars * 2];
            for (int i = 0; i < nChars; i++)
            {
                char ch = password[i];
                passwordData[i * 2 + 0] = (byte)((ch << 0) & 0xFF);
                passwordData[i * 2 + 1] = (byte)((ch << 8) & 0xFF);
            }

            byte[] kd;
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] passwordHash = md5.ComputeHash(passwordData);

            md5.Clear();
            md5.Initialize();

            byte[] data=new byte[passwordHash.Length*16 + docIdData.Length*16];

            int offset=0;
            for (int i = 0; i < 16; i++)
            {
                Array.Copy(passwordHash, 0, data, offset, PASSWORD_HASH_NUMBER_OF_BYTES_USED);
                offset+=passwordHash.Length;
                Array.Copy(docIdData,0,data,offset,docIdData.Length);
                offset += docIdData.Length;                
            }
            kd = md5.ComputeHash(data);
            byte[] result = new byte[KEY_DIGEST_LENGTH];
            Array.Copy(kd, 0, result, 0, KEY_DIGEST_LENGTH);
            return result;
        }
示例#15
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;
            }
        }
        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();

            }
        }
        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();
            }
        }
示例#18
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);
 }
示例#19
0
        /// <summary>
        /// Returnes the string as MD5 hashing
        /// </summary>
        /// <param name="datastr"></param>
        /// <returns></returns>
        public static string EncryptString(string datastr)
        {
            HashAlgorithm mhash = new MD5CryptoServiceProvider();
              string res = string.Empty; // the returning result

              // Convert the original string to array of Bytes
              byte[] bytValue = Encoding.UTF8.GetBytes(datastr);

              // Compute the Hash, returns an array of Bytes
              byte[] bytHash = mhash.ComputeHash(bytValue);

              mhash.Clear();

              // convert the byte data to hex string values
              for (int i = 0; i < bytHash.Length; i++)
              {
            if (bytHash[i] < 16)
            {
              res += "0" + bytHash[i].ToString("x");
            }
            else
            {
              res += bytHash[i].ToString("x");
            }
              }

              return res;
        }
示例#20
0
        /// <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);
        }
        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);
        }
示例#22
0
        public string DecryptKey(string clave)
        {
            string key = "**H0H**D*L";
            byte[] keyArray;
            //convierte el texto en una secuencia de bytes
            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();

            return UTF8Encoding.UTF8.GetString(resultArray);
        }
示例#23
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;
            }
        }
示例#24
0
 static public string CalcETag(string str)
 {
     System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
     byte[] hash = md5.ComputeHash(System.Text.Encoding.Unicode.GetBytes(str));
     md5.Clear();
     return(Convert.ToBase64String(hash));
 }
示例#25
0
 private static byte[] makeMD5(byte[] key)
 {
     MD5CryptoServiceProvider MD5Provider = new MD5CryptoServiceProvider();
     byte[] keyhash = MD5Provider.ComputeHash(key);
     MD5Provider.Clear();
     return keyhash;
 }
示例#26
0
        /// <summary>
        /// 计算MD5
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string GetMD5(String s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return("");
            }

            //获取加密服务
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            //获取要加密的字段,并转化为Byte[]数组
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);

            //加密Byte[]数组
            bytes = md5.ComputeHash(bytes);
            md5.Clear();

            StringBuilder strReturn = new StringBuilder();

            for (int i = 0; i < bytes.Length; i++)
            {
                strReturn.Append(Convert.ToString(bytes[i], 16).PadLeft(2, '0'));
            }

            return(strReturn.ToString().PadLeft(32, '0'));
        }
示例#27
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);
        }
示例#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);
        }
示例#29
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);
        }
        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;
        }
示例#31
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);
        }
        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);
        }
示例#33
0
        /// <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;
            }
        }
示例#34
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;
     }
 }
        /// <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);
        }
示例#36
0
 public static string md5str(string s)
 {
     byte[] data = System.Text.Encoding.UTF8.GetBytes(s);
     System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
     byte[] bs = md5.ComputeHash(data);
     md5.Clear();
     return(BitConverter.ToString(bs).ToLower().Replace("-", ""));
 }
示例#37
0
    // 生成更新包列表文件
    void GenPatcherLst()
    {
        string sPatchLstPath = m_sPatcherDir + "/" + ms_sPatchLstFileName;

        string[]      files      = Directory.GetFiles(m_sPatcherDir, "*" + PackageScatteredApp.ms_sPatcherFileType, SearchOption.TopDirectoryOnly);
        List <string> lstPatcher = new List <string>();

        for (int i = 0; i < files.Length; i++)
        {
            string sNewFileName = files[i].Substring(0, files[i].LastIndexOf(".")).Substring(m_sPatcherDir.Length + 1);
            Debug.Log(files[i] + " <=> " + sNewFileName);
            string[] sNewVer = sNewFileName.Split('_');
            if (sNewVer.Length != 3)
            {
                continue;
            }
            int nPos = lstPatcher.Count;
            for (int j = lstPatcher.Count - 1; j >= 0; j--)
            {
                string   sFileName = lstPatcher[j];
                string[] sVer      = sFileName.Split('_');
                if (sVer.Length != 3)
                {
                    continue;
                }
                if (-1 == CompareVer(sNewVer[1], sVer[1]) || (0 == CompareVer(sNewVer[1], sVer[1]) && 1 == CompareVer(sNewVer[2], sVer[2])))
                {
                    nPos = j;
                }
            }
            lstPatcher.Insert(nPos, sNewFileName);
        }
        // 生成更新包列表文件
        JsonData jd = new JsonData();

        jd.SetJsonType(JsonType.Array);
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bt;
        string sMd5 = "";

        for (int i = 0; i < lstPatcher.Count; i++)
        {
            Debug.Log("GenPatcherLst: " + lstPatcher[i]);
            bt   = File.ReadAllBytes(m_sPatcherDir + "/" + lstPatcher[i] + PackageScatteredApp.ms_sPatcherFileType);
            sMd5 = BitConverter.ToString(md5.ComputeHash(bt));
            JsonData jdd = new JsonData();
            jdd.SetJsonType(JsonType.Object);
            jdd["file"] = lstPatcher[i] + PackageScatteredApp.ms_sPatcherFileType;
            jdd["md5"]  = sMd5;
            jdd["size"] = (bt.Length / 1024.0f / 1024.0f).ToString();
            jd.Add(jdd);
        }
        md5.Clear();
        Util.writeJson(jd, sPatchLstPath);
    }
示例#38
0
    /// <summary>
    /// バイト列をMD5ハッシュ化します。
    /// </summary>
    /// <returns><c>true</c> if hash byteData; otherwise, <c>false</c>.</returns>
    /// <param name="byteData">Byte data.</param>
    public static byte[] Hash(byte[] byteData)
    {
        // MD5CryptoServiceProviderオブジェクトを作成
        System.Security.Cryptography.MD5CryptoServiceProvider md5 =
            new System.Security.Cryptography.MD5CryptoServiceProvider();
        // ハッシュ値を計算する
        byte[] hashCode = md5.ComputeHash(byteData);
        // 閉じる
        md5.Clear();

        return(hashCode);
    }
    /// <summary>
    /// 二进制转成Md5
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static string GetMD5(this byte[] bytes)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] bytHash = md5.ComputeHash(bytes);
        md5.Clear();
        string sTemp = "";

        for (int i = 0; i < bytHash.Length; i++)
        {
            sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
        }
        return(sTemp.ToLower());
    }
示例#40
0
    public static string MD5(byte[] content)
    {
        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] hash = md5.ComputeHash(content);
        md5.Clear();

        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i = 0; i < hash.Length; ++i)
        {
            sb.Append(hash[i].ToString("X2"));
        }
        return(sb.ToString());
    }
示例#41
0
        public static string HashString(string Value)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.ASCII.GetBytes(Value);
            data = x.ComputeHash(data);
            string ret = "";

            for (int i = 0; i < data.Length; i++)
            {
                ret += data[i].ToString("x2").ToLower();
            }
            x.Clear();
            return(ret);
        }
示例#42
0
        public static String Md5(string s)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
            bytes = md5.ComputeHash(bytes);
            md5.Clear();
            string ret = "";

            for (int i = 0; i < bytes.Length; i++)
            {
                ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
            }
            return(ret.PadLeft(32, '0'));
        }
示例#43
0
        public static string md5_32(string s)
        {
            System.Security.Cryptography.MD5 mD = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] array = System.Text.Encoding.UTF8.GetBytes(s);
            array = mD.ComputeHash(array);
            mD.Clear();
            string text = "";

            for (int i = 0; i < array.Length; i++)
            {
                text += System.Convert.ToString(array[i], 16).PadLeft(2, '0');
            }
            return(text.PadLeft(32, '0'));
        }
示例#44
0
    public static string Md5(Stream stream)
    {
        stream.Seek(0, SeekOrigin.Begin);
        var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

        byte[] hash_byte = md5.ComputeHash(stream);

        var md5str = System.BitConverter.ToString(hash_byte);

        md5str = md5str.Replace("-", "");

        md5.Clear();
        return(md5str);
    }
    public static string GetMD5(this Stream sDataIn)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        md5.Initialize();
        byte[] bytes = md5.ComputeHash(sDataIn);
        md5.Clear();
        StringBuilder sb = new StringBuilder();

        foreach (byte b in bytes)
        {
            sb.Append(b.ToString("x2"));
        }

        return(sb.ToString());
    }
示例#46
0
    public static string Md5(string data)
    {
        var md5       = new System.Security.Cryptography.MD5CryptoServiceProvider();
        var byteValue = System.Text.Encoding.UTF8.GetBytes(data);
        var byteHash  = md5.ComputeHash(byteValue);

        md5.Clear();
        var result = "";

        for (var i = 0; i < byteHash.Length; i++)
        {
            result += byteHash[i].ToString("X").PadLeft(2, '0');
        }
        return(result.ToLower());
    }
示例#47
0
        private static string EncryptByMD5(string md5)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] value, hash;
            value = System.Text.Encoding.UTF8.GetBytes(md5);
            hash  = md.ComputeHash(value);
            md.Clear();
            string temp = "";

            for (int i = 0, len = hash.Length; i < len; i++)
            {
                temp += hash[i].ToString("X").PadLeft(4, '0');
            }
            return(temp);
        }
示例#48
0
        public static string MD5Encrypt(string sDataIn, string move)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] byt, bytHash;
            byt     = System.Text.Encoding.UTF8.GetBytes(move + sDataIn);
            bytHash = md5.ComputeHash(byt);
            md5.Clear();
            string sTemp = "";

            for (int i = 0; i < bytHash.Length; i++)
            {
                sTemp += bytHash[i].ToString("x").PadLeft(2, '0');
            }
            return(sTemp);
        }
示例#49
0
        ///// <summary>
        ///// 用MD5加密字符串
        ///// </summary>
        ///// <param name="assemble">待加密的字符串</param>
        ///// <returns></returns>
        //public static string MD5Encrypt(string assemble)
        //{
        //    string cl = assemble;
        //    string pwd = "";
        //    MD5 md5 = MD5.Create();//实例化一个md5对像
        //                           // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
        //    byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
        //    // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
        //    for (int i = 0; i < s.Length; i++)
        //    {
        //        // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
        //        pwd = pwd + s[i].ToString("x");
        //    }
        //    return pwd;
        //}

        /// <summary>
        /// 取MD5
        /// </summary>
        /// <param name="sDataIn"></param>
        /// <returns></returns>
        public static string GetMD5(string sDataIn)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytValue, bytHash;
            bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
            bytHash  = md5.ComputeHash(bytValue);
            md5.Clear();
            string sTemp = "";

            for (int i = 0; i < bytHash.Length; i++)
            {
                sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
            }
            return(sTemp.ToLower());
        }
示例#50
0
        private string GetMd5(string str)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] byt, bytHash;
            byt     = System.Text.Encoding.UTF8.GetBytes(str);
            bytHash = md5.ComputeHash(byt);
            md5.Clear();
            string sTemp = "";

            for (int i = 0; i < bytHash.Length; i++)
            {
                sTemp += bytHash[i].ToString("x").PadLeft(2, '0');
            }
            return(sTemp);
        }
示例#51
0
        //工具类:
        //1、MD5处理  Md5Encrypt
        //2、生成唯一流水号
        //3、AES加密、解密处理
        //4、RSA公钥加密、解密、验签处理

        //---------------------------------------MD5处理------------------------------------------------------------

        public static String Md5Encrypt(String strSource)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 =
                new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytValue, bytHash;
            bytValue = System.Text.Encoding.UTF8.GetBytes("" + strSource);
            bytHash  = md5.ComputeHash(bytValue);
            md5.Clear();
            string sTemp = "";

            for (int i = 0; i < bytHash.Length; i++)
            {
                sTemp += bytHash[i].ToString("x").PadLeft(2, '0');
            }
            return(sTemp);
        }
示例#52
0
    public static string md5str(string content)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] data    = System.Text.Encoding.UTF8.GetBytes(content);
        byte[] md5Data = md5.ComputeHash(data, 0, data.Length);
        md5.Clear();

        string destString = "";

        for (int i = 0; i < md5Data.Length; i++)
        {
            destString += System.Convert.ToString(md5Data[i], 16).PadLeft(2, '0');
        }
        destString = destString.PadLeft(32, '0');
        return(destString);
    }
示例#53
0
        /// <summary>
        /// 计算MD5 16位
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string Md5Hex(String str)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
            bytes = md5.ComputeHash(bytes);
            md5.Clear();

            StringBuilder strReturn = new StringBuilder();

            for (int i = 0; i < bytes.Length; i++)
            {
                strReturn.Append(Convert.ToString(bytes[i], 16).PadLeft(2, '0'));
            }

            return(strReturn.ToString().PadLeft(32, '0'));
        }
示例#54
0
        public static string GetMD5(string md5, Encoding encode)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] value, hash;
            value = encode.GetBytes(md5);
            hash  = md.ComputeHash(value);
            md.Clear();
            string temp = "";

            for (int i = 0, len = hash.Length; i < len; i++)
            {
                temp += hash[i].ToString("X").PadLeft(2, '0');
            }

            return(temp);
        }
示例#55
0
        public static IEnumerable <string> getMd5(string md5)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] value, hash;
            value = System.Text.Encoding.UTF8.GetBytes(md5);
            hash  = md.ComputeHash(value);
            md.Clear();
            string temp = "";

            for (int i = 0, len = hash.Length; i < len; i++)
            {
                temp += hash[i].ToString("x").PadLeft(2, '0');
            }

            yield return(temp);
        }
示例#56
0
    public static string getFileMd5(string file)
    {
        FileStream f = new FileStream(file, FileMode.Open);

        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] retVal = md5.ComputeHash(f);
        f.Close();
        md5.Clear();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < retVal.Length; i++)
        {
            sb.Append(retVal[i].ToString("x2"));
        }
        return(sb.ToString());
    }
示例#57
0
        public static string Sinature(string s)
        {
            var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] bytes    = System.Text.Encoding.UTF8.GetBytes(s);
            byte[] md5Bytes = md5.ComputeHash(bytes);
            md5.Clear();
            string ret = "";

            for (int i = 0; i < md5Bytes.Length; i++)
            {
                ret += md5Bytes[i].ToString("x");
            }

            return(ret);
        }
示例#58
0
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="sDataIn">要加密的字符串</param>
        /// <param name="move">偏移量</param>
        /// <returns>加密后的字符串</returns>
        public static string MD5(string sDataIn, string move)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytValue, bytHash;
            bytValue = System.Text.Encoding.UTF8.GetBytes(move + sDataIn);
            bytHash  = md5.ComputeHash(bytValue); md5.Clear();
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < bytHash.Length; i++)
            {
                builder.Append(bytHash[i].ToString("x").PadLeft(2, '0'));
            }
            string sTemp = builder.ToString();

            return(sTemp);
        }
示例#59
0
    /// <summary>
    /// チェックサム(MD5)を取得
    /// </summary>
    public string GetMD5()
    {
        //ref http://dobon.net/vb/dotnet/string/md5.html
        var bytes = GetBin();
        var md5   = new System.Security.Cryptography.MD5CryptoServiceProvider();
        var bs    = md5.ComputeHash(bytes);

        md5.Clear();

        var result = new System.Text.StringBuilder();

        foreach (var b in bs)
        {
            result.Append(b.ToString("x2"));
        }

        return(result.ToString());
    }
示例#60
0
    public static string Hash_MD5_32(string word, bool toUpper = true)
    {
        try
        {
            System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP
                = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
            byte[] bytHash  = MD5CSP.ComputeHash(bytValue);
            MD5CSP.Clear();

            //根据计算得到的Hash码翻译为MD5码
            string sHash = "", sTemp = "";
            for (int counter = 0; counter < bytHash.Count(); counter++)
            {
                long i = bytHash[counter] / 16;
                if (i > 9)
                {
                    sTemp = ((char)(i - 10 + 0x41)).ToString();
                }
                else
                {
                    sTemp = ((char)(i + 0x30)).ToString();
                }
                i = bytHash[counter] % 16;
                if (i > 9)
                {
                    sTemp += ((char)(i - 10 + 0x41)).ToString();
                }
                else
                {
                    sTemp += ((char)(i + 0x30)).ToString();
                }
                sHash += sTemp;
            }

            //根据大小写规则决定返回的字符串
            return(toUpper ? sHash : sHash.ToLower());
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }