Exemplo n.º 1
0
        public static string Descriptografar(string Message)
        {
            byte[] Results;

            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

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

            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(senha));

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

            TDESAlgorithm.Key = TDESKey;

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

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

            byte[] DataToDecrypt = Convert.FromBase64String(Message);

            try
            {
                System.Security.Cryptography.ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();

                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            return(UTF8.GetString(Results));
        }
Exemplo n.º 2
0
        // http://www.codeproject.com/KB/aspnet/ASPNET_20_Webconfig.aspx
        // http://www.codeproject.com/KB/database/Connection_Strings.aspx
        public static string DeCrypt(string SourceText)
        {
            string strReturnValue = "";

            if (string.IsNullOrEmpty(SourceText))
            {
                return(strReturnValue);
            } // End if (string.IsNullOrEmpty(SourceText))


            using (System.Security.Cryptography.TripleDESCryptoServiceProvider Des = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
            {
                using (System.Security.Cryptography.MD5CryptoServiceProvider HashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
                {
                    string strSymmetricKey = GetSymmetricKey();
                    Des.Key  = HashMD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSymmetricKey));
                    Des.Mode = System.Security.Cryptography.CipherMode.ECB;

                    System.Security.Cryptography.ICryptoTransform desdencrypt = Des.CreateDecryptor();
                    byte[] buff = System.Convert.FromBase64String(SourceText);
                    strReturnValue = System.Text.Encoding.UTF8.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
                } // End Using HashMD5
            }     // End Using Des

            return(strReturnValue);
        } // End Function DeCrypt
Exemplo n.º 3
0
        public static string Decrypt3DES(string msg, string key, string iv)
        {
            string result = string.Empty;

            byte [] arrMsg = Convert.FromBase64String(msg);

            var provider = new System.Security.Cryptography.TripleDESCryptoServiceProvider()
            {
                Key     = System.Text.Encoding.UTF8.GetBytes(GF.getMD5(key)),               //Convert.FromBase64String(GF.getMD5(key)),
                IV      = System.Text.Encoding.UTF8.GetBytes(iv),
                Mode    = System.Security.Cryptography.CipherMode.CBC,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

            System.Security.Cryptography.ICryptoTransform ct = provider.CreateDecryptor();

            try {
                var arrResult = ct.TransformFinalBlock(arrMsg, 0, arrMsg.Length);
                result = System.Text.Encoding.UTF8.GetString(arrResult);
            } catch {
                //Console.Write ("X");
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Decrypt the specified string using the <see cref="System.Security.Cryptography.TripleDESCryptoServiceProvider" /> cryptographic
        /// service provider. It is expected that <see cref="Business.Interfaces.IAppSetting.EncryptionKey" /> is used for the encryption key.
        /// </summary>
        /// <param name="encryptedText">A string to be decrypted. The encrypted string should have been encrypted using the
        /// <see cref="Encrypt" /> function in this class. If the value is null or empty, the return value is equal to String.Empty.</param>
        /// <param name="encryptionKey">The encryption key.</param>
        /// <returns>Returns the original, unencrypted string contained in the <paramref name="encryptedText" /> parameter.</returns>
        /// <exception cref="System.FormatException">Thrown when the text cannot be decrypted.</exception>
        public static string Decrypt(string encryptedText, string encryptionKey)
        {
            if (String.IsNullOrEmpty(encryptedText))
            {
                return(String.Empty);
            }

            // Get the byte code of the string
            byte[] toEncryptArray = Convert.FromBase64String(encryptedText);

            using (var tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
            {
                // Set the secret key for the tripleDES algorithm.
                tdes.Key = System.Text.Encoding.UTF8.GetBytes(encryptionKey);

                // 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;

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

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

                // Return the Clear decrypted TEXT
                return(System.Text.Encoding.UTF8.GetString(resultArray));
            }
        }
Exemplo n.º 5
0
        public static string ES(string toDecrypt)
        {
            string key = "k29vn - Đặng Đức Kiên";

            if (string.IsNullOrEmpty(MrkKEY))
            {
                key = "k29vn - Đặng Đức Kiên";
            }
            else
            {
                key = MrkKEY;
            }
            string rt = string.Empty;

            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = System.Convert.FromBase64String(toDecrypt);
                System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(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);
                rt = UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch { rt = string.Empty; }
            return(rt);
        }
Exemplo n.º 6
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 = (string)settingsReader.GetValue("SecurityKey", typeof(String));

            if (useHashing)
            {
                System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                hashmd5.Clear();
            }
            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);

            tdes.Clear();
            return(UTF8Encoding.UTF8.GetString(resultArray));
        }
Exemplo n.º 7
0
        public static byte[] GetEmbeddedBytes(String file)
        {
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(file))
            {
                if (stream != null)
                {
                    var assemblyData = new Byte[stream.Length];
                    stream.Read(assemblyData, 0, assemblyData.Length);

                    var    key       = System.Text.Encoding.ASCII.GetBytes("t7n6cVWf9Tbns0eI");
                    var    iv        = System.Text.Encoding.ASCII.GetBytes("9qh17ZUf");
                    var    provider  = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
                    var    transform = provider.CreateDecryptor(key, iv);
                    byte[] bytes;
                    using (var cstream = new MemoryStream())
                    {
                        using (var cryptoStream = new System.Security.Cryptography.CryptoStream(cstream, transform, System.Security.Cryptography.CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(assemblyData, 0, assemblyData.Length);
                            cryptoStream.FlushFinalBlock();
                            bytes = cstream.ToArray();

                            using (var compressedStream = new MemoryStream(bytes))
                                using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
                                    using (var resultStream = new MemoryStream())
                                    {
                                        zipStream.CopyTo(resultStream);
                                        return(resultStream.ToArray());
                                    }
                        }
                    }
                }
            }
            return(null);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Decrypt given string
        /// </summary>
        /// <param name="x">Given String</param>
        public static string Decrypt(string x)
        {
            if (x.Equals(""))
            {
                return(x);
            }

            byte[] buff = Convert.FromBase64String(x);
            return(ASCIIEncoding.ASCII.GetString(
                       des.CreateDecryptor().TransformFinalBlock(buff, 0, buff.Length)));
        }
        private static string EncryptDecryptText(bool isEncode, string text, string encryptionKey)
        {
            // Step 1. Hash the encryptionKey using 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 System.Security.Cryptography.MD5CryptoServiceProvider();
            var tdesKey      = hashProvider.ComputeHash(Encoding.Unicode.GetBytes(encryptionKey));

            // Step 2. Create a new TripleDESCryptoServiceProvider object
            var tdesAlgorithm = new System.Security.Cryptography.TripleDESCryptoServiceProvider
            {
                Key  = tdesKey,
                Mode =
                    System.Security
                    .Cryptography
                    .CipherMode.ECB,
                Padding =
                    System.Security
                    .Cryptography
                    .PaddingMode.PKCS7
            };

            // Step 3. Setup the encoder

            // Step 4. Convert the input text to a byte[]
            byte[] dataToEncryptDecrypt = isEncode ? Encoding.Unicode.GetBytes(text) : Convert.FromBase64String(text);

            // Step 5. Attempt to encrypt/Decrypt the string
            byte[] encryptDecryptedBytes;
            try
            {
                if (isEncode)
                {
                    var encryptor = tdesAlgorithm.CreateEncryptor();
                    encryptDecryptedBytes = encryptor.TransformFinalBlock(dataToEncryptDecrypt, 0,
                                                                          dataToEncryptDecrypt.Length);
                }
                else
                {
                    var decryptor = tdesAlgorithm.CreateDecryptor();
                    encryptDecryptedBytes = decryptor.TransformFinalBlock(dataToEncryptDecrypt, 0,
                                                                          dataToEncryptDecrypt.Length);
                }
            }
            finally
            {
                // Clear the TripleDes and Hashprovider services of any sensitive information
                tdesAlgorithm.Clear();
                hashProvider.Clear();
            }

            // Step 6. Return the encrypted string as a base64 encoded string
            return(isEncode ? Convert.ToBase64String(encryptDecryptedBytes) : Encoding.Unicode.GetString(encryptDecryptedBytes));
        }
Exemplo n.º 10
0
        //Decryption Method
        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);
        }
Exemplo n.º 11
0
 /// <summary>
 /// decrypt the string
 /// </summary>
 /// <param name="value"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public string Decrypt(string value, string key)
 {
     System.Security.Cryptography.TripleDESCryptoServiceProvider DES     = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
     System.Security.Cryptography.MD5CryptoServiceProvider       hashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
     // scramble the key
     key = ScrambleKey(key);
     // Compute the MD5 hash.
     DES.Key = hashMD5.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key));
     // Set the cipher mode.
     DES.Mode = System.Security.Cryptography.CipherMode.ECB;
     // Create the decryptor.
     System.Security.Cryptography.ICryptoTransform DESDecrypt = DES.CreateDecryptor();
     byte[] Buffer = Convert.FromBase64String(value);
     // Transform and return the string.
     return(System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)));
 }
Exemplo n.º 12
0
        /// <summary>
        /// Decrypts a specified key against the original security
        /// key, with the option to hash.
        /// </summary>
        /// <param name="cipherString">String to decrypt</param>
        /// <param name="securityKey">The original security key</param>
        /// <param name="useHashing">Weather hashing is enabled</param>
        /// <returns>The decrypted key</returns>
        private static string Decrypt(string cipherString, string securityKey, bool useHashing)
        {
            string retVal = string.Empty;

            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = Convert.FromBase64String(cipherString);
                // Validate inputs
                ValidateInput(cipherString);
                ValidateInput(securityKey);
                if (useHashing)
                {
                    // If hashing was used get the hash code with regards to your key
                    System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(securityKey));
                    // Release any resource held by the MD5CryptoServiceProvider
                    hashmd5.Clear();
                }
                else
                {
                    // If hashing was not implemented get the byte code of the key
                    keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(securityKey);
                }
                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.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                // Release resources held by TripleDes Encryptor
                tdes.Clear();
                // Return the Clear decrypted TEXT
                retVal = System.Text.UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(retVal);
        }
Exemplo n.º 13
0
 private static string DeCrypt(string strDecypt, string key)
 {
     try
     {
         byte[] keyArr;
         byte[] DeCryptArr = Convert.FromBase64String(strDecypt);
         System.Security.Cryptography.MD5CryptoServiceProvider MD5Hash = new System.Security.Cryptography.MD5CryptoServiceProvider();
         keyArr = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
         System.Security.Cryptography.TripleDESCryptoServiceProvider tripDes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
         tripDes.Key     = keyArr;
         tripDes.Mode    = System.Security.Cryptography.CipherMode.ECB;
         tripDes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
         System.Security.Cryptography.ICryptoTransform transform = tripDes.CreateDecryptor();
         byte[] arrResult = transform.TransformFinalBlock(DeCryptArr, 0, DeCryptArr.Length);
         return(UTF8Encoding.UTF8.GetString(arrResult));
     }
     catch (Exception ex) { }
     return(string.Empty);
 }
Exemplo n.º 14
0
        private static string Decrypt(string cipherString)
        {
            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = Convert.FromBase64String(cipherString);

                //Di default disabilito l'hashing
                bool useHashing = false;

                //La chiave deve essere di 24 caratteri
                string key = "ValueTeamDocsPa3Services";

                if (useHashing)
                {
                    System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                    hashmd5.Clear();
                }
                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);

                tdes.Clear();
                return(UTF8Encoding.UTF8.GetString(resultArray));
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 15
0
        public static string Decrypt(this String base64Text)
        {
            string TripleDESDecyrptedOutput = "";

            try
            {
                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("252121112237244914192"));
                DES.Mode = System.Security.Cryptography.CipherMode.ECB;
                System.Security.Cryptography.ICryptoTransform DESDecrypt = DES.CreateDecryptor();
                Buffer = Convert.FromBase64String(base64Text);
                TripleDESDecyrptedOutput = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
                return(TripleDESDecyrptedOutput);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 16
0
 public static string ES(string toDecrypt)
 {
     string key = "k29vn - Đặng Đức Kiên";
     if (string.IsNullOrEmpty(MrkKEY)) key = "k29vn - Đặng Đức Kiên";
     else key = MrkKEY;
     string rt = string.Empty;
     try
     {
         byte[] keyArray;
         byte[] toEncryptArray = System.Convert.FromBase64String(toDecrypt);
         System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
         keyArray = hashmd5.ComputeHash(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);
         rt = UTF8Encoding.UTF8.GetString(resultArray);
     }
     catch { rt = string.Empty; }
     return rt;
 }
        static void Main(string[] args)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider tripleDES = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            byte[] data = Encoding.UTF8.GetBytes("This is a sample message");
            byte[] key  = Encoding.UTF8.GetBytes("NOSTROMOHASSOMEGODPOWERS");
            tripleDES.Key = key;
            tripleDES.IV  = new byte[tripleDES.BlockSize / 8];
            var encryptor = tripleDES.CreateEncryptor();

            byte[] result = new byte[data.Length];
            result = encryptor.TransformFinalBlock(data, 0, data.Length);
            string res = BitConverter.ToString(result).Replace("-", "");

            Console.WriteLine(BitConverter.ToString(result).Replace("-", ""));

            byte[] data2 = result;
            tripleDES.Key = key;
            tripleDES.IV  = new byte[tripleDES.BlockSize / 8];
            var decryptor = tripleDES.CreateDecryptor();

            byte[] result2 = new byte[data2.Length];
            result2 = decryptor.TransformFinalBlock(data2, 0, data2.Length);
            Console.WriteLine(Encoding.UTF8.GetString(result2));
        }
Exemplo n.º 18
0
        public string Decrypt(string ciphertext)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider cipher = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            cipher.BlockSize = 64;
            string plaintext;

            using (System.IO.MemoryStream plaintextStream = new System.IO.MemoryStream())
            {
                using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(plaintextStream, cipher.CreateDecryptor(this.Key, this.IV), System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    byte[] bytes = Convert.FromBase64String(ciphertext);
                    cryptoStream.Write(bytes, 0, bytes.Length);
                    cryptoStream.FlushFinalBlock();
                    plaintext = Encoding.UTF8.GetString(plaintextStream.ToArray());
                }
            }

            return(plaintext);
        }
Exemplo n.º 19
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)));
 }
Exemplo n.º 20
0
        //Protected Shared strSymmetricKey As String = "Als symmetrischer Key kann irgendein Text verwendet werden. äöü'"
        // http://www.codeproject.com/KB/aspnet/ASPNET_20_Webconfig.aspx
        // http://www.codeproject.com/KB/database/Connection_Strings.aspx
        public static string DeCrypt(string SourceText)
        {
            string strReturnValue = "";

            if (string.IsNullOrEmpty(SourceText))
            {
                return strReturnValue;
            } // End if (string.IsNullOrEmpty(SourceText))

            using (System.Security.Cryptography.TripleDESCryptoServiceProvider Des = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
            {

                using (System.Security.Cryptography.MD5CryptoServiceProvider HashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
                {
                    Des.Key = HashMD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSymmetricKey));
                    Des.Mode = System.Security.Cryptography.CipherMode.ECB;

                    System.Security.Cryptography.ICryptoTransform desdencrypt = Des.CreateDecryptor();
                    byte[] buff = System.Convert.FromBase64String(SourceText);
                    strReturnValue = System.Text.Encoding.UTF8.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
                } // End Using HashMD5

            } // End Using Des

            return strReturnValue;
        }
Exemplo n.º 21
0
 private static string DeCrypt(string strDecypt, string key)
 {
     try
     {
         byte[] keyArr;
         byte[] DeCryptArr = Convert.FromBase64String(strDecypt);
         System.Security.Cryptography.MD5CryptoServiceProvider MD5Hash = new System.Security.Cryptography.MD5CryptoServiceProvider();
         keyArr = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
         System.Security.Cryptography.TripleDESCryptoServiceProvider tripDes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
         tripDes.Key = keyArr;
         tripDes.Mode = System.Security.Cryptography.CipherMode.ECB;
         tripDes.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
         System.Security.Cryptography.ICryptoTransform transform = tripDes.CreateDecryptor();
         byte[] arrResult = transform.TransformFinalBlock(DeCryptArr, 0, DeCryptArr.Length);
         return UTF8Encoding.UTF8.GetString(arrResult);
     }
     catch (Exception ex) { }
     return string.Empty;
 }
Exemplo n.º 22
0
        /// <summary>
        /// Decrypt the specified string using the System.Security.Cryptography.TripleDESCryptoServiceProvider cryptographic
        /// service provider. The secret key used in the decryption is specified in the encryptionKey configuration setting.
        /// </summary>
        /// <param name="encryptedText">A string to be decrypted. The encrypted string should have been encrypted using the
        /// Encrypt function in this class. If the value is null or empty, the return value is equal to String.Empty.</param>
        /// <returns>
        /// Returns the original, unencrypted string contained in the encryptedText parameter.
        /// </returns>
        /// <exception cref="System.FormatException">Thrown when the text cannot be decrypted.</exception>
        public static string Decrypt(string encryptedText)
        {
            if (String.IsNullOrEmpty(encryptedText))
                return String.Empty;

            // Get the byte code of the string
            byte[] toEncryptArray = Convert.FromBase64String(encryptedText);

            using (System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
            {
                // Set the secret key for the tripleDES algorithm.
                tdes.Key = EncryptionKey;

                // 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.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

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

                // Return the Clear decrypted TEXT
                return System.Text.Encoding.UTF8.GetString(resultArray);
            }
        }