JCECipherEncryptionHandler.java
7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* 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);
}
}