Пример #1
0
        public SymmetricTransform(SymmetricAlgorithmPlus algo, bool encryptMode, byte[] iv)
        {
            _algo          = algo;
            _blockBytes    = algo.BlockSize >> 3;
            _encryptMode   = encryptMode;
            _mt_threshold  = _blockBytes * 2;
            _iv            = (byte[])iv.Clone();
            _mode          = algo.ModePlus;
            _decryptBuffer = new byte[OutputBlockSize];

            if (_algo.NumberOfThreads > 1 && (algo.ModePlus == CipherModePlus.ECB || algo.ModePlus == CipherModePlus.CTR || (algo.ModePlus == CipherModePlus.CBC && !encryptMode)))
            {
                _useThread  = true;
                _waitHandle = new ConfluentWaitHandle();
                _threads    = _algo.NumberOfThreads;
                if (_mode != CipherModePlus.ECB)
                {
                    _mt_temp = new byte[_threads][];
                    for (int i = 0; i < _mt_temp.Length; i++)
                    {
                        _mt_temp[i] = new byte[iv.Length];
                    }
                    _temp = _mt_temp[0];
                }
            }
            else if (_mode != CipherModePlus.ECB)
            {
                _temp = new byte[iv.Length];
            }
        }
Пример #2
0
        public SymmetricKey(SymmetricAlgorithmType type, byte[] iv, byte[] key, CipherModePlus cipherMode, PaddingMode paddingMode, bool enableIVShuffle)
        {
            _type = type;
            switch (type) {
                case SymmetricAlgorithmType.None:
                    _algo = null;
                    return;
                case SymmetricAlgorithmType.Camellia:
                    _algo = new openCrypto.CamelliaManaged ();
                    break;
                case SymmetricAlgorithmType.Rijndael:
                    _algo = new openCrypto.RijndaelManaged ();
                    break;
                default:
                    throw new ArgumentOutOfRangeException ();
            }

            _algo.ModePlus = cipherMode;
            _algo.Padding = paddingMode;
            _algo.KeySize = key.Length << 3;
            _algo.BlockSize = iv.Length << 3;
            _algo.FeedbackSize = iv.Length << 3;
            _iv = iv;
            _key = key;
            _ivShuffle = enableIVShuffle;
        }
Пример #3
0
		static double[] Run (SymmetricAlgorithmPlus algo, CipherModePlus mode, int keySize, int blockSize, int dataSize, int threads)
		{
			double[] result = SpeedTest.Run (algo, mode, keySize, blockSize, dataSize, threads);
			for (int i = 0; i < 10; i++) {
				double[] temp = SpeedTest.Run (algo, mode, keySize, blockSize, dataSize, threads);
				result[0] = Math.Max (result[0], temp[0]);
				result[1] = Math.Max (result[1], temp[1]);
			}
			return result;
		}
Пример #4
0
 static double[] Run(SymmetricAlgorithmPlus algo, CipherModePlus mode, int keySize, int blockSize, int dataSize, int threads)
 {
     double[] result = SpeedTest.Run(algo, mode, keySize, blockSize, dataSize, threads);
     for (int i = 0; i < 10; i++)
     {
         double[] temp = SpeedTest.Run(algo, mode, keySize, blockSize, dataSize, threads);
         result[0] = Math.Max(result[0], temp[0]);
         result[1] = Math.Max(result[1], temp[1]);
     }
     return(result);
 }
Пример #5
0
        public static double[] Run(SymmetricAlgorithmPlus algo, CipherModePlus mode, int keySize, int blockSize, int dataSize, int threads)
        {
            double[] result;
            int      totalBlocks = dataSize / (blockSize >> 3);

            byte[] key = new byte [keySize >> 3];
            byte[] iv  = new byte [blockSize >> 3];
            algo.NumberOfThreads = threads;
            algo.ModePlus        = mode;
            result = Run(algo, key, iv, totalBlocks);
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = dataSize * 8.0 / result[i] / (1024.0 * 1024.0);
            }
            return(result);
        }
Пример #6
0
        private void RunTest(SymmetricAlgorithmPlus algo, CipherModePlus mode)
        {
            const int TestDataSize = 1 << 20;

            byte[] data1 = new byte[TestDataSize];
            byte[] data2 = new byte[TestDataSize];
            byte[] data3 = new byte[TestDataSize];
            byte[] iv    = new byte[algo.IV.Length];
            byte[] key   = new byte[algo.Key.Length];

            RNG.GetBytes(data1);
            RNG.GetBytes(key);
            RNG.GetBytes(iv);
            algo.ModePlus = mode;

            algo.NumberOfThreads = 4;
            using (ICryptoTransform ct = algo.CreateEncryptor(key, iv)) {
                ct.TransformBlock(data1, 0, TestDataSize, data2, 0);
            }
            algo.NumberOfThreads = 1;
            using (ICryptoTransform ct = algo.CreateDecryptor(key, iv)) {
                ct.TransformBlock(data2, 0, TestDataSize, data3, 0);
            }
            for (int i = 0; i < TestDataSize; i++)
            {
                Assert.AreEqual(data1[i], data3[i], "Encryption Error");
            }

            algo.NumberOfThreads = 1;
            using (ICryptoTransform ct = algo.CreateEncryptor(key, iv)) {
                ct.TransformBlock(data1, 0, TestDataSize, data2, 0);
            }
            algo.NumberOfThreads = 4;
            using (ICryptoTransform ct = algo.CreateDecryptor(key, iv)) {
                ct.TransformBlock(data2, 0, TestDataSize, data3, 0);
            }
            for (int i = 0; i < TestDataSize; i++)
            {
                Assert.AreEqual(data1[i], data3[i], "Decryption Error");
            }
        }