Exemplo n.º 1
0
        public byte [] Decrypt(byte [] cipher)
        {
            //int iters = cipher.Length / 8;
            int blockSize = _tdcsp.IV.Length;
            int rem       = cipher.Length % 16;

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

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

            //reuse passed in Rijndael
            byte [] origKey = (byte[])_tdcsp.Key.Clone();
            byte [] origIv  = (byte[])_tdcsp.IV.Clone();
            //_tdcsp.Key = (byte[]) _tdcsp.Key.Clone();
            _tdcsp.IV = lastBlock;

            //tempTdcsp.Mode = CipherMode.ECB;
            //TODO make this support other lengths 0x01 - 0x07070707070707
            //byte [] pkcs5 = new byte [] {0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08};

            byte [] pkcs5        = new byte [] { 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 };
            byte [] padPlus      = _tdcsp.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);

            //no reset passed in Rijndael
            _tdcsp.Key = origKey;
            _tdcsp.IV  = origIv;
            byte [] plain = _tdcsp.DecryptValue(paddedCipher);
            return(plain);
        }