/// <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> /// Decrypts a secret using the configured <c>SymmetricAlgorithm</c>. /// <seealso cref="ISymmetricCryptoProvider.Decrypt"/> /// </summary> /// <param name="ciphertext"><para>The cipher text for which you want to decrypt.</para></param> /// <returns><para>The resulting plain text.</para></returns> /// <seealso cref="ISymmetricCryptoProvider.Decrypt"/> public byte[] Decrypt(byte[] ciphertext) { ArgumentValidation.CheckForNullReference(ciphertext, "encryptedText"); ArgumentValidation.CheckForZeroBytes(ciphertext, "encryptedText"); byte[] output = null; SymmetricAlgorithmProviderData data = GetSymmetricAlgorithmProviderDataFromCursor(); SymmetricCryptographer crypto = new SymmetricCryptographer(data.AlgorithmType, data.Key); output = crypto.Decrypt(ciphertext); SecurityCryptoSymmetricDecryptionEvent.Fire(string.Empty); return(output); }
public void EncryptAndDecryptWithTypeUsingProtectedKey() { 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; }
public void ConstructingWithNullTypeThrows() { SymmetricCryptographer symm = new SymmetricCryptographer((Type)null, (ProtectedKey)null); }
public void ConstructingWithBadTypeThrows() { SymmetricCryptographer symm = new SymmetricCryptographer(typeof(object), (ProtectedKey)null); }