示例#1
0
        /**
         * Do the appropriate processing for CFB mode encryption.
         *
         * @param in the array containing the data to be encrypted.
         * @param inOff offset into the in array the data starts at.
         * @param out the array the encrypted data will be copied into.
         * @param outOff the offset into the out array the output will start at.
         * @exception DataLengthException if there isn't enough data in in, or
         * space in out.
         * @exception IllegalStateException if the cipher isn't initialised.
         * @return the number of bytes processed and produced.
         */
        public int encryptBlock(
            byte[]      inBytes,
            int inOff,
            byte[]      outBytes,
            int outOff)
        //throws DataLengthException, StateException
        {
            if ((inOff + blockSize) > inBytes.Length)
            {
                throw new DataLengthException("input buffer too short");
            }

            if ((outOff + blockSize) > outBytes.Length)
            {
                throw new DataLengthException("output buffer too short");
            }

            cipher.processBlock(cfbV, 0, cfbOutV, 0);

            //
            // XOR the cfbV with the plaintext producing the cipher text
            //
            for (int i = 0; i < blockSize; i++)
            {
                outBytes[outOff + i] = (byte)(cfbOutV[i] ^ inBytes[inOff + i]);
            }

            //
            // change over the input block.
            //
            Array.Copy(cfbV, blockSize, cfbV, 0, cfbV.Length - blockSize);
            Array.Copy(outBytes, outOff, cfbV, cfbV.Length - blockSize, blockSize);

            return(blockSize);
        }
示例#2
0
        /**
         * Do the appropriate chaining step for CBC mode encryption.
         *
         * @param in the array containing the data to be encrypted.
         * @param inOff offset into the in array the data starts at.
         * @param out the array the encrypted data will be copied into.
         * @param outOff the offset into the out array the output will start at.
         * @exception DataLengthException if there isn't enough data in in, or
         * space in out.
         * @exception IllegalStateException if the cipher isn't initialised.
         * @return the number of bytes processed and produced.
         */
        private int encryptBlock(
            byte[]      inBytes,
            int inOff,
            byte[]      outBytes,
            int outOff)
        //throws DataLengthException, IllegalStateException
        {
            if ((inOff + blockSize) > inBytes.Length)
            {
                throw new DataLengthException("input buffer too short");
            }

            /*
             * XOR the cbcV and the input,
             * then encrypt the cbcV
             */
            for (int i = 0; i < blockSize; i++)
            {
                cbcV[i] ^= inBytes[inOff + i];
            }

            int length = cipher.processBlock(cbcV, 0, outBytes, outOff);

            /*
             * copy ciphertext to cbcV
             */
            Array.Copy(outBytes, outOff, cbcV, 0, cbcV.Length);

            return(length);
        }
示例#3
0
        /**
         * encrypt/decrypt a single byte returning the result.
         *
         * @param in the byte to be processed.
         * @return the result of processing the input byte.
         */
        public byte returnByte(
            byte inByte)
        {
            oneByte[0] = inByte;

            cipher.processBlock(oneByte, 0, oneByte, 0);

            return(oneByte[0]);
        }
示例#4
0
        public void update(
            byte inByte)
        {
            int resultLen = 0;

            if (bufOff == buf.Length)
            {
                resultLen = cipher.processBlock(buf, 0, mac, 0);
                bufOff    = 0;
            }

            buf[bufOff++] = inByte;
        }
示例#5
0
        public byte[] wrap(
            byte[]  inBytes,
            int inOff,
            int inLen)
        {
            if (!forWrapping)
            {
                throw new Exception("not set for wrapping");
            }

            int n = inLen / 8;

            if ((n * 8) != inLen)
            {
                throw new DataLengthException("wrap data must be a multiple of 8 bytes");
            }

            byte[] block = new byte[inLen + iv.Length];
            byte[] buf   = new byte[8 + iv.Length];

            Array.Copy(iv, 0, block, 0, iv.Length);
            Array.Copy(inBytes, 0, block, iv.Length, inLen);

            engine.init(true, param);

            for (int j = 0; j != 6; j++)
            {
                for (int i = 1; i <= n; i++)
                {
                    Array.Copy(block, 0, buf, 0, iv.Length);
                    Array.Copy(block, 8 * i, buf, iv.Length, 8);
                    engine.processBlock(buf, 0, buf, 0);

                    int t = n * j + i;
                    for (int k = 1; t != 0; k++)
                    {
                        byte v = (byte)t;

                        buf[iv.Length - k] ^= v;
                        t = (int)((uint)t >> 8);
                    }

                    Array.Copy(buf, 0, block, 0, 8);
                    Array.Copy(buf, 8, block, 8 * i, 8);
                }
            }

            return(block);
        }
示例#6
0
        /**
         * process a single byte, producing an output block if neccessary.
         *
         * @param in the input byte.
         * @param out the space for any output that might be produced.
         * @param outOff the offset from which the output will be copied.
         * @return the number of output bytes copied to out.
         * @exception DataLengthException if there isn't enough space in out.
         * @exception IllegalStateException if the cipher isn't initialised.
         */
        public virtual int processByte(
            byte inByte,
            byte[]      outBytes,
            int outOff)
        //throws DataLengthException, IllegalStateException
        {
            int resultLen = 0;

            buf[bufOff++] = inByte;

            if (bufOff == buf.Length)
            {
                resultLen = cipher.processBlock(buf, 0, outBytes, outOff);
                bufOff    = 0;
            }

            return(resultLen);
        }
示例#7
0
        public int processBlock(byte[] inBytes, int inOff, byte[] outBytes, int outOff)
        //throws DataLengthException, IllegalStateException
        {
            cipher.processBlock(counter, 0, counterOut, 0);

            //
            // XOR the counterOut with the plaintext producing the cipher text
            //
            for (int i = 0; i < counterOut.Length; i++)
            {
                outBytes[outOff + i] = (byte)(counterOut[i] ^ inBytes[inOff + i]);
            }

            BigInteger bi = new BigInteger(counter);

            bi.add(ONE);
            counter = bi.toByteArray();

            return(counter.Length);
        }