예제 #1
0
        /// <summary>
        /// Encrypts a byte array using the specified encryption algorithm,
        /// key, and initialization vector after adding eight bytes of
        /// cryptographic salt.
        /// </summary>
        /// <param name="input">The clear text array.</param>
        /// <param name="algorithm">The symmetric algorithm name.</param>
        /// <param name="key">The encryption key.</param>
        /// <param name="IV">The initialization vector.</param>
        /// <returns>The encrypted output.</returns>
        /// <remarks>
        /// <note>
        /// This method returns an zero length result if the input
        /// array has zero length.
        /// </note>
        /// </remarks>
        public static byte[] EncryptWithSalt8(byte[] input, string algorithm, byte[] key, byte[] IV)
        {
            if (input.Length == 0)
            {
                return(new byte[0]);
            }

            using (BlockEncryptor encryptor = new BlockEncryptor(algorithm, key, IV))
                return(encryptor.Encrypt(Helper.Concat(Crypto.GetSalt8(), input)));
        }
예제 #2
0
        /// <summary>
        /// Performs a secure symmetric encryption including cryptographic salt, padding, and
        /// data validation.
        /// </summary>
        /// <param name="symmetricKey">The symmetric algorithm arguments.</param>
        /// <param name="plainText">The unencrypted data.</param>
        /// <param name="paddedSize">Specifies the minimum padded size of the encrypted content.</param>
        /// <returns>The encrypted result.</returns>
        public static byte[] Encrypt(SymmetricKey symmetricKey, byte[] plainText, int paddedSize)
        {
            EnhancedMemoryStream output    = new EnhancedMemoryStream(Math.Max(plainText.Length, paddedSize) + 512);
            EnhancedMemoryStream ms        = new EnhancedMemoryStream(512);
            BlockEncryptor       encryptor = new BlockEncryptor(symmetricKey);

            try
            {
                // Write header fields

                output.WriteInt32(Magic);
                output.WriteInt32(0);

                // Write encrypted contents

                ms.WriteInt32(Magic);
                ms.WriteBytesNoLen(Crypto.GetSalt8());
                ms.WriteBytes32(plainText);

                for (int i = plainText.Length; i < paddedSize; i++)
                {
                    ms.WriteByte((byte)i);     // Padding bytes
                }
                output.WriteBytes32(encryptor.Encrypt(ms.ToArray()));

                // That's it, we're done.

                return(output.ToArray());
            }
            finally
            {
                if (encryptor != null)
                {
                    encryptor.Dispose();
                }

                output.Close();
                ms.Close();
            }
        }
예제 #3
0
        /// <summary>
        /// Encrypts a byte array using a combination of an asymmetric RSA key and the
        /// specified symmetric encryption algorithm and a one-time key generated by
        /// the method.
        /// </summary>
        /// <param name="rsaKey">The encrypting RSA key as XML or as a secure key container name.</param>
        /// <param name="plainText">The data to be encrypted.</param>
        /// <param name="algorithm">The symmetric encryption algorithm name.</param>
        /// <param name="keySize">The one-time symmetric key size to generate in bits.</param>
        /// <param name="paddedSize">Specifies the minimum padded size of the encrypted content.</param>
        /// <param name="symmetricKey">Returns as the symmetric encryption algorithm arguments.</param>
        /// <returns>The encrypted result.</returns>
        /// <remarks>
        /// <para>
        /// Note that applications should take some care to ensure that the <paramref name="symmetricKey" />
        /// value return is disposed so that the symmetric encryption key will be cleared.
        /// </para>
        /// <para>
        /// The current supported cross platform encryption algorithms
        /// are: "DES", "RC2", "TripleDES", and "AES" (Rijndael).
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentException">Thrown if the requested encryption algorithm is unknown.</exception>
        public static byte[] Encrypt(string rsaKey, byte[] plainText, string algorithm, int keySize, int paddedSize,
                                     out SymmetricKey symmetricKey)
        {
            EnhancedMemoryStream output    = new EnhancedMemoryStream(Math.Max(plainText.Length, paddedSize) + 512);
            EnhancedMemoryStream ms        = new EnhancedMemoryStream(512);
            BlockEncryptor       encryptor = null;

            byte[] symKey;
            byte[] symIV;

            Crypto.GenerateSymmetricKey(algorithm, keySize, out symKey, out symIV);

            encryptor    = new BlockEncryptor(algorithm, symKey, symIV);
            symmetricKey = new SymmetricKey(algorithm, (byte[])symKey.Clone(), (byte[])symIV.Clone());

            try
            {
                // Write header fields

                output.WriteInt32(Magic);
                output.WriteInt32(0);

                // Write encryption Info

                ms.WriteString16(algorithm);
                ms.WriteBytes16(symKey);
                ms.WriteBytes16(symIV);
                ms.WriteBytesNoLen(Crypto.GetSalt8());
                output.WriteBytes16(AsymmetricCrypto.Encrypt(CryptoAlgorithm.RSA, rsaKey, ms.ToArray()));

                // Write encrypted contents

                ms.SetLength(0);
                ms.WriteInt32(Magic);
                ms.WriteBytesNoLen(Crypto.GetSalt8());
                ms.WriteBytes32(plainText);

                for (int i = plainText.Length; i < paddedSize; i++)
                {
                    ms.WriteByte((byte)i);     // Padding bytes
                }
                output.WriteBytes32(encryptor.Encrypt(ms.ToArray()));

                // That's it, we're done.

                return(output.ToArray());
            }
            finally
            {
                if (symKey != null)
                {
                    Array.Clear(symKey, 0, symKey.Length);
                }

                if (symIV != null)
                {
                    Array.Clear(symIV, 0, symIV.Length);
                }

                if (encryptor != null)
                {
                    encryptor.Dispose();
                }

                output.Close();
                ms.Close();
            }
        }
예제 #4
0
 /// <summary>
 /// Encodes a string as UTF8 and encryptes it using the specified
 /// encryption algorithm, key, and initialization vector after
 /// adding eight bytes of cryptographic salt.
 /// </summary>
 /// <param name="input">The clear text string.</param>
 /// <param name="algorithm">The symmetric algorithm name.</param>
 /// <param name="key">The encryption key.</param>
 /// <param name="IV">The initialization vector.</param>
 /// <returns>The encrypted output.</returns>
 public static byte[] EncryptStringWithSalt8(string input, string algorithm, byte[] key, byte[] IV)
 {
     using (BlockEncryptor encryptor = new BlockEncryptor(algorithm, key, IV))
         return(encryptor.Encrypt(Helper.Concat(Crypto.GetSalt8(), Helper.ToUTF8(input))));
 }
예제 #5
0
 /// <summary>
 /// Encrypts a byte array using the specified encryption algorithm,
 /// key, and initialization vector.
 /// </summary>
 /// <param name="input">The clear text array.</param>
 /// <param name="algorithm">The symmetric algorithm name.</param>
 /// <param name="key">The encryption key.</param>
 /// <param name="IV">The initialization vector.</param>
 /// <returns>The encrypted output.</returns>
 public static byte[] Encrypt(byte[] input, string algorithm, byte[] key, byte[] IV)
 {
     using (BlockEncryptor encryptor = new BlockEncryptor(algorithm, key, IV))
         return(encryptor.Encrypt(input));
 }