示例#1
0
        /// <summary>
        /// Compress and encrypt a file, then decrypt and deflate to an output directory
        /// </summary>
        /// 
        /// <param name="InputDirectory">The path of the folder to be archived</param>
        /// <param name="OutputDirectory">The decompressed files output directory</param>
        /// <param name="CompressedFilePath">The name and path of the new compressed and encrypted archive</param>
        public static void CompressionCipherTest(string InputDirectory, string OutputDirectory, string CompressedFilePath)
        {
            KeyParams kp = new KeyGenerator().GetKeyParams(32, 16);
            // Create an archive //
            // create the cipher
            using (ICipherMode cipher = new CTR(new RDX()))
            {
                // initialize the cipher for encryption
                cipher.Initialize(true, kp);

                // create the archive file
                using (FileStream fs = new FileStream(CompressedFilePath, FileMode.Create))
                {
                    // compress and encrypt directory
                    using (CompressionCipher cc = new CompressionCipher(true, cipher))
                    {
                        // set the input folder path and archive output stream
                        cc.Initialize(InputDirectory, fs);
                        // write the compressed and encrypted archive to file
                        cc.Write();
                    }
                }
            }

            // Inflate an archive //
            // create the cipher
            using (ICipherMode cipher = new CTR(new RDX()))
            {
                // initialize the cipher for decryption
                cipher.Initialize(false, kp);

                // open the archive
                using (FileStream decmp = new FileStream(CompressedFilePath, FileMode.Open))
                {
                    // decrypt and inflate to output directory
                    using (CompressionCipher cc = new CompressionCipher(false, cipher))
                    {
                        // set the output folder path and archive path
                        cc.Initialize(OutputDirectory, decmp);
                        // decrypt and inflate the directory
                        cc.Write();
                    }
                }
            }
            // manual inspection of files..
        }
示例#2
0
        /// <remarks>
        /// Encrypts the key package buffer
        /// </remarks>
        private void TransformBuffer(byte[] KeyData, byte[] Salt)
        {
            byte[] kvm = new byte[48];

            // use salt to derive key and counter vector
            using (Keccak512 digest = new Keccak512(384))
                kvm = digest.ComputeHash(Salt);

            byte[] key = new byte[32];
            byte[] iv = new byte[16];
            Buffer.BlockCopy(kvm, 0, key, 0, key.Length);
            Buffer.BlockCopy(kvm, key.Length, iv, 0, iv.Length);

            using (KeyParams keyparam = new KeyParams(key, iv))
            {
                // 40 rounds of serpent
                using (CTR cipher = new CTR(new SPX(40)))
                {
                    cipher.Initialize(true, keyparam);
                    cipher.Transform(KeyData, KeyData);
                }
            }
        }
示例#3
0
        private void ParallelTest()
        {
            byte[] data;
            byte[] dec1;
            byte[] dec2;
            byte[] enc1;
            byte[] enc2;
            int blockSize;
            KeyParams keyParam = new KeyParams(GetBytes(32), GetBytes(16));

            // CTR mode
            using (CTR cipher = new CTR(new RDX()))
            {
                data = GetBytes(1036);

                // how to calculate an ideal block size
                int plen = (data.Length / cipher.ParallelMinimumSize) * cipher.ParallelMinimumSize;
                // you can factor it up or down or use a default
                if (plen > cipher.ParallelMaximumSize)
                    plen = 1024;

                // set parallel block size
                cipher.ParallelBlockSize = plen;

                // encrypt //
                // parallel
                cipher.Initialize(true, keyParam);
                cipher.IsParallel = true;
                blockSize = cipher.ParallelBlockSize;
                enc1 = Transform1(cipher, data, blockSize);

                // normal mode
                cipher.Initialize(true, keyParam);
                cipher.IsParallel = false;
                blockSize = cipher.BlockSize;
                enc2 = Transform1(cipher, data, blockSize);

                // decrypt
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize = cipher.ParallelBlockSize;
                dec1 = Transform1(cipher, enc1, blockSize);

                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize = cipher.BlockSize;
                dec2 = Transform1(cipher, enc1, blockSize);
            }

            if (Compare.AreEqual(enc1, enc2) == false)
                throw new Exception("Parallel CTR: Encrypted output is not equal!");
            if (Compare.AreEqual(dec1, dec2) == false)
                throw new Exception("Parallel CTR: Decrypted output is not equal!");

            OnProgress(new TestEventArgs("Passed Parallel CTR encryption and decryption tests.."));

            // CBC mode
            using (CBC cipher = new CBC(new RDX()))
            {
                // must be divisible by block size, add padding if required
                data = GetBytes(2048);

                // encrypt
                cipher.ParallelBlockSize = 1024;

                // encrypt only in normal mode for cbc
                cipher.Initialize(true, keyParam);
                blockSize = cipher.BlockSize;
                enc1 = Transform1(cipher, data, blockSize);

                // decrypt
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize = cipher.ParallelBlockSize;
                dec1 = Transform1(cipher, enc1, blockSize);

                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize = cipher.BlockSize;
                dec2 = Transform1(cipher, enc1, blockSize);
            }

            if (Compare.AreEqual(dec1, dec2) == false)
                throw new Exception("Parallel CBC: Decrypted output is not equal!");

            OnProgress(new TestEventArgs("Passed Parallel CBC decryption tests.."));

            // CFB mode
            using (CFB cipher = new CFB(new RDX()))
            {
                // must be divisible by block size, add padding if required
                data = GetBytes(2048);

                // encrypt
                cipher.ParallelBlockSize = 1024;

                // encrypt only in normal mode for cfb
                cipher.Initialize(true, keyParam);
                blockSize = cipher.BlockSize;
                enc1 = Transform1(cipher, data, blockSize);

                // decrypt
                cipher.Initialize(false, keyParam);
                cipher.IsParallel = true;
                blockSize = cipher.ParallelBlockSize;
                dec1 = Transform1(cipher, enc1, blockSize);

                cipher.Initialize(false, keyParam);
                cipher.IsParallel = false;
                blockSize = cipher.BlockSize;
                dec2 = Transform1(cipher, enc1, blockSize);
            }

            if (Compare.AreEqual(dec1, dec2) == false)
                throw new Exception("Parallel CFB: Decrypted output is not equal!");
            if (Compare.AreEqual(dec1, data) == false)
                throw new Exception("Parallel CFB: Decrypted output is not equal!");

            OnProgress(new TestEventArgs("Passed Parallel CFB decryption tests.."));

            // dispose container
            keyParam.Dispose();
        }
示例#4
0
        private void CTRTest(byte[] Key, byte[,][] Input, byte[,][] Output)
        {
            byte[] outBytes = new byte[16];
            byte[] iv = _vectors[1];
            int index = 24;

            if (Key.Length == 24)
                index = 26;
            else if (Key.Length == 32)
                index = 28;

            using (CTR mode = new CTR(new RDX()))
            {
                mode.Initialize(true, new KeyParams(Key, iv));

                for (int i = 0; i < 4; i++)
                {
                    mode.Transform(Input[index, i], outBytes);

                    if (Compare.AreEqual(outBytes, Output[index, i]) == false)
                        throw new Exception("CTR Mode: Encrypted arrays are not equal!");
                }
            }

            index++;
            using (CTR mode = new CTR(new RDX()))
            {
                mode.Initialize(false, new KeyParams(Key, iv));

                for (int i = 0; i < 4; i++)
                {
                    mode.Transform(Input[index, i], outBytes);

                    if (Compare.AreEqual(outBytes, _output[index, i]) == false)
                        throw new Exception("CTR Mode: Decrypted arrays are not equal!");
                }
            }
        }
示例#5
0
        /// <summary>
        /// Test the PacketCipher class implementation
        /// <para>Throws an Exception on failure</</para>
        /// </summary>
        public static void PacketCipherTest()
        {
            const int BLSZ = 1024;
            KeyParams key;
            byte[] data;
            MemoryStream instrm;
            MemoryStream outstrm = new MemoryStream();

            using (KeyGenerator kg = new KeyGenerator())
            {
                // get the key
                key = kg.GetKeyParams(32, 16);
                // 2 * 1200 byte packets
                data = kg.GetBytes(BLSZ * 2);
            }
            // data to encrypt
            instrm = new MemoryStream(data);

            // Encrypt a stream //
            // create the outbound cipher
            using (ICipherMode cipher = new CTR(new RDX()))
            {
                // initialize the cipher for encryption
                cipher.Initialize(true, key);
                // set block size
                ((CTR)cipher).ParallelBlockSize = BLSZ;

                // encrypt the stream
                using (PacketCipher pc = new PacketCipher(cipher))
                {
                    byte[] inbuffer = new byte[BLSZ];
                    byte[] outbuffer = new byte[BLSZ];
                    int bytesread = 0;

                    while ((bytesread = instrm.Read(inbuffer, 0, BLSZ)) > 0)
                    {
                        // encrypt the buffer
                        pc.Write(inbuffer, 0, outbuffer, 0, BLSZ);
                        // add it to the output stream
                        outstrm.Write(outbuffer, 0, outbuffer.Length);
                    }
                }
            }

            // reset stream position
            outstrm.Seek(0, SeekOrigin.Begin);
            MemoryStream tmpstrm = new MemoryStream();

            // Decrypt a stream //
            // create the inbound cipher
            using (ICipherMode cipher = new CTR(new RDX()))
            {
                // initialize the cipher for decryption
                cipher.Initialize(false, key);
                // set block size
                ((CTR)cipher).ParallelBlockSize = BLSZ;

                // decrypt the stream
                using (PacketCipher pc = new PacketCipher(cipher))
                {
                    byte[] inbuffer = new byte[BLSZ];
                    byte[] outbuffer = new byte[BLSZ];
                    int bytesread = 0;

                    while ((bytesread = outstrm.Read(inbuffer, 0, BLSZ)) > 0)
                    {
                        // process the encrypted bytes
                        pc.Write(inbuffer, 0, outbuffer, 0, BLSZ);
                        // write to stream
                        tmpstrm.Write(outbuffer, 0, outbuffer.Length);
                    }
                }
            }

            // compare decrypted output with data
            if (!Compare.AreEqual(tmpstrm.ToArray(), data))
                throw new Exception();
        }
示例#6
0
        /// <summary>
        /// Test the StreamCipher class implementation
        /// <para>Throws an Exception on failure</</para>
        /// </summary>
        public static void StreamCipherTest()
        {
            const int BLSZ = 1024;
            KeyParams key;
            byte[] data;
            MemoryStream instrm;
            MemoryStream outstrm = new MemoryStream();

            using (KeyGenerator kg = new KeyGenerator())
            {
                // get the key
                key = kg.GetKeyParams(32, 16);
                // 2048 bytes
                data = kg.GetBytes(BLSZ * 2);
            }
            // data to encrypt
            instrm = new MemoryStream(data);

            // Encrypt a stream //
            // create the outbound cipher
            using (ICipherMode cipher = new CTR(new RDX()))
            {
                // initialize the cipher for encryption
                cipher.Initialize(true, key);
                // set block size
                ((CTR)cipher).ParallelBlockSize = BLSZ;

                // encrypt the stream
                using (StreamCipher sc = new StreamCipher(cipher))
                {
                    sc.Initialize(instrm, outstrm);
                    // encrypt the buffer
                    sc.Write();
                }
            }

            // reset stream position
            outstrm.Seek(0, SeekOrigin.Begin);
            MemoryStream tmpstrm = new MemoryStream();

            // Decrypt a stream //
            // create the decryption cipher
            using (ICipherMode cipher = new CTR(new RDX()))
            {
                // initialize the cipher for decryption
                cipher.Initialize(false, key);
                // set block size
                ((CTR)cipher).ParallelBlockSize = BLSZ;

                // decrypt the stream
                using (StreamCipher sc = new StreamCipher(cipher))
                {
                    sc.Initialize(outstrm, tmpstrm);
                    // process the encrypted bytes
                    sc.Write();
                }
            }

            // compare decrypted output with data
            if (!Compare.AreEqual(tmpstrm.ToArray(), data))
                throw new Exception();
        }