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
|
package org.bouncycastle.jcajce.provider.asymmetric.dsa;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidParameterException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.DSAParameterSpec;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.generators.DSAParametersGenerator;
import org.bouncycastle.crypto.params.DSAParameterGenerationParameters;
import org.bouncycastle.crypto.params.DSAParameters;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class AlgorithmParameterGeneratorSpi
extends java.security.AlgorithmParameterGeneratorSpi
{
protected SecureRandom random;
protected int strength = 1024;
protected DSAParameterGenerationParameters params;
protected void engineInit(
int strength,
SecureRandom random)
{
if (strength < 512 || strength > 3072)
{
throw new InvalidParameterException("strength must be from 512 - 3072");
}
if (strength <= 1024 && strength % 64 != 0)
{
throw new InvalidParameterException("strength must be a multiple of 64 below 1024 bits.");
}
if (strength > 1024 && strength % 1024 != 0)
{
throw new InvalidParameterException("strength must be a multiple of 1024 above 1024 bits.");
}
this.strength = strength;
this.random = random;
}
protected void engineInit(
AlgorithmParameterSpec genParamSpec,
SecureRandom random)
throws InvalidAlgorithmParameterException
{
throw new InvalidAlgorithmParameterException("No supported AlgorithmParameterSpec for DSA parameter generation.");
}
protected AlgorithmParameters engineGenerateParameters()
{
DSAParametersGenerator pGen;
if (strength <= 1024)
{
pGen = new DSAParametersGenerator();
}
else
{
pGen = new DSAParametersGenerator(new SHA256Digest());
}
if (random == null)
{
random = new SecureRandom();
}
if (strength == 1024)
{
params = new DSAParameterGenerationParameters(1024, 160, 80, random);
pGen.init(params);
}
else if (strength > 1024)
{
params = new DSAParameterGenerationParameters(strength, 256, 80, random);
pGen.init(params);
}
else
{
pGen.init(strength, 20, random);
}
DSAParameters p = pGen.generateParameters();
AlgorithmParameters params;
try
{
params = AlgorithmParameters.getInstance("DSA", BouncyCastleProvider.PROVIDER_NAME);
params.init(new DSAParameterSpec(p.getP(), p.getQ(), p.getG()));
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage());
}
return params;
}
}
|