01: package org.bouncycastle.asn1.x509;
02:
03: import java.math.BigInteger;
04: import java.util.Enumeration;
05:
06: import org.bouncycastle.asn1.ASN1Encodable;
07: import org.bouncycastle.asn1.ASN1EncodableVector;
08: import org.bouncycastle.asn1.ASN1Sequence;
09: import org.bouncycastle.asn1.ASN1TaggedObject;
10: import org.bouncycastle.asn1.DERInteger;
11: import org.bouncycastle.asn1.DERObject;
12: import org.bouncycastle.asn1.DERSequence;
13:
14: public class RSAPublicKeyStructure extends ASN1Encodable {
15: private BigInteger modulus;
16: private BigInteger publicExponent;
17:
18: public static RSAPublicKeyStructure getInstance(
19: ASN1TaggedObject obj, boolean explicit) {
20: return getInstance(ASN1Sequence.getInstance(obj, explicit));
21: }
22:
23: public static RSAPublicKeyStructure getInstance(Object obj) {
24: if (obj == null || obj instanceof RSAPublicKeyStructure) {
25: return (RSAPublicKeyStructure) obj;
26: }
27:
28: if (obj instanceof ASN1Sequence) {
29: return new RSAPublicKeyStructure((ASN1Sequence) obj);
30: }
31:
32: throw new IllegalArgumentException(
33: "Invalid RSAPublicKeyStructure: "
34: + obj.getClass().getName());
35: }
36:
37: public RSAPublicKeyStructure(BigInteger modulus,
38: BigInteger publicExponent) {
39: this .modulus = modulus;
40: this .publicExponent = publicExponent;
41: }
42:
43: public RSAPublicKeyStructure(ASN1Sequence seq) {
44: if (seq.size() != 2) {
45: throw new IllegalArgumentException("Bad sequence size: "
46: + seq.size());
47: }
48:
49: Enumeration e = seq.getObjects();
50:
51: modulus = DERInteger.getInstance(e.nextElement())
52: .getPositiveValue();
53: publicExponent = DERInteger.getInstance(e.nextElement())
54: .getPositiveValue();
55: }
56:
57: public BigInteger getModulus() {
58: return modulus;
59: }
60:
61: public BigInteger getPublicExponent() {
62: return publicExponent;
63: }
64:
65: /**
66: * This outputs the key in PKCS1v2 format.
67: * <pre>
68: * RSAPublicKey ::= SEQUENCE {
69: * modulus INTEGER, -- n
70: * publicExponent INTEGER, -- e
71: * }
72: * </pre>
73: * <p>
74: */
75: public DERObject toASN1Object() {
76: ASN1EncodableVector v = new ASN1EncodableVector();
77:
78: v.add(new DERInteger(getModulus()));
79: v.add(new DERInteger(getPublicExponent()));
80:
81: return new DERSequence(v);
82: }
83: }
|