Exemple #1
0
        private byte[] encryptBlock(
            byte[]  inBytes,
            int inOff,
            int inLen,
            byte[]  z)
        //throws InvalidCipherTextException
        {
            byte[]        C             = null;
            KeyParameter  macKey        = null;
            KDFParameters kParam        = new KDFParameters(z, param.getDerivationV());
            int           c_text_length = 0;
            int           macKeySize    = param.getMacKeySize();

            kdf.init(kParam);

            if (cipher == null)                 // stream mode
            {
                byte[] buf = new byte[inLen + (macKeySize / 8)];

                C             = new byte[inLen + mac.getMacSize()];
                c_text_length = inLen;

                kdf.generateBytes(buf, 0, buf.Length);

                for (int i = 0; i != inLen; i++)
                {
                    C[i] = (byte)(inBytes[inOff + i] ^ buf[i]);
                }

                macKey = new KeyParameter(buf, inLen, (macKeySize / 8));
            }
            else
            {
                int    cipherKeySize = ((IESWithCipherParameters)param).getCipherKeySize();
                byte[] buf           = new byte[(cipherKeySize / 8) + (macKeySize / 8)];

                cipher.init(true, new KeyParameter(buf, 0, (cipherKeySize / 8)));

                c_text_length = cipher.getOutputSize(inLen);

                C = new byte[c_text_length + mac.getMacSize()];

                int off = cipher.processBytes(inBytes, inOff, inLen, C, 0);

                cipher.doFinal(C, off);

                macKey = new KeyParameter(buf, (cipherKeySize / 8), (macKeySize / 8));
            }

            byte[] macIV = param.getEncodingV();

            mac.init(macKey);
            mac.update(C, 0, c_text_length);
            mac.update(macIV, 0, macIV.Length);
            //
            // return the message and it's MAC
            //
            mac.doFinal(C, c_text_length);
            return(C);
        }
Exemple #2
0
        public void init(
            DerivationParameters param)
        {
            if (!(typeof(KDFParameters).IsInstanceOfType(param)))
            {
                throw new ArgumentException("KDF parameters required for KDF2Generator");
            }

            KDFParameters p = (KDFParameters)param;

            shared = p.getSharedSecret();
            iv     = p.getIV();
        }
Exemple #3
0
        private byte[] decryptBlock(
            byte[]  in_enc,
            int inOff,
            int inLen,
            byte[]  z)
        //throws InvalidCipherTextException
        {
            byte[]        M          = null;
            KeyParameter  macKey     = null;
            KDFParameters kParam     = new KDFParameters(z, param.getDerivationV());
            int           macKeySize = param.getMacKeySize();

            kdf.init(kParam);

            inLen -= mac.getMacSize();

            if (cipher == null)                 // stream mode
            {
                byte[] buf = new byte[inLen + (macKeySize / 8)];

                M = new byte[inLen];

                kdf.generateBytes(buf, 0, buf.Length);

                for (int i = 0; i != inLen; i++)
                {
                    M[i] = (byte)(in_enc[inOff + i] ^ buf[i]);
                }

                macKey = new KeyParameter(buf, inLen, (macKeySize / 8));
            }
            else
            {
                int    cipherKeySize = ((IESWithCipherParameters)param).getCipherKeySize();
                byte[] buf           = new byte[(cipherKeySize / 8) + (macKeySize / 8)];

                cipher.init(false, new KeyParameter(buf, 0, (cipherKeySize / 8)));

                byte[] tmp = new byte[cipher.getOutputSize(inLen)];

                int off = cipher.processBytes(in_enc, inOff, inLen, tmp, 0);

                off += cipher.doFinal(tmp, off);

                M = new byte[off];

                Array.Copy(tmp, 0, M, 0, off);

                macKey = new KeyParameter(buf, (cipherKeySize / 8), (macKeySize / 8));
            }

            byte[] macIV = param.getEncodingV();

            mac.init(macKey);
            mac.update(in_enc, inOff, inLen);
            mac.update(macIV, 0, macIV.Length);
            mac.doFinal(macBuf, 0);

            inOff += inLen;

            for (int t = 0; t < macBuf.Length; t++)
            {
                if (macBuf[t] != in_enc[inOff + t])
                {
                    throw (new InvalidCipherTextException("Mac codes failed to equal."));
                }
            }

            return(M);
        }