001/*
002 * This file is part of the iText (R) project.
003 * Copyright (c) 1998-2011 1T3XT BVBA
004 * Authors: Bruno Lowagie, Paulo Soares, et al.
005 *
006 * This program is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU Affero General Public License version 3
008 * as published by the Free Software Foundation with the addition of the
009 * following permission added to Section 15 as permitted in Section 7(a):
010 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
011 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
015 * or FITNESS FOR A PARTICULAR PURPOSE.
016 * See the GNU Affero General Public License for more details.
017 * You should have received a copy of the GNU Affero General Public License
018 * along with this program; if not, see http://www.gnu.org/licenses or write to
019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
020 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
021 * http://itextpdf.com/terms-of-use/
022 *
023 * The interactive user interfaces in modified source and object code versions
024 * of this program must display Appropriate Legal Notices, as required under
025 * Section 5 of the GNU Affero General Public License.
026 *
027 * In accordance with Section 7(b) of the GNU Affero General Public License,
028 * a covered work must retain the producer line in every PDF that is created
029 * or manipulated using iText.
030 *
031 * You can be released from the requirements of the license by purchasing
032 * a commercial license. Buying such a license is mandatory as soon as you
033 * develop commercial activities involving the iText software without
034 * disclosing the source code of your own applications.
035 * These activities include: offering paid services to customers as an ASP,
036 * serving PDFs on the fly in a web application, shipping iText with a closed
037 * source product.
038 *
039 * For more information, please contact iText Software Corp. at this
040 * address: sales@itextpdf.com
041 */
042package com.itextpdf.text.pdf.crypto;
043
044import org.bouncycastle.crypto.BlockCipher;
045import org.bouncycastle.crypto.engines.AESFastEngine;
046import org.bouncycastle.crypto.modes.CBCBlockCipher;
047import org.bouncycastle.crypto.params.KeyParameter;
048
049/**
050 * Creates an AES Cipher with CBC and no padding.
051 * @author Paulo Soares
052 */
053public class AESCipherCBCnoPad {
054    private BlockCipher cbc;
055    
056    /** Creates a new instance of AESCipher */
057    public AESCipherCBCnoPad(boolean forEncryption, byte[] key) {
058        BlockCipher aes = new AESFastEngine();
059        cbc = new CBCBlockCipher(aes);
060        KeyParameter kp = new KeyParameter(key);
061        cbc.init(forEncryption, kp);
062    }
063    
064    public byte[] processBlock(byte[] inp, int inpOff, int inpLen) {
065        if ((inpLen % cbc.getBlockSize()) != 0)
066            throw new IllegalArgumentException("Not multiple of block: " + inpLen);
067        byte[] outp = new byte[inpLen];
068        int baseOffset = 0;
069        while (inpLen > 0) {
070            cbc.processBlock(inp, inpOff, outp, baseOffset);
071            inpLen -= cbc.getBlockSize();
072            baseOffset += cbc.getBlockSize();
073            inpOff += cbc.getBlockSize();
074        }
075        return outp;
076    }
077}