public static string Decrypt(string CipherText, string Password, string Salt = "Kosher", string HashAlgorithm = "SHA1", int PasswordIterations = 2, string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (string.IsNullOrEmpty(CipherText)) { return(""); } byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector); byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt); byte[] CipherTextBytes = Convert.FromBase64String(CipherText); System.Security.Cryptography.PasswordDeriveBytes DerivedPassword = new System.Security.Cryptography.PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations); byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8); System.Security.Cryptography.RijndaelManaged SymmetricKey = new System.Security.Cryptography.RijndaelManaged(); SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; byte[] PlainTextBytes = new byte[CipherTextBytes.Length]; int ByteCount = 0; using (System.Security.Cryptography.ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes)) { using (System.IO.MemoryStream MemStream = new System.IO.MemoryStream(CipherTextBytes)) { using (System.Security.Cryptography.CryptoStream CryptoStream = new System.Security.Cryptography.CryptoStream(MemStream, Decryptor, System.Security.Cryptography.CryptoStreamMode.Read)) { ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length); MemStream.Close(); CryptoStream.Close(); } } } SymmetricKey.Clear(); return(Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount)); }
public static string Encrypt(string plainText, string passPhrase) { byte[] initVectorBytes = Encoding.ASCII.GetBytes("tu89geji340t89u2"); const int keysize = 256; byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); using (System.Security.Cryptography.PasswordDeriveBytes password = new System.Security.Cryptography.PasswordDeriveBytes(passPhrase, null)) { byte[] keyBytes = password.GetBytes(keysize / 8); using (System.Security.Cryptography.RijndaelManaged symmetricKey = new System.Security.Cryptography.RijndaelManaged()) { symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; using (System.Security.Cryptography.ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)) { using (MemoryStream memoryStream = new MemoryStream()) { using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, encryptor, System.Security.Cryptography.CryptoStreamMode.Write)) { cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); byte[] cipherTextBytes = memoryStream.ToArray(); return(Convert.ToBase64String(cipherTextBytes)); } } } } } }
public static string Encrypt(string[] keyArray) { string key = ""; foreach (string s in keyArray) { key = key + s + ";"; } byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector); byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt); byte[] PlainTextBytes = Encoding.UTF8.GetBytes(key); System.Security.Cryptography.PasswordDeriveBytes DerivedPassword = new System.Security.Cryptography.PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations); byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8); System.Security.Cryptography.RijndaelManaged SymmetricKey = new System.Security.Cryptography.RijndaelManaged(); SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; byte[] CipherTextBytes = null; using (System.Security.Cryptography.ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)) { using (System.IO.MemoryStream MemStream = new System.IO.MemoryStream()) { using (System.Security.Cryptography.CryptoStream CryptoStream = new System.Security.Cryptography.CryptoStream(MemStream, Encryptor, System.Security.Cryptography.CryptoStreamMode.Write)) { CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length); CryptoStream.FlushFinalBlock(); CipherTextBytes = MemStream.ToArray(); MemStream.Close(); CryptoStream.Close(); } } } SymmetricKey.Clear(); return(Convert.ToBase64String(CipherTextBytes)); }
private static string Decrypt(string cipherText, string passPhrase) { byte[] initVectorBytes = System.Text.Encoding.ASCII.GetBytes("tu89geji340t89u2"); const int keysize = 256; byte[] cipherTextBytes = Convert.FromBase64String(cipherText); using (System.Security.Cryptography.PasswordDeriveBytes password = new System.Security.Cryptography.PasswordDeriveBytes(passPhrase, null)) { byte[] keyBytes = password.GetBytes(keysize / 8); using (System.Security.Cryptography.RijndaelManaged symmetricKey = new System.Security.Cryptography.RijndaelManaged()) { symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; using (System.Security.Cryptography.ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)) { using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes)) { using (System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read)) { byte[] plainTextBytes = new byte[cipherTextBytes.Length]; int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); return(System.Text.Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount)); } } } } } }
private static byte[] GenerateKey(string strPassword, int lenght) { byte[] bytSalt = System.Text.Encoding.ASCII.GetBytes("salt"); System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(strPassword, bytSalt); // OBSOLETO CON FRAMEWORK 2.0 //Dim pdb As New System.Security.Cryptography.Rfc2898DeriveBytes(strPassword, bytSalt) ' DA USARE CON 2.0 RICHIEDE UN SALT PIU' LUNGO E INCOMPATIBILE return(pdb.GetBytes(lenght)); }
/// <summary> /// Generuje hash klucza. /// </summary> /// <param name="password">Hasło.</param> /// <param name="keySize">Rozmiar klucza. Maksymalnie 256 bitów, minimalnie 128 bitów, przeskok o 64 bity.</param> /// <returns>Hash klucza.</returns> public static byte[] GetKey(string password, int keySize) { if (password == null) { throw new System.ArgumentNullException("Argument 'password' is null."); } byte[] pwd = System.Text.Encoding.UTF8.GetBytes(password); byte[] salt = System.Text.Encoding.UTF8.GetBytes(password); // Na Windows Phone 8 dostępny jest tylko algorytm SHA1. //System.Security.Cryptography.PasswordDeriveBytes pdb = // new System.Security.Cryptography.PasswordDeriveBytes(pwd, salt, "SHA1", 100 + pwd.Length); System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(pwd, salt, "SHA512", 100 + pwd.Length); return(pdb.GetBytes(keySize)); }
/// <summary> /// ����һ���ı� /// </summary> /// <param name="szOriginalText">ԭʼ�ı�</param> /// <param name="szKey">������Կ</param> /// <returns>���ܺ������</returns> public static string EncryptText(string szOriginalText, string szKey) { try { byte[] originalData = System.Text.Encoding.Unicode.GetBytes(szOriginalText); System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(szKey , new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); byte[] encryptedData = GlobalMethods.Security.Encrypt(originalData, pdb.GetBytes(32), pdb.GetBytes(16)); return System.Convert.ToBase64String(encryptedData); } catch (Exception ex) { LogManager.Instance.WriteLog("GlobalMethods.EncryptText", ex); return null; } }
/// <summary> /// 解密一段密文 /// </summary> /// <param name="szOriginalText">密文文本</param> /// <param name="szKey">解密密钥</param> /// <returns>原始文本</returns> public static string DecryptText(string szEncryptedText, string szKey) { try { byte[] encryptedData = System.Convert.FromBase64String(szEncryptedText); System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(szKey , new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); byte[] originalData = GlobalMethods.Security.Decrypt(encryptedData, pdb.GetBytes(32), pdb.GetBytes(16)); return(System.Text.Encoding.Unicode.GetString(originalData)); } catch (Exception ex) { LogManager.Instance.WriteLog("GlobalMethods.DecryptText", ex); return(null); } }
/// <summary> /// Will decrypt the <paramref name="source"/> byte array using the given <paramref name="encryptorConfiguration"/>. /// </summary> /// <param name="source">The byte array to decrypt.</param> /// <param name="encryptorConfiguration">The encryption configuration used to decrypt the <paramref name="source"/> byte array.</param> /// <returns>The <paramref name="source"/> byte array decrypted using the given <paramref name="encryptorConfiguration"/>.</returns> public static byte[] Decrypt(byte[] source, IEncryptorConfiguration encryptorConfiguration) { // TODO: Fix This. Problem Id= 3333E15BFE8746CEAA1D5664BFFD0E18 // What's happening here is that I'm using the IEncryptorConfiguration.PasswordSaltHash as the PASSWORD for the PasswordDeriveBytes type; that's not right! // According to Microsoft documentation, it is using SHA1 by default. // Must Rethink This! if ((source == null) || (source.Length == 0)) { return(source); } if (encryptorConfiguration == null) { return(source); } System.Security.Cryptography.PasswordDeriveBytes passwordDerivedBytes = null; using (var ec = encryptorConfiguration.SymmetricAlgorithm) { passwordDerivedBytes = new System.Security.Cryptography.PasswordDeriveBytes(encryptorConfiguration.SaltedPassword.PasswordSaltHash, encryptorConfiguration.SaltedPassword.Salt); ec.Key = passwordDerivedBytes.GetBytes(ec.KeySize / 8); ec.IV = passwordDerivedBytes.GetBytes(ec.BlockSize / 8); using (var ms = new System.IO.MemoryStream()) { using (var cs = new System.Security.Cryptography.CryptoStream(ms, ec.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)) { cs.Write(source, 0, source.Length); cs.Close(); return(ms.ToArray()); } } } }
public static string Decrypt(string CipherText, string Password, string Salt, string HashAlgorithm, int PasswordIterations = 2, string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (string.IsNullOrEmpty(CipherText)) return ""; byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector); byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt); byte[] CipherTextBytes = Convert.FromBase64String(CipherText); System.Security.Cryptography.PasswordDeriveBytes DerivedPassword = new System.Security.Cryptography.PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations); byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8); System.Security.Cryptography.RijndaelManaged SymmetricKey = new System.Security.Cryptography.RijndaelManaged(); SymmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; byte[] PlainTextBytes = new byte[CipherTextBytes.Length]; int ByteCount = 0; using (System.Security.Cryptography.ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes)) { using (MemoryStream MemStream = new MemoryStream(CipherTextBytes)) { using (System.Security.Cryptography.CryptoStream CryptoStream = new System.Security.Cryptography.CryptoStream(MemStream, Decryptor, System.Security.Cryptography.CryptoStreamMode.Read)) { ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length); MemStream.Close(); CryptoStream.Close(); } } } SymmetricKey.Clear(); return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount); }
/// </RETURNS> public string Encrypt(string plainText) { // Convert strings to byte arrays. // Let us assume that strings only contain ASCII codes. // If strings include Unicode characters, use Unicode, UTF7, or UTF8 // encoding. byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); // Convert our plaintext to a byte array. // Let us assume that plaintext contains UTF8-encoded characters. byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); // First, we must create a password, from which the key will be derived. // This password will be generated from the specified passphrase and // salt value. The password will be created using the specified hash // algorithm. Password creation can be done in several iterations. System.Security.Cryptography.PasswordDeriveBytes password = new System.Security.Cryptography.PasswordDeriveBytes( passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); // Use the password to generate pseudo-random bytes for the encryption // key. Specify the size of the key in bytes (instead of bits). byte[] keyBytes = password.GetBytes(keySize / 8); // Create uninitialized Rijndael encryption object. System.Security.Cryptography.RijndaelManaged symmetricKey = new System.Security.Cryptography.RijndaelManaged(); // It is reasonable to set encryption mode to Cipher Block Chaining // (CBC). Use default options for other symmetric key parameters. symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; // Generate encryptor from the existing key bytes and initialization // vector. Key size will be defined based on the number of the key // bytes. System.Security.Cryptography.ICryptoTransform encryptor = symmetricKey.CreateEncryptor( keyBytes, initVectorBytes); // Define memory stream which will be used to hold encrypted data. System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); // Define cryptographic stream (always use Write mode for encryption). System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, encryptor, System.Security.Cryptography.CryptoStreamMode.Write); // Start encrypting. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); // Finish encrypting. cryptoStream.FlushFinalBlock(); // Convert our encrypted data from a memory stream to a byte array. byte[] cipherTextBytes = memoryStream.ToArray(); // Close both streams. memoryStream.Close(); cryptoStream.Close(); // Convert encrypted data to a base64-encoded string and return result. string cipherText = Convert.ToBase64String(cipherTextBytes); // Return encrypted string. return(cipherText); }
/// <SUMMARY> /// Decrypts specified ciphertext using Rijndael symmetric key algorithm. /// </SUMMARY> /// <PARAM name="cipherText"> /// Base64-formatted ciphertext value. /// </PARAM> /// <PARAM name="passPhrase"> /// Passphrase from which a pseudo-random password will be derived. The /// derived password will be used to generate the encryption key. /// Passphrase can be any string. In this example we assume that this /// passphrase is an ASCII string. /// </PARAM> /// <PARAM name="saltValue"> /// Salt value used along with passphrase to generate password. Salt can /// be any string. In this example we assume that salt is an ASCII string. /// </PARAM> /// <PARAM name="hashAlgorithm"> /// Hash algorithm used to generate password. Allowed values are: "MD5" and /// "SHA1". SHA1 hashes are a bit slower, but more secure than MD5 hashes. /// </PARAM> /// <PARAM name="passwordIterations"> /// Number of iterations used to generate password. One or two iterations /// should be enough. /// </PARAM> /// <PARAM name="initVector"> /// Initialization vector (or IV). This value is required to encrypt the /// first block of plaintext data. For RijndaelManaged class IV must be /// exactly 16 ASCII characters long. /// </PARAM> /// <PARAM name="keySize"> /// Size of encryption key in bits. Allowed values are: 128, 192, and 256. /// Longer keys are more secure than shorter keys. /// </PARAM> /// <RETURNS> /// Decrypted string value. /// </RETURNS> /// <REMARKS> /// Most of the logic in this function is similar to the Encrypt /// logic. In order for decryption to work, all parameters of this function /// - except cipherText value - must match the corresponding parameters of /// the Encrypt function which was called to generate the /// ciphertext. /// </REMARKS> public string Decrypt(string cipherText) { // Convert strings defining encryption key characteristics to byte // arrays. Let us assume that strings only contain ASCII codes. // If strings include Unicode characters, use Unicode, UTF7, or UTF8 // encoding. byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); // Convert our ciphertext to a byte array. byte[] cipherTextBytes = Convert.FromBase64String(cipherText); // First, we must create a password, from which the key will be // derived. This password will be generated from the specified // passphrase and salt value. The password will be created using // the specified hash algorithm. Password creation can be done in // several iterations. System.Security.Cryptography.PasswordDeriveBytes password = new System.Security.Cryptography.PasswordDeriveBytes( passPhrase, saltValueBytes, hashAlgorithm, passwordIterations); // Use the password to generate pseudo-random bytes for the encryption // key. Specify the size of the key in bytes (instead of bits). byte[] keyBytes = password.GetBytes(keySize / 8); // Create uninitialized Rijndael encryption object. System.Security.Cryptography.RijndaelManaged symmetricKey = new System.Security.Cryptography.RijndaelManaged(); // It is reasonable to set encryption mode to Cipher Block Chaining // (CBC). Use default options for other symmetric key parameters. symmetricKey.Mode = System.Security.Cryptography.CipherMode.CBC; // Generate decryptor from the existing key bytes and initialization // vector. Key size will be defined based on the number of the key // bytes. System.Security.Cryptography.ICryptoTransform decryptor = symmetricKey.CreateDecryptor( keyBytes, initVectorBytes); // Define memory stream which will be used to hold encrypted data. System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(cipherTextBytes); // Define cryptographic stream (always use Read mode for encryption). System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read); // Since at this point we don't know what the size of decrypted data // will be, allocate the buffer long enough to hold ciphertext; // plaintext is never longer than ciphertext. byte[] plainTextBytes = new byte[cipherTextBytes.Length]; // Start decrypting. int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); // Close both streams. memoryStream.Close(); cryptoStream.Close(); // Convert decrypted data to a string. // Let us assume that the original plaintext string was UTF8-encoded. string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); // Return decrypted string. return(plainText); }