示例#1
0
        public void EnhancedMemoryStream_Bytes32()
        {
            var es = new EnhancedMemoryStream();

            byte[] read, write;

            es.WriteBytes32(null);
            es.Seek(0, SeekOrigin.Begin);
            read = es.ReadBytes32();
            Assert.IsNull(read);

            write = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            es.Seek(0, SeekOrigin.Begin);
            es.WriteBytes32(write);
            es.Seek(0, SeekOrigin.Begin);
            read = es.ReadBytes32();
            CollectionAssert.AreEqual(write, read);

            write = new byte[40000];
            for (int i = 0; i < write.Length; i++)
            {
                write[i] = (byte)i;
            }

            es.Seek(0, SeekOrigin.Begin);
            es.WriteBytes32(write);
            es.Seek(0, SeekOrigin.Begin);
            read = es.ReadBytes32();
            CollectionAssert.AreEqual(write, read);
        }
示例#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();
            }
        }