예제 #1
0
        public virtual byte[] Unwrap(byte[] input, int inOff, int length)
        {
            if (this.forWrapping)
            {
                throw new InvalidOperationException("Not set for unwrapping");
            }
            if (input == null)
            {
                throw new InvalidCipherTextException("Null pointer as ciphertext");
            }
            int blockSize = this.engine.GetBlockSize();

            if (length % blockSize != 0)
            {
                throw new InvalidCipherTextException("Ciphertext not multiple of " + blockSize);
            }
            ParametersWithIV parameters = new ParametersWithIV(this.param, DesEdeWrapEngine.IV2);

            this.engine.Init(false, parameters);
            byte[] array = new byte[length];
            for (int num = 0; num != array.Length; num += blockSize)
            {
                this.engine.ProcessBlock(input, inOff + num, array, num);
            }
            byte[] array2 = DesEdeWrapEngine.reverse(array);
            this.iv = new byte[8];
            byte[] array3 = new byte[array2.Length - 8];
            Array.Copy(array2, 0, this.iv, 0, 8);
            Array.Copy(array2, 8, array3, 0, array2.Length - 8);
            this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
            this.engine.Init(false, this.paramPlusIV);
            byte[] array4 = new byte[array3.Length];
            for (int num2 = 0; num2 != array4.Length; num2 += blockSize)
            {
                this.engine.ProcessBlock(array3, num2, array4, num2);
            }
            byte[] array5 = new byte[array4.Length - 8];
            byte[] array6 = new byte[8];
            Array.Copy(array4, 0, array5, 0, array4.Length - 8);
            Array.Copy(array4, array4.Length - 8, array6, 0, 8);
            if (!this.CheckCmsKeyChecksum(array5, array6))
            {
                throw new InvalidCipherTextException("Checksum inside ciphertext is corrupted");
            }
            return(array5);
        }
예제 #2
0
        public virtual byte[] Wrap(byte[] input, int inOff, int length)
        {
            if (!this.forWrapping)
            {
                throw new InvalidOperationException("Not initialized for wrapping");
            }
            byte[] array = new byte[length];
            Array.Copy(input, inOff, array, 0, length);
            byte[] array2 = this.CalculateCmsKeyChecksum(array);
            byte[] array3 = new byte[array.Length + array2.Length];
            Array.Copy(array, 0, array3, 0, array.Length);
            Array.Copy(array2, 0, array3, array.Length, array2.Length);
            int blockSize = this.engine.GetBlockSize();

            if (array3.Length % blockSize != 0)
            {
                throw new InvalidOperationException("Not multiple of block length");
            }
            this.engine.Init(true, this.paramPlusIV);
            byte[] array4 = new byte[array3.Length];
            for (int num = 0; num != array3.Length; num += blockSize)
            {
                this.engine.ProcessBlock(array3, num, array4, num);
            }
            byte[] array5 = new byte[this.iv.Length + array4.Length];
            Array.Copy(this.iv, 0, array5, 0, this.iv.Length);
            Array.Copy(array4, 0, array5, this.iv.Length, array4.Length);
            byte[]           array6     = DesEdeWrapEngine.reverse(array5);
            ParametersWithIV parameters = new ParametersWithIV(this.param, DesEdeWrapEngine.IV2);

            this.engine.Init(true, parameters);
            for (int num2 = 0; num2 != array6.Length; num2 += blockSize)
            {
                this.engine.ProcessBlock(array6, num2, array6, num2);
            }
            return(array6);
        }