JCEAESEncryptionHandler.java
3.74 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityConfigurationException
* com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandler
*/
package com.adobe.internal.pdftoolkit.core.encryption;
import com.adobe.internal.pdftoolkit.core.encryption.JCECipherEncryptionHandler;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityConfigurationException;
import com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandler;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public final class JCEAESEncryptionHandler
extends JCECipherEncryptionHandler
implements EncryptionHandler {
private static final String PRNG = "SHA1PRNG";
private static final byte[] SALT = new byte[]{115, 65, 108, 84};
private Provider mSHA1prngProvider = null;
private SecureRandom mRandomGenerator = null;
public JCEAESEncryptionHandler(byte[] encryptionKey, MessageDigest md5Digest, Provider aesProvider, Provider sha1prngProvider, SecureRandom randomGenerator) throws PDFSecurityConfigurationException {
super("AES/CBC/PKCS5Padding", encryptionKey, md5Digest, aesProvider);
this.mAddition = SALT;
this.mSHA1prngProvider = sha1prngProvider;
this.mRandomGenerator = randomGenerator;
}
protected byte[] initCipher(Cipher cipher, byte[] content, int len, byte[] key, int mode) throws PDFSecurityConfigurationException {
byte[] newContent;
int algLen = "AES/CBC/PKCS5Padding".indexOf(47);
if (algLen == -1) {
algLen = "AES/CBC/PKCS5Padding".length();
}
String keyAlgorithm = "AES/CBC/PKCS5Padding".substring(0, algLen);
IvParameterSpec iv = null;
if (mode == 2) {
iv = new IvParameterSpec(content, 0, 16);
newContent = new byte[len - 16];
System.arraycopy(content, 16, newContent, 0, newContent.length);
} else {
byte[] random = new byte[16];
if (this.mRandomGenerator == null) {
try {
this.mRandomGenerator = this.mSHA1prngProvider == null ? SecureRandom.getInstance("SHA1PRNG") : SecureRandom.getInstance("SHA1PRNG", this.mSHA1prngProvider);
}
catch (NoSuchAlgorithmException e) {
throw new PDFSecurityConfigurationException("Cannot generate random number for AES cipher", (Throwable)e);
}
}
this.mRandomGenerator.nextBytes(random);
iv = new IvParameterSpec(random);
newContent = new byte[len + 16];
System.arraycopy(iv.getIV(), 0, newContent, 0, iv.getIV().length);
System.arraycopy(content, 0, newContent, iv.getIV().length, len);
}
SecretKeySpec mEncryptKey = new SecretKeySpec(key, keyAlgorithm);
try {
cipher.init(mode, (Key)mEncryptKey, iv);
}
catch (InvalidKeyException e) {
throw new PDFSecurityConfigurationException("Error in AES/CBC/PKCS5Padding encryption handler", (Throwable)e);
}
catch (InvalidAlgorithmParameterException e) {
throw new PDFSecurityConfigurationException("Error in AES/CBC/PKCS5Padding encryption handler", (Throwable)e);
}
return newContent;
}
SecureRandom getSHA1PRNG() {
return this.mRandomGenerator;
}
}