internal static EncryptionResult Encrypt(SecureString input, SecureString key) { EncryptionResult result = null; byte[] data = GetData(key); result = Encrypt(input, data); Array.Clear(data, 0, data.Length); return(result); }
internal static EncryptionResult Encrypt(SecureString input, byte[] key, byte[] iv) { Utils.CheckSecureStringArg(input, "input"); Utils.CheckKeyArg(key, "key"); byte[] encryptedData = null; MemoryStream ms = null; ICryptoTransform encryptor = null; CryptoStream cs = null; // // prepare the crypto stuff. Initialization Vector is // randomized by default. // Aes aes = Aes.Create(); if (iv == null) { iv = aes.IV; } encryptor = aes.CreateEncryptor(key, iv); ms = new MemoryStream(); using (cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { // // get clear text data from the input SecureString // byte[] data = GetData(input); // // encrypt it // cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); // // clear the clear text data array // Array.Clear(data, 0, data.Length); // // convert the encrypted blob to a string // encryptedData = ms.ToArray(); EncryptionResult output = new EncryptionResult(ByteArrayToString(encryptedData), Convert.ToBase64String(iv)); return(output); } }
/// <summary> /// Return contents of the SecureString after encrypting /// using the specified key and encoding the encrypted blob as a string. /// </summary> /// <param name="input">Input string to encrypt.</param> /// <param name="key">Encryption key.</param> /// <returns>A string (see summary).</returns> internal static EncryptionResult Encrypt(SecureString input, SecureString key) { EncryptionResult output = null; // // get clear text key from the SecureString key // byte[] keyBlob = GetData(key); // // encrypt the data // output = Encrypt(input, keyBlob); // // clear the clear text key // Array.Clear(keyBlob, 0, keyBlob.Length); return(output); }
internal static EncryptionResult Encrypt(SecureString input, byte[] key, byte[] iv) { Utils.CheckSecureStringArg(input, "input"); Utils.CheckKeyArg(key, "key"); byte[] encryptedData = null; MemoryStream ms = null; ICryptoTransform encryptor = null; CryptoStream cs = null; // // prepare the crypto stuff. Initialization Vector is // randomized by default. // Aes aes = Aes.Create(); if (iv == null) iv = aes.IV; encryptor = aes.CreateEncryptor(key, iv); ms = new MemoryStream(); using (cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { // // get clear text data from the input SecureString // byte[] data = GetData(input); // // encrypt it // cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); // // clear the clear text data array // Array.Clear(data, 0, data.Length); // // convert the encrypted blob to a string // encryptedData = ms.ToArray(); EncryptionResult output = new EncryptionResult(ByteArrayToString(encryptedData), Convert.ToBase64String(iv)); return output; } }