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
|
package org.spongycastle.cms.bc;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import org.spongycastle.asn1.ASN1ObjectIdentifier;
import org.spongycastle.asn1.x509.AlgorithmIdentifier;
import org.spongycastle.cms.CMSAlgorithm;
import org.spongycastle.cms.CMSException;
import org.spongycastle.crypto.BufferedBlockCipher;
import org.spongycastle.crypto.CipherKeyGenerator;
import org.spongycastle.crypto.StreamCipher;
import org.spongycastle.crypto.io.CipherOutputStream;
import org.spongycastle.crypto.params.KeyParameter;
import org.spongycastle.operator.GenericKey;
import org.spongycastle.operator.OutputEncryptor;
import org.spongycastle.util.Integers;
public class BcCMSContentEncryptorBuilder
{
private static Map keySizes = new HashMap();
static
{
keySizes.put(CMSAlgorithm.AES128_CBC, Integers.valueOf(128));
keySizes.put(CMSAlgorithm.AES192_CBC, Integers.valueOf(192));
keySizes.put(CMSAlgorithm.AES256_CBC, Integers.valueOf(256));
keySizes.put(CMSAlgorithm.CAMELLIA128_CBC, Integers.valueOf(128));
keySizes.put(CMSAlgorithm.CAMELLIA192_CBC, Integers.valueOf(192));
keySizes.put(CMSAlgorithm.CAMELLIA256_CBC, Integers.valueOf(256));
}
private static int getKeySize(ASN1ObjectIdentifier oid)
{
Integer size = (Integer)keySizes.get(oid);
if (size != null)
{
return size.intValue();
}
return -1;
}
private final ASN1ObjectIdentifier encryptionOID;
private final int keySize;
private EnvelopedDataHelper helper = new EnvelopedDataHelper();
private SecureRandom random;
public BcCMSContentEncryptorBuilder(ASN1ObjectIdentifier encryptionOID)
{
this(encryptionOID, getKeySize(encryptionOID));
}
public BcCMSContentEncryptorBuilder(ASN1ObjectIdentifier encryptionOID, int keySize)
{
this.encryptionOID = encryptionOID;
this.keySize = keySize;
}
public BcCMSContentEncryptorBuilder setSecureRandom(SecureRandom random)
{
this.random = random;
return this;
}
public OutputEncryptor build()
throws CMSException
{
return new CMSOutputEncryptor(encryptionOID, keySize, random);
}
private class CMSOutputEncryptor
implements OutputEncryptor
{
private KeyParameter encKey;
private AlgorithmIdentifier algorithmIdentifier;
private Object cipher;
CMSOutputEncryptor(ASN1ObjectIdentifier encryptionOID, int keySize, SecureRandom random)
throws CMSException
{
if (random == null)
{
random = new SecureRandom();
}
CipherKeyGenerator keyGen = helper.createKeyGenerator(encryptionOID, random);
encKey = new KeyParameter(keyGen.generateKey());
algorithmIdentifier = helper.generateAlgorithmIdentifier(encryptionOID, encKey, random);
cipher = helper.createContentCipher(true, encKey, algorithmIdentifier);
}
public AlgorithmIdentifier getAlgorithmIdentifier()
{
return algorithmIdentifier;
}
public OutputStream getOutputStream(OutputStream dOut)
{
if (cipher instanceof BufferedBlockCipher)
{
return new CipherOutputStream(dOut, (BufferedBlockCipher)cipher);
}
else
{
return new CipherOutputStream(dOut, (StreamCipher)cipher);
}
}
public GenericKey getKey()
{
return new GenericKey(algorithmIdentifier, encKey.getKey());
}
}
}
|