/// <summary> /// Encrypt String using specified Algorithm /// </summary> /// <param name="targetValue"></param> /// <param name="cryptoAlgorithm"></param> /// <returns></returns> public byte[] Encrypt(string targetValue, eSymmetricCryptoAlgorithm cryptoAlgorithm) { byte[] arrEncryptedValue = null; if (targetValue.Length > 0) { arrEncryptedValue = SymmetricCrypto(cryptoAlgorithm, eCryptoMode.Encrypt, System.Text.Encoding.UTF8.GetBytes(targetValue), marrEncryptionKey, marrEncryptionVector); } return arrEncryptedValue; }
/// <summary> /// Encrypt File using specified Algorithm /// </summary> /// <param name="targetValue"></param> /// <param name="cryptoAlgorithm"></param> /// <returns></returns> public byte[] Encrypt(FileStream decryptedFile, eSymmetricCryptoAlgorithm cryptoAlgorithm) { byte[] arrEncryptedValue = null; byte[] buffer = null; buffer = new byte[decryptedFile.Length]; decryptedFile.Read(buffer, 0, buffer.Length); decryptedFile.Close(); arrEncryptedValue = SymmetricCrypto(cryptoAlgorithm, eCryptoMode.Encrypt, buffer, marrEncryptionKey, marrEncryptionVector); return arrEncryptedValue; }
/// <summary> /// Get Encryption Provider appropriate to eSymmetricCryptoAlgorithm value specified /// </summary> /// <param name="Algorithm"></param> /// <returns></returns> private System.Security.Cryptography.SymmetricAlgorithm GetSymmetricEncryptionProvider(eSymmetricCryptoAlgorithm Algorithm) { // Return a provider to match our chosen encryption algorithm // ========================================================== SymmetricAlgorithm objReturn = null; // Generate crypto provider switch(Algorithm) { case eSymmetricCryptoAlgorithm.DES: { objReturn = new DESCryptoServiceProvider(); break; } case eSymmetricCryptoAlgorithm.RC2: { objReturn = new RC2CryptoServiceProvider(); break; } case eSymmetricCryptoAlgorithm.Rinjdael: { objReturn = new RijndaelManaged(); break; } case eSymmetricCryptoAlgorithm.TripleDES: { objReturn = new TripleDESCryptoServiceProvider(); break; } } // Apply standard parameters to this provider // (None to be applied at this time) // Return provider return objReturn; }
/// <summary> /// Performs Encryption\Decryption using specified Algorithm, Key, and Vector /// </summary> /// <param name="cryptoAlgorithm"></param> /// <param name="cryptoMode"></param> /// <param name="inputData"></param> /// <param name="encryptionKey"></param> /// <param name="encryptionVector"></param> /// <returns></returns> private byte[] SymmetricCrypto(eSymmetricCryptoAlgorithm cryptoAlgorithm, eCryptoMode cryptoMode, byte[] inputData, byte[] encryptionKey, byte[] encryptionVector) { SymmetricAlgorithm cryptProvider; ICryptoTransform cryptEngine = null; MemoryStream objCryptoStream; CryptoStream objCryptoProcess; byte[] arrOutputData; // Create encryption provider - required for validation steps below // ---------------------------------------------------------------- cryptProvider = GetSymmetricEncryptionProvider(cryptoAlgorithm); // Validate contents of byte arrays // -------------------------------- if (ValidateSymmetricalEncryptionParameters(cryptProvider, ref inputData, ref encryptionKey, ref encryptionVector, cryptoMode) == false) { return null; } // Attach Key and Vector to cryptProvider cryptProvider.Key = encryptionKey; cryptProvider.IV = encryptionVector; // Encrypt\Decrypt objCryptoStream = new MemoryStream(); switch (cryptoMode) { case eCryptoMode.Encrypt: { cryptEngine = cryptProvider.CreateEncryptor(); break; } case eCryptoMode.Decrypt: { cryptEngine = cryptProvider.CreateDecryptor(); break; } } // Encrypted\Decrypt value into a Stream objCryptoProcess = new CryptoStream(objCryptoStream, cryptEngine, CryptoStreamMode.Write); objCryptoProcess.Write(inputData, 0, inputData.Length); objCryptoProcess.Close(); // Extract Encrypted\Decrypted value from Stream arrOutputData = objCryptoStream.ToArray(); cryptEngine = null; cryptProvider = null; objCryptoStream = null; objCryptoProcess = null; return arrOutputData; }
/// <summary> /// Return a Vector appropriate to the specified CryptoAlgorithm /// </summary> /// <param name="Algorithm"></param> /// <returns></returns> public string GetSymmetricVector(eSymmetricCryptoAlgorithm Algorithm) { SymmetricAlgorithm cryptEngine; string strReturn; // Use Crypto Engine and generate Vector cryptEngine = GetSymmetricEncryptionProvider(Algorithm); strReturn = GetSymmetricVector(cryptEngine); cryptEngine = null; return strReturn; }
/// <summary> /// Decrypt to String using specified Algorithm /// </summary> /// <param name="targetValue"></param> /// <param name="cryptoAlgorithm"></param> /// <returns></returns> public string Decrypt(byte[] targetValue, eSymmetricCryptoAlgorithm cryptoAlgorithm) { byte[] arrDecryptedValue = null; string strDecryptedValue = string.Empty; if (targetValue.Length >= 8) { arrDecryptedValue = SymmetricCrypto(cryptoAlgorithm, eCryptoMode.Decrypt, targetValue, marrEncryptionKey, marrEncryptionVector); strDecryptedValue = System.Text.Encoding.UTF8.GetString(arrDecryptedValue); } return strDecryptedValue; }