Example #1
0
        /// <summary>
        /// Sets or resets the encryption level for the encryption instance
        /// </summary>
        /// <param name="keysize">The encryption level as an CryptoLevel enum</param>
        public void SetEncryptionLevel(CryptoLevel keysize = CryptoLevel.AES256)
        {
            // update the keySize property with the requested encryption level
            KeySize = keysize;

            // at this time we are utilizing hard-coded values
            ConfigureCryptoInCode();

            // convert the CryptoLevel to an integer to assist calculationg the crypto properties
            m_encryptionBits = KeySize == CryptoLevel.AES256 ? 256 : KeySize == CryptoLevel.AES128 ? 128 : 0;

            // initialize the IV array
            iv_array = new byte[16];

            // initialize the Key array
            key_array = new byte[m_encryptionBits / 8];

            // generate the Crypto Key and IV
            // create the key and Initial Vector from the
            OpenSslCompatDeriveBytes crap = new OpenSslCompatDeriveBytes(m_cryptoSalt, m_nonce, m_cryptoHash, m_cryptoIterations);

            stuff_array = crap.GetBytes((m_encryptionBits / 8) + 16);
            Buffer.BlockCopy(stuff_array, 0, key_array, 0, m_encryptionBits / 8);
            Buffer.BlockCopy(stuff_array, m_encryptionBits / 8, iv_array, 0, 16);
        }
Example #2
0
        /// <summary>
        /// Encrypts the un-cyphered data as an array of bytes
        /// </summary>
        /// <param name="clearPayload">The un-ciphered data to be encrypted</param>
        /// <param name="keySize">The encryption level to be used to construct the encryption hash.</param>
        /// <returns>An ciphered array of bytes</returns>
        public byte[] EncryptAES(byte[] clearPayload, CryptoLevel keysize = CryptoLevel.AES256)
        {
            // if the key strength requested is different from the instance
            if (KeySize != KeySize)
            {
                KeySize = keysize;
                // re-calculate the encryption hash using the encryption level requested
                SetEncryptionLevel(KeySize);
            }

            // Check arguments.
            if (clearPayload == null || clearPayload.Length <= 0)
            {
                throw new ArgumentNullException("clearPayload");
            }
            if (key_array == null || key_array.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }
            if (iv_array == null || iv_array.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }

            // declare the output object.
            byte[] encrypted;
            // Create an Aes object
            // with the specified key and IV.
            using (Aes aes = Aes.Create())
            {
                aes.Key     = key_array;
                aes.IV      = iv_array;
                aes.Mode    = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

                // Create the streams used for encryption.

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(clearPayload, 0, clearPayload.Length);
                        csEncrypt.FlushFinalBlock();
                        encrypted = msEncrypt.ToArray();
                    }
                    msEncrypt.Close();
                }
            }
            // Return the encrypted bytes from the memory stream.
            return(encrypted);
        }
Example #3
0
        /// <summary>
        /// Sets or resets the encryption level for the encryption instance
        /// </summary>
        /// <param name="keysize">The encryption level as an CryptoLevel enum</param>
        public static void SetEncryptionLevel(CryptoLevel keysize = CryptoLevel.AES256)
        {
            // update the keySize property with the requested encryption level
            KeySize = keysize;

            // at this time we are utilizing hard-coded values
            ConfigureCryptoInCode();

            // convert the CryptoLevel to an integer to assist calculationg the crypto properties
            m_encryptionBits = KeySize == CryptoLevel.AES256 ? 256 : KeySize == CryptoLevel.AES128 ? 128 : 0;

            // initialize the IV array
            iv_array = new byte[16];

            // initialize the Key array
            key_array = new byte[m_encryptionBits / 8];

            // generate the Crypto Key and IV
            // create the key and Initial Vector from the
            OpenSslCompatDeriveBytes crap = new OpenSslCompatDeriveBytes(m_cryptoSalt, m_nonce, m_cryptoHash, m_cryptoIterations);

            stuff_array = crap.GetBytes((m_encryptionBits / 8) + 16);
            Buffer.BlockCopy(stuff_array, 0, key_array, 0, m_encryptionBits / 8);
            Buffer.BlockCopy(stuff_array, m_encryptionBits / 8, iv_array, 0, 16);
        }
Example #4
0
        /// <summary>
        /// Encrypts the un-cyphered data as an array of bytes
        /// </summary>
        /// <param name="clearPayload">The un-ciphered data to be encrypted</param>
        /// <param name="keySize">The encryption level to be used to construct the encryption hash.</param>
        /// <returns>An ciphered array of bytes</returns>
        public static byte[] EncryptAES(byte[] clearPayload, CryptoLevel keysize = CryptoLevel.AES256)
        {
            // if the key strength requested is different from the instance
            if (KeySize != KeySize)
            {
                KeySize = keysize;
                // re-calculate the encryption hash using the encryption level requested
                SetEncryptionLevel(KeySize);
            }

            // Check arguments.
            if (clearPayload == null || clearPayload.Length <= 0)
            {
                throw new ArgumentNullException("clearPayload");
            }
            if (key_array == null || key_array.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }
            if (iv_array == null || iv_array.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }

            // declare the output object.
            byte[] encrypted;
            // Create an Aes object
            // with the specified key and IV.
            using (Aes aes = Aes.Create())
            {
                aes.Key = key_array;
                aes.IV = iv_array;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

                // Create the streams used for encryption.

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(clearPayload, 0, clearPayload.Length);
                        csEncrypt.FlushFinalBlock();
                        encrypted = msEncrypt.ToArray();
                    }
                    msEncrypt.Close();
                }
            }
            // Return the encrypted bytes from the memory stream.
            return encrypted;
        }
Example #5
0
 void setAesStrength()
 {
     switch (cboStrength.SelectedIndex)
     {
         case 0:
             m_keySize = CryptoLevel.None;
             break;
         case 1:
             m_keySize = CryptoLevel.AES128;
             break;
         default:
             m_keySize = CryptoLevel.AES256;
             break;
     }
     CryptoManager.Crypto.InitializeEncryption(aesize: (CryptoLevel)cboStrength.SelectedIndex, hash: cboCryptoHash.Text);
 }
Example #6
0
        /// <summary>
        /// Sets or resets the encryption level for the encryption instance
        /// </summary>
        /// <param name="keysize">The encryption level as an CryptoLevel enum</param>
        public void InitializeEncryption(CryptoLevel aesize = CryptoLevel.AES256, String hash = "SHA1", Int16 iterations = 5, Int16 nonceLength = 8, String salt = "", String nonce = "")
        {
            KeySize = aesize;
            m_cryptoHash = hash;
            m_cryptoIterations = iterations;
            NonceLength = nonceLength;

            // if a salt and a nonce have been provided use them:
            if (!String.IsNullOrWhiteSpace(salt) && !String.IsNullOrWhiteSpace(nonce))
            {
                Nonce = Encoding.ASCII.GetBytes(nonce);
                CryptoSalt = salt;
            }
            //otherwise; use the calculated nonce and salt values

            // initialize the IV array
            iv_array = new byte[16];

            // initialize the Key array
            key_array = new byte[m_encryptionBits / 8];

            // generate the Crypto Key and IV
            // create the key and Initial Vector from the
            OpenSslCompatDeriveBytes crap = new OpenSslCompatDeriveBytes(CryptoSalt, Nonce, CryptoHash, CryptoIterations);

            stuff_array = crap.GetBytes((m_encryptionBits / 8) + 16);
            Buffer.BlockCopy(stuff_array, 0, key_array, 0, m_encryptionBits / 8);
            Buffer.BlockCopy(stuff_array, m_encryptionBits / 8, iv_array, 0, 16);
        }
Example #7
0
        /// <summary>
        /// Decrypts a cyphered key and returns it as clear text
        /// </summary>
        /// <param name="plainText">The ciphered string values in the form of a byte array</param>
        /// <param name="keySize">the encryption strength - haven't seen this make a difference yet.</param>
        /// <returns>Clear text as a string</returns>
        public byte[] DecryptAES(byte[] cipheredPayload, CryptoLevel keysize = CryptoLevel.AES256)
        {
            // if the key strenght requested is different from the instance
            if (KeySize != KeySize)
            {
                KeySize = keysize;
            }

            // Check arguments.
            if (cipheredPayload == null || cipheredPayload.Length <= 0)
            {
                throw new ArgumentNullException("cipheredPayload");
            }
            if (key_array == null || key_array.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }
            if (iv_array == null || iv_array.Length <= 0)
            {
                throw new ArgumentNullException("Key");
            }

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an Aes object
            // with the specified key and IV.
            using (Aes aes = Aes.Create())
            {
                aes.Key = key_array;
                aes.IV = iv_array;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.PKCS7;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipheredPayload))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return Encoding.ASCII.GetBytes(plaintext);
        }