public CryptDeriveKey ( string algname, string alghashname, int keySize, byte rgbIV ) : byte[] | ||
algname | string | |
alghashname | string | |
keySize | int | |
rgbIV | byte | |
return | byte[] |
// ************************************** DEBUT 1 ***************************** //public static string Encrypt(string original) //{ // MD5CryptoServiceProvider hashMd5 = new MD5CryptoServiceProvider(); // byte[] passwordHash = hashMd5.ComputeHash( // UnicodeEncoding.Unicode.GetBytes(clefDuCryptage)); // TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); // des.Key = passwordHash; // des.Mode = CipherMode.ECB; // byte[] buffer = UnicodeEncoding.Unicode.GetBytes(original); // return UnicodeEncoding.Unicode.GetString( // des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length)); //} //public static String Decrypt(String StringToDecrypt) //{ // String StringDecrypted = ""; // MD5CryptoServiceProvider hashMd5 = new MD5CryptoServiceProvider(); // byte[] passwordHash = hashMd5.ComputeHash( // UnicodeEncoding.Unicode.GetBytes(clefDuCryptage)); // TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); // des.Key = passwordHash; // des.Mode = CipherMode.ECB; // byte[] buffer = UnicodeEncoding.Unicode.GetBytes(StringToDecrypt); // StringDecrypted = UnicodeEncoding.Unicode.GetString( // des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length)); // return StringDecrypted; //} // ************************************** FIN 1 ***************************** // ************************************** DEBUT 2 ***************************** //public static string Encrypt(string input, string key) //{ // byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input); // TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider(); // tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key); // tripleDES.Mode = CipherMode.ECB; // tripleDES.Padding = PaddingMode.PKCS7; // ICryptoTransform cTransform = tripleDES.CreateEncryptor(); // byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length); // tripleDES.Clear(); // return Convert.ToBase64String(resultArray, 0, resultArray.Length); //} //public static string Decrypt(string input, string key) //{ // byte[] inputArray = Convert.FromBase64String(input); // TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider(); // tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key); // tripleDES.Mode = CipherMode.ECB; // tripleDES.Padding = PaddingMode.PKCS7; // ICryptoTransform cTransform = tripleDES.CreateDecryptor(); // byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length); // tripleDES.Clear(); // return UTF8Encoding.UTF8.GetString(resultArray); //} // ************************************** FIN 2 ***************************** /// <summary> /// Crypte une chaine en utilisant un chiffreur symétrique /// </summary> /// <param name="plainText">Chaine à crypter</param> /// <param name="pass">Mot de passe utilisé pour dériver la clé</param> /// <returns>Chaine cryptée</returns> public static string Encrypt(string plainText, string pass) { string result = string.Empty; System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider(); des.IV = new byte[8]; System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(pass, new byte[0]); des.Key = pdb.CryptDeriveKey("RC2", "SHA1", 128, new byte[8]); using (MemoryStream ms = new MemoryStream(plainText.Length * 2)) { using (System.Security.Cryptography.CryptoStream encStream = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)) { byte[] plainBytes = Encoding.UTF8.GetBytes(plainText); encStream.Write(plainBytes, 0, plainBytes.Length); encStream.FlushFinalBlock(); byte[] encryptedBytes = new byte[ms.Length]; ms.Position = 0; ms.Read(encryptedBytes, 0, (int)ms.Length); encStream.Close(); ms.Close(); result = Convert.ToBase64String(encryptedBytes); } } return(result); }
public byte[] Decrypt(byte[] encrypted, byte[] publicKey) { RijndaelManaged objCrypRij = new RijndaelManaged(); byte[] bytIV = GetMD5EncodeBytes(publicKey); objCrypRij.IV = bytIV; PasswordDeriveBytes pdb = new PasswordDeriveBytes(GetDecodeBytes(publicKey), new byte[0]); objCrypRij.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]); byte[] encryptedBytes = encrypted; MemoryStream msText = new MemoryStream(encrypted.GetLength(0)); CryptoStream decStream = new CryptoStream(msText, objCrypRij.CreateDecryptor(), CryptoStreamMode.Write); decStream.Write(encryptedBytes, 0, encryptedBytes.Length); decStream.FlushFinalBlock(); byte[] plainBytes = new byte[msText.Length]; msText.Position = 0; msText.Read(plainBytes, 0, Convert.ToInt32(msText.Length)); decStream.Close(); return plainBytes; }
public string TripleDESDecode(string str) { try { if (str.Length <= 0) return null; TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); Byte[] iv = new Byte[8]; ; des.IV = iv; PasswordDeriveBytes pdb = new PasswordDeriveBytes("St$$#ef)!en", Encoding.ASCII.GetBytes("St$$#ef)!en".Length.ToString())); des.Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, des.IV); Byte[] encryptedbytes = Convert.FromBase64String(str); MemoryStream ms = new MemoryStream(str.Length); CryptoStream decstream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); decstream.Write(encryptedbytes, 0, encryptedbytes.Length); decstream.FlushFinalBlock(); Byte[] plainbytes = ms.ToArray(); ms.Position = 0; ms.Read(plainbytes, 0, (int)(ms.Length - 1)); decstream.Close(); return System.Text.Encoding.UTF8.GetString(plainbytes); } catch { return null; } }
/// <summary> /// Décrypte une chaine cryptée à partir d'un chiffreur symétrique /// </summary> /// <param name="base64String">chaine cryptée</param> /// <param name="pass">Mot de passe utilisé pour dériver la clé</param> /// <returns>Chaine décryptée</returns> private static string Decrypt(string base64String, string pass) { string result = string.Empty; System.Security.Cryptography.TripleDESCryptoServiceProvider des = new System.Security.Cryptography.TripleDESCryptoServiceProvider(); des.IV = new byte[8]; System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(pass, new byte[0]); des.Key = pdb.CryptDeriveKey("RC2", "SHA1", 128, new byte[8]); byte[] encryptedBytes = Convert.FromBase64String(base64String); using (MemoryStream ms = new MemoryStream(base64String.Length)) { using (System.Security.Cryptography.CryptoStream decStream = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)) { decStream.Write(encryptedBytes, 0, encryptedBytes.Length); decStream.FlushFinalBlock(); byte[] plainBytes = new byte[ms.Length]; ms.Position = 0; ms.Read(plainBytes, 0, (int)ms.Length); result = Encoding.UTF8.GetString(plainBytes); } } return(result); }
public void PasswordDerivedBytes_Test() { byte[] randBytes = new byte[5]; new Random(10032010).NextBytes(randBytes); var tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider(); var pwddb = new System.Security.Cryptography.PasswordDeriveBytes("1", new byte[] { 1 }); tdes.Key = pwddb.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV); //string s = Convert.ToBase64String(tdes.Key); }
public void PasswordDerivedBytes_Test() { byte[] randBytes = new byte[5]; new Random(10032010).NextBytes(randBytes); var tdes = new TripleDESCryptoServiceProvider(); var pwddb = new PasswordDeriveBytes("1", new byte[] { 1 }); tdes.Key = pwddb.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV); //string s = Convert.ToBase64String(tdes.Key); }
public EncryptionTransform(String password) { PasswordDeriveBytes passwordBytes = new PasswordDeriveBytes(password + paranoidSaltString,salt); // Create a TripleDESCryptoServiceProvider object. TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); tdes.Mode = CipherMode.ECB; // Create the key and add it to the Key property. tdes.Key = passwordBytes.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV); decrypter = tdes.CreateDecryptor(); encrypter = tdes.CreateEncryptor(); }
private ICryptoTransform CreateTranformer(bool forEncryption) { byte[] key = null; byte[] pdbsalt = null; byte[] iv = null; try { // Salt byte array. pdbsalt = GenerateSalt(); // Create PasswordDeriveBytes object that will generate // a Key for TripleDES algorithm. PasswordDeriveBytes pdb = new PasswordDeriveBytes(Key, pdbsalt); iv = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 }; // Create a private key for TripleDES algorithm. // The iv parameter is not currently used. // * http://blogs.msdn.com/shawnfa/archive/2004/04/14/113514.aspx key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, iv); if (forEncryption) { return TripleDES.Create().CreateEncryptor(key, iv); } else { return TripleDES.Create().CreateDecryptor(key, iv); } } catch (CryptographicException) { return null; } finally { if (key != null) { Array.Clear(key, 0, key.Length); } if (pdbsalt != null) { Array.Clear(pdbsalt, 0, pdbsalt.Length); } if (iv != null) { Array.Clear(iv, 0, iv.Length); } } }
public static string decriptUrl(string Valor) { string Chave = "#Cho053D4t@R0r!s#"; Valor = Valor.Replace("MAIS", "+"); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.IV = new byte[8]; PasswordDeriveBytes pdb = new PasswordDeriveBytes(Chave, new byte[-1 + 1]); des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]); byte[] encryptedBytes = Convert.FromBase64String(Valor); System.IO.MemoryStream MS = new System.IO.MemoryStream(Valor.Length); CryptoStream decStreAM = new CryptoStream(MS, des.CreateDecryptor(), CryptoStreamMode.Write); decStreAM.Write(encryptedBytes, 0, encryptedBytes.Length); decStreAM.FlushFinalBlock(); byte[] plainBytes = new byte[Convert.ToInt32(MS.Length - 1) + 1]; MS.Position = 0; MS.Read(plainBytes, 0, Convert.ToInt32(MS.Length)); decStreAM.Close(); return System.Text.Encoding.UTF8.GetString(plainBytes); }
public static string SimpleSymmetricDecrypt(this string data, string key) { TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.IV = new byte[8]; PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, new byte[0]); des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]); byte[] encryptedBytes = Convert.FromBase64String(data); MemoryStream ms = new MemoryStream(data.Length); CryptoStream decStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); decStream.Write(encryptedBytes, 0, encryptedBytes.Length); decStream.FlushFinalBlock(); byte[] plainBytes = new byte[ms.Length]; ms.Position = 0; ms.Read(plainBytes, 0, (int)ms.Length); decStream.Close(); return Encoding.UTF8.GetString(plainBytes); }
public static string criptUrl(string valor) { string chave = "#Cho053D4t@R0r!s#"; TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.IV = new byte[8]; PasswordDeriveBytes pdb = new PasswordDeriveBytes(chave, new byte[-1 + 1]); des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]); System.IO.MemoryStream ms = new System.IO.MemoryStream((valor.Length * 2) - 1); CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(valor); encStream.Write(plainBytes, 0, plainBytes.Length); encStream.FlushFinalBlock(); byte[] encryptedBytes = new byte[Convert.ToInt32(ms.Length - 1) + 1]; ms.Position = 0; ms.Read(encryptedBytes, 0, Convert.ToInt32(ms.Length)); encStream.Close(); string c = null; c = Convert.ToBase64String(encryptedBytes).Replace("+", "MAIS"); return c; }
private static byte[] EncryptBytes(byte[] SecureBytes, string PrivateKey) { byte[] array = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(PrivateKey, array); byte[] array2 = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] key = passwordDeriveBytes.CryptDeriveKey("TripleDES", "MD5", 0, array2); TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider(); tripleDESCryptoServiceProvider.Key = key; tripleDESCryptoServiceProvider.Mode = CipherMode.ECB; return tripleDESCryptoServiceProvider.CreateEncryptor().TransformFinalBlock(SecureBytes, 0, SecureBytes.Length); }
private static void InitKeys() { lock (typeof(CryptConfig)) { string apath = HttpContext.Current.Request.ApplicationPath; Configuration config = WebConfigurationManager.OpenWebConfiguration(apath); MachineKeySection key = (MachineKeySection)config.GetSection("system.web/machineKey"); _crypt_pass = key.DecryptionKey; _hmac_pass = key.ValidationKey; PasswordDeriveBytes pdb = new PasswordDeriveBytes(_hmac_pass, null); byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; _hmac_key = pdb.CryptDeriveKey("RC2", "SHA1", 128, iv); _crypt_key = pdb.CryptDeriveKey("RC2", "SHA1", 128, iv); } }
/// <summary> /// Public constructor. /// </summary> public Encryptor() { // FIXME: AAA - need support for key and salt changing. What's best interface? byte[] salt = Esapi.SecurityConfiguration().MasterSalt; string pass = Esapi.SecurityConfiguration().MasterPassword; // setup algorithms encryptAlgorithm = Esapi.SecurityConfiguration().EncryptionAlgorithm; signatureAlgorithm = Esapi.SecurityConfiguration().DigitalSignatureAlgorithm; randomAlgorithm = Esapi.SecurityConfiguration().RandomAlgorithm; hashAlgorithm = Esapi.SecurityConfiguration().HashAlgorithm; try { // Set up encryption and decryption SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.Create(encryptAlgorithm); symmetricAlgorithm.GenerateIV(); iv = symmetricAlgorithm.IV; symmetricAlgorithm.Padding = PaddingMode.PKCS7; PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(pass, salt); // FIXME: We are using SHA1 hardcoded here, because for some reason CryptDeriveKey doesn't // like other hash algorithms. Also, it appears to not like Rijndael as a encryption algorithm. secretKey = passwordDeriveBytes.CryptDeriveKey(encryptAlgorithm, "SHA1", symmetricAlgorithm.KeySize, iv); encoding = Esapi.SecurityConfiguration().CharacterEncoding; // 13 is the code for DSA asymmetricKeyPair = new CspParameters(13); // The asymmetric key will be stored in the key container using the name ESAPI. asymmetricKeyPair.KeyContainerName = "ESAPI"; // Set up signing keypair using the master password and salt // FIXME: Enhance - make DSA configurable RandomNumberGenerator randomNumberGenerator = RNGCryptoServiceProvider.Create(randomAlgorithm); } catch (Exception e) { // can't throw this exception in initializer, but this will log it new EncryptionException("Encryption failure", "Error creating Encryptor", e); } }
public void CryptDeriveKey_TooLongKey () { PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "MD5", 1000); pd.CryptDeriveKey ("AlgName", "MD5", -256, new byte [8]); }
public static string Decrypt(string datastr) { PasswordDeriveBytes bytes = new PasswordDeriveBytes("qianxun8", null); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); byte[] rgbKey = bytes.CryptDeriveKey("DES", "SHA1", 0, new byte[8]); byte[] buffer = Encoding.Unicode.GetBytes(datastr); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(rgbKey, rgbKey), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); return Encoding.Unicode.GetString(stream.ToArray()); }
private string decryptTripleDes(string encryptedBase64, string Password) { string password = Password; shiftLeft(ref password, 7); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.IV = new byte[8]; PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]); des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]); byte[] encryptedBytes = Convert.FromBase64String(encryptedBase64); MemoryStream ms = new MemoryStream(encryptedBase64.Length); CryptoStream decStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); decStream.Write(encryptedBytes, 0, encryptedBytes.Length); decStream.FlushFinalBlock(); byte[] plainBytes = new byte[ms.Length]; ms.Position = 0; ms.Read(plainBytes, 0, (int)ms.Length); decStream.Close(); return Encoding.UTF8.GetString(plainBytes); }
public static byte[] TRIPLEDESEncrypt(string Data, string Password, byte[] Key, byte[] IV) { try { var pdb = new PasswordDeriveBytes(Password, Key); Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, IV); //Key = pdb.GetBytes(Key.Length); // Create a MemoryStream. var mStream = new MemoryStream(); // Create a CryptoStream using the MemoryStream // and the passed key and initialization vector (IV). var cStream = new CryptoStream(mStream, new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV), CryptoStreamMode.Write); // Convert the passed string to a byte array. var toEncrypt = new UnicodeEncoding().GetBytes(Data); // Write the byte array to the crypto stream and flush it. cStream.Write(toEncrypt, 0, toEncrypt.Length); cStream.FlushFinalBlock(); // Get an array of bytes from the // MemoryStream that holds the // encrypted data. var ret = mStream.ToArray(); // Close the streams. cStream.Close(); mStream.Close(); // Return the encrypted buffer. return ret; } catch (CryptographicException e) { Console.WriteLine(@"A Cryptographic error occurred: {0}", e.Message); return null; } }
public static string TRIPLEDESDecrypt(byte[] Data, string Password, byte[] Key, byte[] IV) { try { var pdb = new PasswordDeriveBytes(Password, Key); Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, IV); //Key = pdb.GetBytes(Key.Length); // Create a new MemoryStream using the passed // array of encrypted data. var msDecrypt = new MemoryStream(Data); // Create a CryptoStream using the MemoryStream // and the passed key and initialization vector (IV). var csDecrypt = new CryptoStream(msDecrypt, new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV), CryptoStreamMode.Read); // Create buffer to hold the decrypted data. var fromEncrypt = new byte[Data.Length]; // Read the decrypted data out of the crypto stream // and place it into the temporary buffer. csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); //Convert the buffer into a string and return it. return new UnicodeEncoding().GetString(fromEncrypt); } catch (CryptographicException e) { Console.WriteLine(@"A Cryptographic error occurred: {0}", e.Message); return null; } }
public string EncryptMessage(string plainMessage, string password) { TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.IV = new byte[8]; PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]); des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]); MemoryStream ms = new MemoryStream(plainMessage.Length * 2); CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); byte[] plainBytes = Encoding.UTF8.GetBytes(plainMessage); encStream.Write(plainBytes, 0, plainBytes.Length); encStream.FlushFinalBlock(); byte[] encryptedBytes = new byte[ms.Length]; ms.Position = 0; ms.Read(encryptedBytes, 0, (int)ms.Length); encStream.Close(); return Convert.ToBase64String(encryptedBytes); }