JCECipherEncryptionHandler.java 7.13 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.internal.io.stream.InputByteStream
 *  com.adobe.internal.io.stream.OutputByteStream
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityConfigurationException
 *  com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandler
 *  com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandlerState
 */
package com.adobe.internal.pdftoolkit.core.encryption;

import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.io.stream.OutputByteStream;
import com.adobe.internal.pdftoolkit.core.encryption.JCEEncryptionHandlerState;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityConfigurationException;
import com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandler;
import com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandlerState;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;

public abstract class JCECipherEncryptionHandler
implements EncryptionHandler {
    protected static final int BUF_LEN = 65536;
    protected byte[] mEncryptKey;
    protected String mAlgorithm;
    protected Cipher mCipher;
    protected byte[] mAddition = null;
    protected MessageDigest mDigest;
    protected boolean mInternal = false;

    public JCECipherEncryptionHandler(String algorithm, byte[] encryptionKey, MessageDigest md5Digest, Provider encryptProvider) throws PDFSecurityConfigurationException {
        try {
            this.mCipher = encryptProvider == null ? Cipher.getInstance(algorithm) : Cipher.getInstance(algorithm, encryptProvider);
        }
        catch (NoSuchAlgorithmException e) {
            throw new PDFSecurityConfigurationException(algorithm + " encryption handler not available.", (Throwable)e);
        }
        catch (NoSuchPaddingException e) {
            throw new PDFSecurityConfigurationException(algorithm + " encryption handler not available.", (Throwable)e);
        }
        this.mAlgorithm = algorithm;
        this.mEncryptKey = encryptionKey;
        this.mDigest = md5Digest;
    }

    protected abstract byte[] initCipher(Cipher var1, byte[] var2, int var3, byte[] var4, int var5) throws PDFSecurityConfigurationException;

    protected byte[] perform(byte[] content, byte[] addKey, int mode) throws PDFSecurityConfigurationException {
        byte[] key = this.mInternal ? addKey : this.calculateCompleteKey(addKey);
        byte[] newContent = this.initCipher(this.mCipher, content, content.length, key, mode);
        byte[] encrypted = null;
        try {
            encrypted = this.mCipher.doFinal(newContent);
        }
        catch (IllegalStateException e) {
            throw new PDFSecurityConfigurationException("Error in " + this.mAlgorithm + " encryption handler.", (Throwable)e);
        }
        catch (IllegalBlockSizeException e) {
            throw new PDFSecurityConfigurationException("Error in " + this.mAlgorithm + " encryption handler.", (Throwable)e);
        }
        catch (BadPaddingException e) {
            throw new PDFSecurityConfigurationException("Error in " + this.mAlgorithm + " encryption handler.", (Throwable)e);
        }
        return encrypted;
    }

    Cipher getCipher() {
        return this.mCipher;
    }

    protected byte[] calculateCompleteKey(byte[] additionKey) {
        byte[] baseKey = this.getBaseEncryptionKey();
        if (additionKey == null) {
            return baseKey;
        }
        int baseLen = baseKey == null ? 0 : baseKey.length;
        byte[] key = new byte[baseLen + additionKey.length];
        if (baseLen > 0) {
            System.arraycopy(baseKey, 0, key, 0, baseKey.length);
        }
        System.arraycopy(additionKey, 0, key, baseLen, additionKey.length);
        this.mDigest.update(key);
        if (this.mAddition != null) {
            this.mDigest.update(this.mAddition);
        }
        byte[] encryptedKey = this.mDigest.digest();
        int len = StrictMath.min(baseLen + additionKey.length, 16);
        byte[] finalKey = new byte[len];
        System.arraycopy(encryptedKey, 0, finalKey, 0, len);
        return finalKey;
    }

    public byte[] encrypt(byte[] content, byte[] key) throws PDFSecurityConfigurationException {
        return this.perform(content, key, 1);
    }

    public byte[] decrypt(byte[] content, byte[] key) throws PDFSecurityConfigurationException {
        return this.perform(content, key, 2);
    }

    protected void perform(InputByteStream content, OutputByteStream output, byte[] addKey, int mode) throws IOException, PDFSecurityConfigurationException {
        byte[] buffer = new byte[65536];
        byte[] key = this.mInternal ? addKey : this.calculateCompleteKey(addKey);
        try {
            int len = content.read(buffer);
            byte[] newContent = this.initCipher(this.mCipher, buffer, len, key, mode);
            len = newContent.length;
            do {
                byte[] encrypted;
                if ((encrypted = this.mCipher.update(newContent, 0, len)) != null) {
                    output.write(encrypted);
                }
                if (content.bytesAvailable() <= 0) break;
                len = content.read(buffer);
                newContent = buffer;
            } while (true);
            int finalLen = this.mCipher.doFinal(buffer, 0);
            if (finalLen > 0) {
                output.write(buffer, 0, finalLen);
            }
        }
        catch (IllegalBlockSizeException e) {
            throw new PDFSecurityConfigurationException("Error in " + this.mAlgorithm + " encryption handler.", (Throwable)e);
        }
        catch (ShortBufferException e) {
            throw new PDFSecurityConfigurationException("Error in " + this.mAlgorithm + " encryption handler.", (Throwable)e);
        }
        catch (BadPaddingException e) {
            throw new PDFSecurityConfigurationException("Error in " + this.mAlgorithm + " encryption handler.", (Throwable)e);
        }
    }

    public void encrypt(InputByteStream src, OutputByteStream dest, byte[] key) throws PDFSecurityConfigurationException, PDFIOException {
        try {
            this.perform(src, dest, key, 1);
        }
        catch (IOException e) {
            throw new PDFIOException((Throwable)e);
        }
    }

    public void decrypt(InputByteStream src, OutputByteStream dest, byte[] key) throws PDFSecurityConfigurationException, PDFIOException {
        try {
            this.perform(src, dest, key, 2);
        }
        catch (IOException e) {
            throw new PDFIOException((Throwable)e);
        }
    }

    public byte[] getBaseEncryptionKey() {
        return this.mEncryptKey;
    }

    public EncryptionHandlerState createEncryptionHandlerState() throws PDFSecurityConfigurationException {
        return new JCEEncryptionHandlerState(this);
    }
}