001: package org.bouncycastle.bcpg;
002:
003: import java.io.*;
004: import java.math.BigInteger;
005:
006: /**
007: * base class for an RSA Secret (or Private) Key.
008: */
009: public class RSASecretBCPGKey extends BCPGObject implements BCPGKey {
010: MPInteger d;
011: MPInteger p;
012: MPInteger q;
013: MPInteger u;
014:
015: BigInteger expP, expQ, crt;
016:
017: /**
018: *
019: * @param in
020: * @throws IOException
021: */
022: public RSASecretBCPGKey(BCPGInputStream in) throws IOException {
023: this .d = new MPInteger(in);
024: this .p = new MPInteger(in);
025: this .q = new MPInteger(in);
026: this .u = new MPInteger(in);
027:
028: expP = d.getValue().remainder(
029: p.getValue().subtract(BigInteger.valueOf(1)));
030: expQ = d.getValue().remainder(
031: q.getValue().subtract(BigInteger.valueOf(1)));
032: crt = q.getValue().modInverse(p.getValue());
033: }
034:
035: /**
036: *
037: * @param d
038: * @param p
039: * @param q
040: */
041: public RSASecretBCPGKey(BigInteger d, BigInteger p, BigInteger q) {
042: //
043: // pgp requires (p < q)
044: //
045: int cmp = p.compareTo(q);
046: if (cmp >= 0) {
047: if (cmp == 0) {
048: throw new IllegalArgumentException(
049: "p and q cannot be equal");
050: }
051:
052: BigInteger tmp = p;
053: p = q;
054: q = tmp;
055: }
056:
057: this .d = new MPInteger(d);
058: this .p = new MPInteger(p);
059: this .q = new MPInteger(q);
060: this .u = new MPInteger(p.modInverse(q));
061:
062: expP = d.remainder(p.subtract(BigInteger.valueOf(1)));
063: expQ = d.remainder(q.subtract(BigInteger.valueOf(1)));
064: crt = q.modInverse(p);
065: }
066:
067: /**
068: * return the modulus for this key.
069: *
070: * @return BigInteger
071: */
072: public BigInteger getModulus() {
073: return p.getValue().multiply(q.getValue());
074: }
075:
076: /**
077: * return the private exponent for this key.
078: *
079: * @return BigInteger
080: */
081: public BigInteger getPrivateExponent() {
082: return d.getValue();
083: }
084:
085: /**
086: * return the prime P
087: */
088: public BigInteger getPrimeP() {
089: return p.getValue();
090: }
091:
092: /**
093: * return the prime Q
094: */
095: public BigInteger getPrimeQ() {
096: return q.getValue();
097: }
098:
099: /**
100: * return the prime exponent of p
101: */
102: public BigInteger getPrimeExponentP() {
103: return expP;
104: }
105:
106: /**
107: * return the prime exponent of q
108: */
109: public BigInteger getPrimeExponentQ() {
110: return expQ;
111: }
112:
113: /**
114: * return the crt coefficient
115: */
116: public BigInteger getCrtCoefficient() {
117: return crt;
118: }
119:
120: /**
121: * return "PGP"
122: *
123: * @see org.bouncycastle.bcpg.BCPGKey#getFormat()
124: */
125: public String getFormat() {
126: return "PGP";
127: }
128:
129: /**
130: * return the standard PGP encoding of the key.
131: *
132: * @see org.bouncycastle.bcpg.BCPGKey#getEncoded()
133: */
134: public byte[] getEncoded() {
135: try {
136: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
137: BCPGOutputStream pgpOut = new BCPGOutputStream(bOut);
138:
139: pgpOut.writeObject(this );
140:
141: return bOut.toByteArray();
142: } catch (IOException e) {
143: return null;
144: }
145: }
146:
147: public void encode(BCPGOutputStream out) throws IOException {
148: out.writeObject(d);
149: out.writeObject(p);
150: out.writeObject(q);
151: out.writeObject(u);
152: }
153: }
|