equal
deleted
inserted
replaced
46 * @author Andreas Sterbenz |
46 * @author Andreas Sterbenz |
47 */ |
47 */ |
48 public final class RSAPublicKeyImpl extends X509Key implements RSAPublicKey { |
48 public final class RSAPublicKeyImpl extends X509Key implements RSAPublicKey { |
49 |
49 |
50 private static final long serialVersionUID = 2644735423591199609L; |
50 private static final long serialVersionUID = 2644735423591199609L; |
|
51 private static final BigInteger THREE = BigInteger.valueOf(3); |
51 |
52 |
52 private BigInteger n; // modulus |
53 private BigInteger n; // modulus |
53 private BigInteger e; // public exponent |
54 private BigInteger e; // public exponent |
54 |
55 |
55 /** |
56 /** |
59 public RSAPublicKeyImpl(BigInteger n, BigInteger e) |
60 public RSAPublicKeyImpl(BigInteger n, BigInteger e) |
60 throws InvalidKeyException { |
61 throws InvalidKeyException { |
61 this.n = n; |
62 this.n = n; |
62 this.e = e; |
63 this.e = e; |
63 RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e); |
64 RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e); |
|
65 checkExponentRange(); |
64 // generate the encoding |
66 // generate the encoding |
65 algid = RSAPrivateCrtKeyImpl.rsaId; |
67 algid = RSAPrivateCrtKeyImpl.rsaId; |
66 try { |
68 try { |
67 DerOutputStream out = new DerOutputStream(); |
69 DerOutputStream out = new DerOutputStream(); |
68 out.putInteger(n); |
70 out.putInteger(n); |
81 * Construct a key from its encoding. Used by RSAKeyFactory. |
83 * Construct a key from its encoding. Used by RSAKeyFactory. |
82 */ |
84 */ |
83 public RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException { |
85 public RSAPublicKeyImpl(byte[] encoded) throws InvalidKeyException { |
84 decode(encoded); |
86 decode(encoded); |
85 RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e); |
87 RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e); |
|
88 checkExponentRange(); |
|
89 } |
|
90 |
|
91 private void checkExponentRange() throws InvalidKeyException { |
|
92 // the exponent should be smaller than the modulus |
|
93 if (e.compareTo(n) >= 0) { |
|
94 throw new InvalidKeyException("exponent is larger than modulus"); |
|
95 } |
|
96 |
|
97 // the exponent should be at least 3 |
|
98 if (e.compareTo(THREE) < 0) { |
|
99 throw new InvalidKeyException("exponent is smaller than 3"); |
|
100 } |
86 } |
101 } |
87 |
102 |
88 // see JCA doc |
103 // see JCA doc |
89 public String getAlgorithm() { |
104 public String getAlgorithm() { |
90 return "RSA"; |
105 return "RSA"; |