/// <summary> /// <para>Encrypts a secret using the configured <c>SymmetricAlgorithm</c>.</para> /// </summary> /// <param name="plaintext"><para>The input to be encrypted. It is the responsibility of the caller to clear this /// byte array when finished.</para></param> /// <returns><para>The resulting cipher text.</para></returns> /// <seealso cref="ISymmetricCryptoProvider.Encrypt"/> public byte[] Encrypt(byte[] plaintext) { if (plaintext == null) { throw new ArgumentNullException("plainText"); } if (plaintext.Length == 0) { throw new ArgumentException(Resources.ExceptionByteArrayValueMustBeGreaterThanZeroBytes, "plaintext"); } byte[] output = null; try { using (SymmetricCryptographer crypto = new SymmetricCryptographer(algorithmType, key)) { output = crypto.Encrypt(plaintext); } } catch (Exception e) { InstrumentationProvider.FireCyptographicOperationFailed(Resources.EncryptionFailed, e); throw; } InstrumentationProvider.FireSymmetricEncryptionPerformed(); return(output); }
/// <summary> /// <para>Encrypts a secret using the configured <c>SymmetricAlgorithm</c>.</para> /// </summary> /// <param name="plaintext"><para>The input for which you want to encrypt.</para></param> /// <returns><para>The resulting cipher text.</para></returns> /// <seealso cref="ISymmetricCryptoProvider.Encrypt"/> public byte[] Encrypt(byte[] plaintext) { ArgumentValidation.CheckForNullReference(plaintext, "plaintext"); ArgumentValidation.CheckForZeroBytes(plaintext, "plaintext"); byte[] output = null; SymmetricAlgorithmProviderData data = GetSymmetricAlgorithmProviderDataFromCursor(); SymmetricCryptographer crypto = new SymmetricCryptographer(data.AlgorithmType, data.Key); output = crypto.Encrypt(plaintext); SecurityCryptoSymmetricEncryptionEvent.Fire(string.Empty); return(output); }
public void EncryptAndDecryptWithType() { byte[] key = new byte[16]; CryptographyUtility.GetRandomBytes(key); ProtectedKey protectedKey = ProtectedKey.CreateFromPlaintextKey(key, DataProtectionScope.LocalMachine); SymmetricCryptographer symm = new SymmetricCryptographer(typeof(RijndaelManaged), protectedKey); byte[] plainText = new byte[12]; CryptographyUtility.GetRandomBytes(plainText); byte[] cipherText = symm.Encrypt(plainText); Assert.IsFalse(CryptographyUtility.CompareBytes(cipherText, plainText)); byte[] decryptedText = symm.Decrypt(cipherText); Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText)); }
/// <summary> /// <para>Encrypts a secret using the configured <c>SymmetricAlgorithm</c>.</para> /// </summary> /// <param name="plaintext"><para>The input to be encrypted. It is the responsibility of the caller to clear this /// byte array when finished.</para></param> /// <returns><para>The resulting cipher text.</para></returns> /// <seealso cref="ISymmetricCryptoProvider.Encrypt"/> public byte[] Encrypt(byte[] plaintext) { if (plaintext == null) throw new ArgumentNullException("plainText"); if (plaintext.Length == 0) throw new ArgumentException(Resources.ExceptionByteArrayValueMustBeGreaterThanZeroBytes, "plaintext"); byte[] output = null; try { using (SymmetricCryptographer crypto = new SymmetricCryptographer(algorithmType, key)) { output = crypto.Encrypt(plaintext); } } catch (Exception e) { InstrumentationProvider.FireCyptographicOperationFailed(Resources.EncryptionFailed, e); throw; } InstrumentationProvider.FireSymmetricEncryptionPerformed(); return output; }