Beispiel #1
0
        public static string Criptografar(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[] DataToEncrypt = UTF8.GetBytes(Message);

            try
            {
                System.Security.Cryptography.ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();

                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            return(Convert.ToBase64String(Results));
        }
Beispiel #2
0
        public static string DS(string toEncrypt)
        {
            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 = UTF8Encoding.UTF8.GetBytes(toEncrypt);
                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.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                rt = System.Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch { rt = string.Empty; }
            return(rt);
        }
Beispiel #3
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 = (string)settingsReader.GetValue("SecurityKey", typeof(String));
            //System.Windows.Forms.MessageBox.Show(key);
            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.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tdes.Clear();
            return(Convert.ToBase64String(resultArray, 0, resultArray.Length));
        }
        private static string Encrypt(string hashKey, string strQueryStringParameter)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider hash_func = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] key = hash_func.ComputeHash(Encoding.ASCII.GetBytes(hashKey));
            byte[] IV  = new byte[hashKey.Length];

            System.Security.Cryptography.SHA1CryptoServiceProvider sha_func = new System.Security.Cryptography.SHA1CryptoServiceProvider();

            byte[] temp = sha_func.ComputeHash(Encoding.ASCII.GetBytes(hashKey));

            for (int i = 0; i < hashKey.Length; i++)
            {
                IV[i] = temp[hashKey.Length];
            }

            byte[] toenc = System.Text.Encoding.UTF8.GetBytes(strQueryStringParameter);

            System.Security.Cryptography.TripleDESCryptoServiceProvider des =
                new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            des.KeySize = 128;
            MemoryStream ms = new MemoryStream();

            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(
                ms,
                des.CreateEncryptor(key, IV),
                System.Security.Cryptography.CryptoStreamMode.Write
                );
            cs.Write(toenc, 0, toenc.Length);
            cs.FlushFinalBlock();

            return(Convert.ToBase64String(ms.ToArray()));
        }
Beispiel #5
0
        ///// <summary>
        ///// Gets the connection string settings for the connection string associated with the gallery data.
        ///// </summary>
        ///// <returns>An instance of <see cref="System.Configuration.ConnectionStringSettings" />.</returns>
        //public static System.Configuration.ConnectionStringSettings GetConnectionStringSettings()
        //{
        //  return System.Configuration.ConfigurationManager.ConnectionStrings.Cast<System.Configuration.ConnectionStringSettings>().First(cnStringObj => cnStringObj.Name == GetConnectionStringName());
        //}

        ///// <summary>
        ///// Gets the name of the connection string for the gallery data.
        ///// </summary>
        ///// <returns>System.String.</returns>
        //private static string GetConnectionStringName()
        //{
        //  using (var repo = new GalleryRepository())
        //  {
        //    return repo.ConnectionStringName;
        //  }
        //}


        /// <summary>
        /// Encrypt 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.
        /// The encrypted string can be decrypted to its original string using the <see cref="Decrypt" /> function in this class.
        /// </summary>
        /// <param name="plainText">A plain text string to be encrypted. 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 an encrypted version of the <paramref name="plainText" /> parameter.</returns>
        public static string Encrypt(string plainText, string encryptionKey)
        {
            if (String.IsNullOrEmpty(plainText))
            {
                return(String.Empty);
            }

            // This method (and the Decrypt method) inspired from Code Project.
            // http://www.codeproject.com/useritems/Cryptography.asp
            byte[] stringToEncryptArray = System.Text.Encoding.UTF8.GetBytes(plainText);

            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;

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

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

                // Return the encrypted data into unreadable string format
                return(Convert.ToBase64String(resultArray));
            }
        }
Beispiel #6
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)));
        }
Beispiel #7
0
        /// <summary>
        /// Encrypt given string
        /// </summary>
        /// <param name="s">Given String</param>
        public static string Encrypt(string s)
        {
            if (s.Equals(""))
            {
                return(s);
            }

            byte[] buff = ASCIIEncoding.ASCII.GetBytes(s);
            return(Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buff, 0, buff.Length)));
        }
Beispiel #8
0
        //Encryption method for credit card
        public string EncryptTripleDES(string Plaintext, 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 DESEncrypt = DES.CreateEncryptor();
            Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Plaintext);
            string TripleDES = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));

            return(TripleDES);
        }
        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));
        }
Beispiel #10
0
        /// <summary>
        /// Encrypts a string using a specified security key with
        /// the option to hash.
        /// </summary>
        /// <param name="toEncrypt">String to encrypt</param>
        /// <param name="securityKey">The key to apply to the encryption</param>
        /// <param name="useHashing">Weather hashing is used</param>
        /// <returns>The encrpyted string</returns>
        private static string Encrypt(string toEncrypt, string securityKey, bool useHashing)
        {
            string retVal = string.Empty;

            try
            {
                byte[] keyArray;
                byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(toEncrypt);
                // Validate inputs
                ValidateInput(toEncrypt);
                ValidateInput(securityKey);
                // If hashing use get hashcode regards to your key
                if (useHashing)
                {
                    System.Security.Cryptography.MD5CryptoServiceProvider hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    keyArray = hashmd5.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(securityKey));
                    // Always release the resources and flush data
                    // of the Cryptographic service provide. Best Practice
                    hashmd5.Clear();
                }
                else
                {
                    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.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
                retVal = Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(retVal);
        }
        public static string Crypt(string SourceText)
        {
            using (System.Security.Cryptography.TripleDESCryptoServiceProvider TripleDes = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
            {
                using (System.Security.Cryptography.MD5CryptoServiceProvider HashMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
                {
                    TripleDes.Key  = HashMD5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(Key));
                    TripleDes.Mode = System.Security.Cryptography.CipherMode.ECB;
                    System.Security.Cryptography.ICryptoTransform desdencrypt = TripleDes.CreateEncryptor();
                    byte[] buff = System.Text.Encoding.UTF8.GetBytes(SourceText);

                    return(System.Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length)));
                }
            }

            return(string.Empty);
        }
Beispiel #12
0
 /// <summary>
 /// encrypt the string with a specific key value
 /// </summary>
 /// <param name="value"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public string Encrypt(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 encryptor.
     System.Security.Cryptography.ICryptoTransform DESEncrypt = DES.CreateEncryptor();
     // Get a byte array of the string.
     byte[] Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(value);
     // Transform and return the string.
     return(Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)));
 }
Beispiel #13
0
        } // End Function DeCrypt

        public static string Crypt(string SourceText)
        {
            string strReturnValue = "";

            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.CreateEncryptor();
                    byte[] buff = System.Text.Encoding.UTF8.GetBytes(SourceText);

                    strReturnValue = System.Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
                } // End Using HashMD5
            }     // End UsingDes

            return(strReturnValue);
        } // End Function Crypt
Beispiel #14
0
 private static string EnCrypt(string strEnCrypt, string key)
 {
     try
     {
         byte[] keyArr;
         byte[] EnCryptArr = UTF8Encoding.UTF8.GetBytes(strEnCrypt);
         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.CreateEncryptor();
         byte[] arrResult = transform.TransformFinalBlock(EnCryptArr, 0, EnCryptArr.Length);
         return(Convert.ToBase64String(arrResult, 0, arrResult.Length));
     }
     catch (Exception ex) { }
     return(string.Empty);
 }
Beispiel #15
0
        public static string Encrypt(this String Plaintext)
        {
            string TrippleDESEncryptedOutput = "";

            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 DESEncrypt = DES.CreateEncryptor();
                Buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Plaintext);
                TrippleDESEncryptedOutput = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
                return(TrippleDESEncryptedOutput);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #16
0
 public static string DS(string toEncrypt)
 {
     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 = UTF8Encoding.UTF8.GetBytes(toEncrypt);
         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.CreateEncryptor();
         byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
         rt = System.Convert.ToBase64String(resultArray, 0, resultArray.Length);
     }
     catch { rt = string.Empty; }
     return rt;
 }
Beispiel #17
0
        public static string Encrypt3DES(string msg, string key, string iv)
        {
            string resutlt = string.Empty;

            byte [] arrMsg = System.Text.Encoding.UTF8.GetBytes(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.CreateEncryptor();

            try {
                var arrResult = ct.TransformFinalBlock(arrMsg, 0, arrMsg.Length);
                resutlt = Convert.ToBase64String(arrResult);
            } catch {
            }

            return(resutlt);
        }
Beispiel #18
0
        public static byte[] CompressEncrypt(byte[] raw)
        {
            byte[] zippedBytes;
            using (var memory = new MemoryStream())
            {
                using (var gzip = new GZipStream(memory, CompressionMode.Compress, true))
                    gzip.Write(raw, 0, raw.Length);
                zippedBytes = memory.ToArray();
            }
            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.CreateEncryptor(key, iv);

            using (var stream = new MemoryStream())
            {
                using (var cryptoStream = new System.Security.Cryptography.CryptoStream(stream, transform, System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    cryptoStream.Write(zippedBytes, 0, zippedBytes.Length);
                    cryptoStream.FlushFinalBlock();
                    return(stream.ToArray());
                }
            }
        }
        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));
        }
Beispiel #20
0
        public string Encrypt(string plaintext)
        {
            System.Security.Cryptography.TripleDESCryptoServiceProvider cipher = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
            cipher.BlockSize = 64;
            string cipherText;

            using (System.IO.MemoryStream cipherTextStream = new System.IO.MemoryStream())
            {
                using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(cipherTextStream, cipher.CreateEncryptor(this.Key, this.IV), System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(plaintext);
                    cryptoStream.Write(bytes, 0, bytes.Length);
                    cryptoStream.FlushFinalBlock();
                    cipherText = Convert.ToBase64String(cipherTextStream.ToArray());
                }
            }

            return(cipherText);
        }
        public static string Crypt(string SourceText)
        {
            string strReturnValue = "";

            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.CreateEncryptor();
                    byte[] buff = System.Text.Encoding.UTF8.GetBytes(SourceText);

                    strReturnValue = System.Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
                } // End Using HashMD5

            } // End UsingDes

            return strReturnValue;
        }
Beispiel #22
0
 private static string EnCrypt(string strEnCrypt, string key)
 {
     try
     {
         byte[] keyArr;
         byte[] EnCryptArr = UTF8Encoding.UTF8.GetBytes(strEnCrypt);
         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.CreateEncryptor();
         byte[] arrResult = transform.TransformFinalBlock(EnCryptArr, 0, EnCryptArr.Length);
         return Convert.ToBase64String(arrResult, 0, arrResult.Length);
     }
     catch (Exception ex) { }
     return string.Empty;
 }
        /// <summary>
        /// Encrypt the specified string using the System.Security.Cryptography.TripleDESCryptoServiceProvider cryptographic
        /// service provider. The secret key used in the encryption is specified in the encryptionKey configuration setting.
        /// The encrypted string can be decrypted to its original string using the Decrypt function in this class.
        /// </summary>
        /// <param name="plainText">A plain text string to be encrypted. If the value is null or empty, the return value is
        /// equal to String.Empty.</param>
        /// <returns>Returns an encrypted version of the plainText parameter.</returns>
        public static string Encrypt(string plainText)
        {
            if (String.IsNullOrEmpty(plainText))
                return String.Empty;

            // This method (and the Decrypt method) inspired from Code Project.
            // http://www.codeproject.com/useritems/Cryptography.asp
            byte[] stringToEncryptArray = System.Text.Encoding.UTF8.GetBytes(plainText);

            using (System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider())
            {
                // Set the secret key for the tripleDES algorithm
                byte[] keyArray = EncryptionKey;
                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;

                // Transform the specified region of bytes array to resultArray
                System.Security.Cryptography.ICryptoTransform cTransform = tdes.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(stringToEncryptArray, 0, stringToEncryptArray.Length);

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

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