Пример #1
0
        public byte [] Decrypt(byte [] cipher)
        {
            //int iters = cipher.Length / 8;
            int blockSize = 8;
            int rem       = cipher.Length % blockSize;

            if (rem != 0)
            {
                throw new Exception("must be in 8 byte blocks");
            }

            byte [] lastBlock = new byte[blockSize];
            Array.Copy(cipher, cipher.Length - blockSize, lastBlock, 0, blockSize);

            TripleDESCryptoServiceProvider tempTdcsp = new TripleDESCryptoServiceProvider();

            tempTdcsp.Key = (byte[])_tdcsp.Key.Clone();
            tempTdcsp.IV  = lastBlock;
            //TODO make this support other lengths 0x01 - 0x07070707070707
            byte [] pkcs5        = new byte [] { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 };
            byte [] padPlus      = tempTdcsp.EncryptValue(pkcs5);
            byte [] paddedCipher = new byte [cipher.Length + blockSize];
            Array.Copy(cipher, 0, paddedCipher, 0, cipher.Length);
            Array.Copy(padPlus, 0, paddedCipher, cipher.Length, blockSize);

            byte [] plain = _tdcsp.DecryptValue(paddedCipher);
            return(plain);
        }