Example #1
0
        /// <summary>
        /// Decrypts the specified ciphertext with the specified key and IV and returns the plaintext string.
        /// </summary>
        /// <param name="ciphertext">The ciphertext string.</param>
        /// <param name="key">The 256 bit key.</param>
        /// <param name="iv">The 128 bit initialization vector.</param>
        /// <returns>The plaintext string, or <c>null</c> if <paramref name="ciphertext"/> is <c>null</c></returns>
        public static string Decrypt(string ciphertext, byte[] key, byte[] iv)
        {
            if (ciphertext == null)
            {
                return null;
            }

            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (iv == null)
            {
                throw new ArgumentNullException("iv");
            }

            byte[] cipherBytes = Convert.FromBase64String(ciphertext);
            using (var utility = new SymmetricUtility(key, iv))
            {
                byte[] decryptedBytes = utility.Decrypt(cipherBytes);
                return Encoding.UTF8.GetString(decryptedBytes);
            }
        }
Example #2
0
        /// <summary>
        /// Encrypts the specified plaintext with the specifed key and IV and UTF8 encoding and returns base 64 string.
        /// </summary>
        /// <param name="plaintext">The plaintext bytes.</param>
        /// <param name="key">The 256 bit key.</param>
        /// <param name="iv">The 128 bit initialization vector.</param>
        /// <returns>The encrypted string, or <c>null</c> if <paramref name="plaintext"/> is <c>null</c>.</returns>
        public static string Encrypt(string plaintext, byte[] key, byte[] iv)
        {
            if (plaintext == null)
            {
                return null;
            }

            byte[] plainBytes = Encoding.UTF8.GetBytes(plaintext);
            using (var utility = new SymmetricUtility(key, iv))
            {
                byte[] cipherBytes = utility.Encrypt(plainBytes);
                return Convert.ToBase64String(cipherBytes);
            }
        }
 public void TestConstructor_NullIV()
 {
     var util = new SymmetricUtility(SymmetricUtility.GenerateKey(), null);
 }
 public void TestConstructor_NullKey()
 {
     var util = new SymmetricUtility(null, SymmetricUtility.GenerateIV());
 }
 public void TestConstructor_Null()
 {
     var util = new SymmetricUtility(null, null);
 }