001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.crypt;
031:
032: import java.security.MessageDigest;
033: import java.security.NoSuchAlgorithmException;
034:
035: import javax.crypto.Cipher;
036: import javax.crypto.NoSuchPaddingException;
037: import javax.crypto.SecretKey;
038: import javax.crypto.spec.IvParameterSpec;
039: import javax.crypto.spec.SecretKeySpec;
040:
041: /**
042: * An {@link ICryptHandler}implementing the AES algorithm.
043: *
044: */
045: public class AESCryptHandler extends StandardCryptHandler {
046: public static final String KEY_ALGORITHM = "AES"; //$NON-NLS-1$
047: public static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; //$NON-NLS-1$
048: public static final String DIGEST_ALGORITHM = "MD5"; //$NON-NLS-1$
049: private int blockSize;
050:
051: protected void updateHash(byte[] encryptionKey, int objectNum,
052: int genNum) {
053: super .updateHash(encryptionKey, objectNum, genNum);
054: md.update(new byte[] { 0x73, 0x41, 0x6c, 0x54 });
055: }
056:
057: public void init(byte[] pCryptKey) throws COSSecurityException {
058: super .init(pCryptKey);
059: try {
060: md = MessageDigest.getInstance(DIGEST_ALGORITHM);
061: cipher = Cipher.getInstance(CIPHER_ALGORITHM);
062: blockSize = cipher.getBlockSize();
063: } catch (NoSuchAlgorithmException e) {
064: throw new COSSecurityException(e);
065: } catch (NoSuchPaddingException e) {
066: throw new COSSecurityException(e);
067: }
068: }
069:
070: synchronized protected byte[] basicDecrypt(byte[] data,
071: byte[] encryptionKey, int objectNum, int genNum)
072: throws COSSecurityException {
073: try {
074: updateHash(encryptionKey, objectNum, genNum);
075: byte[] keyBase = md.digest();
076: IvParameterSpec ivSpec = new IvParameterSpec(data, 0,
077: blockSize);
078: SecretKey skeySpec = new SecretKeySpec(keyBase, 0, length,
079: KEY_ALGORITHM);
080: cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
081: return cipher.doFinal(data, blockSize, data.length
082: - blockSize);
083: } catch (Exception e) {
084: throw new COSSecurityException(e);
085: }
086: }
087:
088: synchronized protected byte[] basicEncrypt(byte[] data,
089: byte[] encryptionKey, int objectNum, int genNum)
090: throws COSSecurityException {
091: try {
092: updateHash(encryptionKey, objectNum, genNum);
093: byte[] keyBase = md.digest();
094: byte[] initVector = cipher.getIV();
095: IvParameterSpec ivSpec = new IvParameterSpec(initVector, 0,
096: initVector.length);
097: SecretKey skeySpec = new SecretKeySpec(keyBase, 0, length,
098: KEY_ALGORITHM);
099: cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
100: byte[] encrypted = cipher.doFinal(data, 0, data.length);
101: byte[] result = new byte[initVector.length
102: + encrypted.length];
103: System.arraycopy(initVector, 0, result, 0,
104: initVector.length);
105: System.arraycopy(encrypted, 0, result, initVector.length,
106: encrypted.length);
107: return result;
108: } catch (Exception e) {
109: throw new COSSecurityException(e);
110: }
111: }
112:
113: }
|