private static string Encrypt_Private_v1(string plainText, SecureString passPhrase, int iterations, bool useBase36) { // in version 1, we use UTF8 for our plain text value to bytes byte[] unencryptedBytes = Encoding.UTF8.GetBytes(plainText); var passPhraseAsBytes = passPhrase.ToByteArray(); var payload = Encrypt_Private_v1(unencryptedBytes, passPhraseAsBytes, iterations); passPhraseAsBytes.ClearByteArray(); // modify the byte array string cipherText; if (useBase36) { cipherText = Base36.ByteArrayToBase36String(payload); cipherText = cipherText.ToLower(); } else { cipherText = Convert.ToBase64String(payload); } // Return encrypted string witht the leading 8 characters as the salt return(cipherText); }
private static string Encrypt_Private(string plainText, string passPhrase, bool useBase36) { RandomGenerator randGen = new RandomGenerator(); string strSalt = randGen.RandomAlphaNumeric(8); byte[] saltValueBytes; saltValueBytes = Encoding.ASCII.GetBytes(strSalt); // Convert our plaintext into 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. Rfc2898DeriveBytes pw = new Rfc2898DeriveBytes(passPhrase, saltValueBytes, 2); // 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 = pw.GetBytes(256 / 8); // Create uninitialized Rijndael encryption object. RijndaelManaged symmetricKey = new RijndaelManaged(); RandomGenerator randGen2 = new RandomGenerator(); string strIv = randGen2.RandomAlphaNumeric(16); // Convert strings into byte arrays. // Let us assume that strings only contain ASCII codes. // If strings include Unicode characters, use Unicode, UTF7, or UTF8 // encoding. byte[] initVectorBytes; initVectorBytes = Encoding.ASCII.GetBytes(strIv); // It is reasonable to set encryption mode to Cipher Block Chaining // (CBC). Use default options for other symmetric key parameters. symmetricKey.Mode = 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. ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); // Define memory stream which will be used to hold encrypted data. MemoryStream memoryStream = new MemoryStream(); // Define cryptographic stream (always use Write mode for encryption). CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); // Start encrypting. cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); // Finish encrypting. cryptoStream.FlushFinalBlock(); // Convert our encrypted data from a memory stream into a byte array. byte[] cipherTextBytes = memoryStream.ToArray(); // Close both streams. memoryStream.Close(); cryptoStream.Close(); // Convert encrypted data into a base64-encoded string. string cipherText; if (useBase36) { cipherText = Base36.ByteArrayToBase36String(cipherTextBytes); cipherText = cipherText.ToLower(); } else { cipherText = Convert.ToBase64String(cipherTextBytes); } // Return encrypted string. string strReturn = strIv + strSalt + cipherText; return(strReturn); //Return cipherText }