コード例 #1
0
        private static byte[] DoCryptStuff(byte[] data, IKey key, CipherDirection direction, CipherMode cipherMode, byte[] iv)
        {
            byte[] result;
            String transformation = key.GetAlgorithm();

            if (key.GetAlgorithm().StartsWith(ALG_DES))
            {
                transformation += "/" + ModetoString(cipherMode) + "/" + DES_NO_PADDING;
            }

            ICipherParameters keyparam = new KeyParameter(key.GetEncoded());
            IBufferedCipher   cipher   = CipherUtilities.GetCipher(transformation);

            if (cipherMode != CipherMode.ECB)
            {
                keyparam = new ParametersWithIV(keyparam, iv);
            }

            byte[] output = new byte[cipher.GetOutputSize(data.Length)];
            cipher.Init(direction == CipherDirection.ENCRYPT_MODE ? true : false, keyparam);
            result = cipher.DoFinal(data);

            if (cipherMode != CipherMode.ECB)
            {
                Array.Copy(result, result.Length - 8, iv, 0, iv.Length);
            }

            //AlgorithmParameterSpec aps = null;
            //try
            //{
            //    Cipher c1 = Cipher.getInstance(transformation, provider.getName());
            //    if (cipherMode != CipherMode.ECB)
            //        aps = new IvParameterSpec(iv);
            //    c1.init(direction, key, aps);
            //    result = c1.doFinal(data);
            //    if (cipherMode != CipherMode.ECB)
            //        System.arraycopy(result, result.length - 8, iv, 0, iv.length);
            //}
            //catch (Exception e)
            //{
            //    throw e;
            //}
            return(result);
        }
コード例 #2
0
        public byte[] Process(byte[] input, CipherDirection dir)
        {
            if (_pwdHash == null)
            {
                throw new CipherDataNotSetException();
            }

            byte[] result;
            if (dir == CipherDirection.Encryption)
            {
                result = _cryptoService.Encrypt(input, _pwdHash, _ivHash);
            }
            else
            {
                result = _cryptoService.Decrypt(input, _pwdHash, _ivHash);
            }

            return(result);
        }
コード例 #3
0
        public void InitBlockTransform(byte[] password, byte[] iv, CipherDirection direction)
        {
            AesCryptoServiceProvider aesCrypto = new AesCryptoServiceProvider();

            aesCrypto.Mode    = Mode;
            aesCrypto.Padding = Padding;
            ICryptoTransform transform;

            if (direction == CipherDirection.Encryption)
            {
                transform = aesCrypto.CreateEncryptor(password, iv);
            }
            else
            {
                transform = aesCrypto.CreateDecryptor(password, iv);
            }

            _blockTransform = transform;
        }
コード例 #4
0
        public string Process(string inputStr, CipherDirection dir, IEncodingService encoder, IDecodingService decoder)
        {
            if (_pwdHash == null)
            {
                throw new CipherDataNotSetException();
            }

            byte[] input = decoder.Decode(inputStr);

            byte[] result;
            if (dir == CipherDirection.Encryption)
            {
                result = _cryptoService.Encrypt(input, _pwdHash, _ivHash);
            }
            else
            {
                result = _cryptoService.Decrypt(input, _pwdHash, _ivHash);
            }

            return(encoder.Encode(result));
        }
コード例 #5
0
        public void Process(Stream input, Stream output, long inputLength, CipherDirection dir)
        {
            if (_pwdHash == null)
            {
                throw new CipherDataNotSetException();
            }

            IBlockCryptoService svc = (IBlockCryptoService)_cryptoService;

            svc.InitBlockTransform(_pwdHash, _ivHash, dir);

            int blockSize = svc.BlockSize;

            if (svc.SupportsMultiblock)
            {
                blockSize *= MultiblockMultiplier;
            }

            svc.SetBufferSize(blockSize);

            byte[] blockBuffer  = new byte[blockSize];
            int    totalBytes   = 0;
            bool   isFinalBlock = false;

            int prevPercentReport = -1;

            while (totalBytes < inputLength || !isFinalBlock)
            {
                int readBytes = input.Read(blockBuffer, 0, blockSize);

                isFinalBlock = (inputLength - totalBytes) <= blockSize;


                totalBytes += readBytes;

                double progress    = (double)totalBytes / inputLength;
                double percents    = progress * 100;
                int    percentsInt = (int)percents;

                if (percentsInt % 10 == 0 && percentsInt > prevPercentReport)
                {
                    ProgressReporter?.ReportProgress("N/A", progress * 100);
                    prevPercentReport = (int)percents;
                }

                //Logger?.Log("Processed: {0}", (double)totalBytes / inputLength);

                byte[] result;
                int    length;

                if (isFinalBlock)
                {
                    result = svc.ProcessFinalBlock(blockBuffer, readBytes);
                    length = result.Length;
                    //Logger.Log("FinalBlock {0} ---> {1}", Utils.PrintByteArray(blockBuffer, readBytes), Utils.PrintByteArray(result));
                }
                else
                {
                    result = new byte[blockSize];
                    length = svc.ProcessBlock(blockBuffer, result);
                    //Logger.Log("Block {0} ---> {1}", Utils.PrintByteArray(blockBuffer, readBytes), Utils.PrintByteArray(result));
                }

                output.Write(result, 0, length);
            }
        }
コード例 #6
0
 private static byte[] DoCryptStuff(byte[] data, IKey key, CipherDirection direction)
 {
     return(DoCryptStuff(data, key, direction, CipherMode.ECB, null));
 }