コード例 #1
0
ファイル: CipherStreamTest.cs プロジェクト: todoasap/CEX-NET
        private void DescriptionTest(CipherDescription Description)
        {
            AllocateRandom(ref _iv, 16);
            AllocateRandom(ref _key, 32);
            AllocateRandom(ref _plnText);

            KeyParams    kp   = new KeyParams(_key, _iv);
            MemoryStream mIn  = new MemoryStream(_plnText);
            MemoryStream mOut = new MemoryStream();
            MemoryStream mRes = new MemoryStream();

            CipherStream cs = new CipherStream(Description);

            cs.Initialize(true, kp);
            cs.Write(mIn, mOut);

            mOut.Seek(0, SeekOrigin.Begin);

            cs.Initialize(false, kp);
            cs.Write(mOut, mRes);

            if (!Evaluate.AreEqual(mRes.ToArray(), _plnText))
            {
                throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
            }
        }
コード例 #2
0
ファイル: CipherStreamTest.cs プロジェクト: todoasap/CEX-NET
        void StreamModesTest(ICipherMode Cipher, IPadding Padding)
        {
            if (Cipher.Engine.LegalKeySizes[0] > 32)
            {
                AllocateRandom(ref _key, 192);
            }
            else
            {
                AllocateRandom(ref _key, 32);
            }

            AllocateRandom(ref _iv, 16);
            // we are testing padding modes, make sure input size is random, but -not- block aligned..
            AllocateRandom(ref _plnText, 0, Cipher.BlockSize);

            KeyParams    kp   = new KeyParams(_key, _iv);
            MemoryStream mIn  = new MemoryStream(_plnText);
            MemoryStream mOut = new MemoryStream();
            MemoryStream mRes = new MemoryStream();

            CipherStream cs = new CipherStream(Cipher, Padding);

            cs.Initialize(true, kp);
            cs.Write(mIn, mOut);

            cs.Initialize(false, kp);
            mOut.Seek(0, SeekOrigin.Begin);
            cs.Write(mOut, mRes);

            int pos = (int)mRes.Position;

            byte[] res = new byte[_plnText.Length];
            Buffer.BlockCopy(mRes.ToArray(), 0, res, 0, pos);

            if (!Evaluate.AreEqual(res, _plnText))
            {
                throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
            }

            Cipher.Dispose();
        }
コード例 #3
0
ファイル: VolumeCipher.cs プロジェクト: todoasap/CEX-NET
        /// <summary>
        /// Decrypt a single file in the volume
        /// </summary>
        ///
        /// <param name="InputPath">The path to the encrypted file</param>
        /// <param name="OututPath">The path to the new decrypted file</param>
        public void Decrypt(string InputPath, string OututPath)
        {
            FileStream   inpStream = GetStream(InputPath, true);
            VolumeHeader vh        = GetHeader(inpStream);
            KeyParams    key       = VolumeKey.FromId(m_keyStream, vh.FileId);

            if (key == null)
            {
                if (ErrorNotification != null)
                {
                    ErrorNotification(this, string.Format("The file {0}; has no key assigned", InputPath));
                }
            }
            else
            {
                FileStream outStream = GetStream(OututPath, false);

                if (inpStream == null || outStream == null)
                {
                    if (ErrorNotification != null)
                    {
                        ErrorNotification(this, string.Format("The file {0}; could not be written to", OututPath));
                    }
                }
                else
                {
                    m_volumeKey.State[m_volumeKey.GetIndex(vh.FileId)] = (byte)VolumeKeyStates.Decrypted;
                    m_cipherStream.ProgressPercent += OnCipherProgress;
                    m_cipherStream.Initialize(false, key);
                    m_cipherStream.Write(inpStream, outStream);
                    m_cipherStream.ProgressPercent -= OnCipherProgress;
                    outStream.SetLength(outStream.Length - VolumeHeader.GetHeaderSize);
                    inpStream.Dispose();
                    outStream.Dispose();
                    UpdateKey();
                }
            }
        }
コード例 #4
0
ファイル: ProcessingTests.cs プロジェクト: todoasap/CEX-NET
        /// <summary>
        /// Test the CipherStream 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 RHX()))
            {
                // set block size
                ((CTR)cipher).ParallelBlockSize = BLSZ;

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

            // 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 RHX()))
            {
                // set block size
                ((CTR)cipher).ParallelBlockSize = BLSZ;

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

            // compare decrypted output with data
            if (!Evaluate.AreEqual(tmpstrm.ToArray(), data))
            {
                throw new Exception();
            }
        }
コード例 #5
0
ファイル: CipherStreamTest.cs プロジェクト: todoasap/CEX-NET
        void StreamTest()
        {
            AllocateRandom(ref _iv, 8);
            AllocateRandom(ref _key, 32);

            KeyParams    kp      = new KeyParams(_key, _iv);
            Salsa20      cipher  = new Salsa20();
            Salsa20      cipher2 = new Salsa20();
            CipherStream cs      = new CipherStream(cipher2);

            cipher.IsParallel = false;

            // ctr test
            for (int i = 0; i < 10; i++)
            {
                int sze      = AllocateRandom(ref _plnText);
                int prlBlock = sze - (sze % (cipher.BlockSize * _processorCount));
                _cmpText = new byte[sze];
                _decText = new byte[sze];
                _encText = new byte[sze];

                cipher.ParallelBlockSize = prlBlock;
                cs.ParallelBlockSize     = prlBlock;
                MemoryStream mIn  = new MemoryStream(_plnText);
                MemoryStream mOut = new MemoryStream();
                MemoryStream mRes = new MemoryStream();

                // *** Compare encryption output *** //

                // local processor
                cipher.Initialize(kp);
                ProcessStream(cipher, _plnText, 0, _encText, 0);

                // streamcipher linear mode
                cs.IsParallel = false;
                // memorystream interface
                cs.Initialize(true, kp);
                cs.Write(mIn, mOut);

                if (!Evaluate.AreEqual(mOut.ToArray(), _encText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(true, kp);
                cs.Write(_plnText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _encText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                mIn.Seek(0, SeekOrigin.Begin);
                mOut.Seek(0, SeekOrigin.Begin);

                // parallel test
                cs.IsParallel = true;
                cs.Initialize(true, kp);
                cs.Write(mIn, mOut);

                if (!Evaluate.AreEqual(mOut.ToArray(), _encText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(true, kp);
                cs.Write(_plnText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _encText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                // ***compare decryption output *** //

                // local processor
                cipher.Initialize(kp);
                ProcessStream(cipher, _encText, 0, _decText, 0);

                if (!Evaluate.AreEqual(_plnText, _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // decrypt linear mode
                cs.IsParallel = false;
                mOut.Seek(0, SeekOrigin.Begin);
                cs.Initialize(false, kp);
                cs.Write(mOut, mRes);

                if (!Evaluate.AreEqual(mRes.ToArray(), _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(false, kp);
                cs.Write(_encText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // decrypt parallel mode
                cs.IsParallel = true;
                mOut.Seek(0, SeekOrigin.Begin);
                mRes.Seek(0, SeekOrigin.Begin);
                cs.Initialize(false, kp);
                cs.Write(mOut, mRes);

                if (!Evaluate.AreEqual(mRes.ToArray(), _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(false, kp);
                cs.Write(_encText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }
            }

            cipher.Dispose();
            cipher2.Dispose();
        }
コード例 #6
0
ファイル: CipherStreamTest.cs プロジェクト: todoasap/CEX-NET
        private void ParametersTest()
        {
            AllocateRandom(ref _iv, 16);
            AllocateRandom(ref _key, 32);
            AllocateRandom(ref _plnText, 1);

            KeyParams kp = new KeyParams(_key, _iv);

            _cmpText = new byte[1];
            _decText = new byte[1];
            _encText = new byte[1];

            RHX engine = new RHX();

            // 1 byte w/ byte arrays
            {
                CTR          cipher = new CTR(engine, false);
                CipherStream cs     = new CipherStream(cipher);

                cs.Initialize(true, kp);
                cs.Write(_plnText, 0, ref _encText, 0);

                cs.Initialize(false, kp);
                cs.Write(_encText, 0, ref _decText, 0);

                if (!Evaluate.AreEqual(_decText, _plnText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                cipher.Dispose();
            }
            // 1 byte w/ stream
            {
                CTR          cipher = new CTR(engine, false);
                CipherStream cs     = new CipherStream(cipher);
                cs.Initialize(true, kp);
                AllocateRandom(ref _plnText, 1);
                MemoryStream mIn  = new MemoryStream(_plnText);
                MemoryStream mOut = new MemoryStream();
                cs.Write(mIn, mOut);

                cs.Initialize(false, kp);
                MemoryStream mRes = new MemoryStream();
                mOut.Seek(0, SeekOrigin.Begin);
                cs.Write(mOut, mRes);

                if (!Evaluate.AreEqual(mRes.ToArray(), _plnText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                cipher.Dispose();
            }

            // partial block w/ byte arrays
            {
                CTR          cipher = new CTR(engine, false);
                CipherStream cs     = new CipherStream(cipher);
                AllocateRandom(ref _plnText, 15);
                Array.Resize(ref _decText, 15);
                Array.Resize(ref _encText, 15);

                cs.Initialize(true, kp);
                cs.Write(_plnText, 0, ref _encText, 0);

                cs.Initialize(false, kp);
                cs.Write(_encText, 0, ref _decText, 0);

                if (!Evaluate.AreEqual(_decText, _plnText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                cipher.Dispose();
            }
            // partial block w/ stream
            {
                CTR          cipher = new CTR(engine, false);
                CipherStream cs     = new CipherStream(cipher);
                AllocateRandom(ref _plnText, 15);
                Array.Resize(ref _decText, 15);
                Array.Resize(ref _encText, 15);

                cs.Initialize(true, kp);
                MemoryStream mIn  = new MemoryStream(_plnText);
                MemoryStream mOut = new MemoryStream();
                cs.Write(mIn, mOut);

                cs.Initialize(false, kp);
                MemoryStream mRes = new MemoryStream();
                mOut.Seek(0, SeekOrigin.Begin);
                cs.Write(mOut, mRes);

                if (!Evaluate.AreEqual(mRes.ToArray(), _plnText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                cipher.Dispose();
            }

            // random block sizes w/ byte arrays
            {
                for (int i = 0; i < 100; i++)
                {
                    CTR cipher = new CTR(engine, false);

                    int sze = AllocateRandom(ref _plnText);
                    _decText = new byte[sze];
                    _encText = new byte[sze];

                    CipherStream cs = new CipherStream(cipher);
                    cs.Initialize(true, kp);
                    cs.Write(_plnText, 0, ref _encText, 0);

                    cs.Initialize(false, kp);
                    cs.Write(_encText, 0, ref _decText, 0);

                    if (!Evaluate.AreEqual(_decText, _plnText))
                    {
                        throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                    }

                    cipher.Dispose();
                }
            }
            // random block sizes w/ stream
            {
                for (int i = 0; i < 100; i++)
                {
                    CTR cipher = new CTR(engine, false);
                    int sze    = AllocateRandom(ref _plnText);
                    _decText = new byte[sze];
                    _encText = new byte[sze];

                    CipherStream cs = new CipherStream(cipher);
                    cs.Initialize(true, kp);
                    MemoryStream mIn  = new MemoryStream(_plnText);
                    MemoryStream mOut = new MemoryStream();
                    cs.Write(mIn, mOut);

                    cs.Initialize(false, kp);
                    MemoryStream mRes = new MemoryStream();
                    mOut.Seek(0, SeekOrigin.Begin);
                    cs.Write(mOut, mRes);

                    if (!Evaluate.AreEqual(mRes.ToArray(), _plnText))
                    {
                        throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                    }

                    cipher.Dispose();
                }
            }

            engine.Dispose();
        }
コード例 #7
0
ファイル: CipherStreamTest.cs プロジェクト: todoasap/CEX-NET
        private void OfbModeTest()
        {
            AllocateRandom(ref _iv, 16);
            AllocateRandom(ref _key, 32);

            KeyParams kp      = new KeyParams(_key, _iv);
            RHX       eng     = new RHX();
            OFB       cipher  = new OFB(eng);
            OFB       cipher2 = new OFB(eng);
            ISO7816   padding = new ISO7816();

            cipher.IsParallel = false;
            CipherStream cs = new CipherStream(cipher2, padding);

            for (int i = 0; i < 10; i++)
            {
                int sze      = AllocateRandom(ref _plnText, 0, eng.BlockSize);
                int prlBlock = sze - (sze % (cipher.BlockSize * _processorCount));
                _cmpText = new byte[sze];
                _decText = new byte[sze];
                _encText = new byte[sze];

                MemoryStream mIn  = new MemoryStream(_plnText);
                MemoryStream mOut = new MemoryStream();
                MemoryStream mRes = new MemoryStream();

                // *** Compare encryption output *** //

                // local processor
                cipher.Initialize(true, kp);
                BlockEncrypt(cipher, padding, _plnText, 0, ref _encText, 0);

                // streamcipher linear mode
                cs.IsParallel = false;
                // memorystream interface
                cs.Initialize(true, kp);
                cs.Write(mIn, mOut);

                if (!Evaluate.AreEqual(mOut.ToArray(), _encText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(true, kp);
                cs.Write(_plnText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _encText))
                {
                    throw new Exception("CipherStreamTest: Encrypted arrays are not equal!");
                }

                // ***compare decryption output *** //

                // local processor
                cipher.Initialize(false, kp);
                BlockDecrypt(cipher, padding, _encText, 0, ref _decText, 0);

                if (!Evaluate.AreEqual(_plnText, _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // decrypt linear mode
                cipher2.IsParallel = false;
                mOut.Seek(0, SeekOrigin.Begin);
                cs.Initialize(false, kp);
                cs.Write(mOut, mRes);

                if (!Evaluate.AreEqual(mRes.ToArray(), _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }

                // byte array interface
                cs.Initialize(false, kp);
                Array.Resize(ref _cmpText, _encText.Length);
                cs.Write(_encText, 0, ref _cmpText, 0);

                if (!Evaluate.AreEqual(_cmpText, _decText))
                {
                    throw new Exception("CipherStreamTest: Decrypted arrays are not equal!");
                }
            }

            eng.Dispose();
        }