blob: 224c3733f3631bfddd1b1880b878355119dae4d4 (
plain)
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
|
package org.spongycastle.openpgp.operator.bc;
import java.security.SecureRandom;
import org.spongycastle.crypto.AsymmetricBlockCipher;
import org.spongycastle.crypto.InvalidCipherTextException;
import org.spongycastle.crypto.params.AsymmetricKeyParameter;
import org.spongycastle.crypto.params.ParametersWithRandom;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.operator.PublicKeyKeyEncryptionMethodGenerator;
/**
* A method generator for supporting public key based encryption operations.
*/
public class BcPublicKeyKeyEncryptionMethodGenerator
extends PublicKeyKeyEncryptionMethodGenerator
{
private SecureRandom random;
private BcPGPKeyConverter keyConverter = new BcPGPKeyConverter();
/**
* Create a public key encryption method generator with the method to be based on the passed in key.
*
* @param key the public key to use for encryption.
*/
public BcPublicKeyKeyEncryptionMethodGenerator(PGPPublicKey key)
{
super(key);
}
/**
* Provide a user defined source of randomness.
*
* @param random the secure random to be used.
* @return the current generator.
*/
public BcPublicKeyKeyEncryptionMethodGenerator setSecureRandom(SecureRandom random)
{
this.random = random;
return this;
}
protected byte[] encryptSessionInfo(PGPPublicKey pubKey, byte[] sessionInfo)
throws PGPException
{
try
{
AsymmetricBlockCipher c = BcImplProvider.createPublicKeyCipher(pubKey.getAlgorithm());
AsymmetricKeyParameter key = keyConverter.getPublicKey(pubKey);
if (random == null)
{
random = new SecureRandom();
}
c.init(true, new ParametersWithRandom(key, random));
return c.processBlock(sessionInfo, 0, sessionInfo.length);
}
catch (InvalidCipherTextException e)
{
throw new PGPException("exception encrypting session info: " + e.getMessage(), e);
}
}
}
|