001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.asn1.ASN1Sequence;
004: import org.bouncycastle.asn1.DERNull;
005: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
006: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
007: import org.bouncycastle.asn1.x509.RSAPublicKeyStructure;
008: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
009: import org.bouncycastle.crypto.params.RSAKeyParameters;
010:
011: import java.io.IOException;
012: import java.math.BigInteger;
013: import java.security.interfaces.RSAPublicKey;
014: import java.security.spec.RSAPublicKeySpec;
015:
016: public class JCERSAPublicKey implements RSAPublicKey {
017: static final long serialVersionUID = 2675817738516720772L;
018:
019: private BigInteger modulus;
020: private BigInteger publicExponent;
021:
022: JCERSAPublicKey(RSAKeyParameters key) {
023: this .modulus = key.getModulus();
024: this .publicExponent = key.getExponent();
025: }
026:
027: JCERSAPublicKey(RSAPublicKeySpec spec) {
028: this .modulus = spec.getModulus();
029: this .publicExponent = spec.getPublicExponent();
030: }
031:
032: JCERSAPublicKey(RSAPublicKey key) {
033: this .modulus = key.getModulus();
034: this .publicExponent = key.getPublicExponent();
035: }
036:
037: JCERSAPublicKey(SubjectPublicKeyInfo info) {
038: try {
039: RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure(
040: (ASN1Sequence) info.getPublicKey());
041:
042: this .modulus = pubKey.getModulus();
043: this .publicExponent = pubKey.getPublicExponent();
044: } catch (IOException e) {
045: throw new IllegalArgumentException(
046: "invalid info structure in RSA public key");
047: }
048: }
049:
050: /**
051: * return the modulus.
052: *
053: * @return the modulus.
054: */
055: public BigInteger getModulus() {
056: return modulus;
057: }
058:
059: /**
060: * return the public exponent.
061: *
062: * @return the public exponent.
063: */
064: public BigInteger getPublicExponent() {
065: return publicExponent;
066: }
067:
068: public String getAlgorithm() {
069: return "RSA";
070: }
071:
072: public String getFormat() {
073: return "X.509";
074: }
075:
076: public byte[] getEncoded() {
077: SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
078: new AlgorithmIdentifier(
079: PKCSObjectIdentifiers.rsaEncryption,
080: new DERNull()), new RSAPublicKeyStructure(
081: getModulus(), getPublicExponent())
082: .getDERObject());
083:
084: return info.getDEREncoded();
085: }
086:
087: public int hashCode() {
088: return this .getModulus().hashCode()
089: ^ this .getPublicExponent().hashCode();
090: }
091:
092: public boolean equals(Object o) {
093: if (o == this ) {
094: return true;
095: }
096:
097: if (!(o instanceof RSAPublicKey)) {
098: return false;
099: }
100:
101: RSAPublicKey key = (RSAPublicKey) o;
102:
103: return getModulus().equals(key.getModulus())
104: && getPublicExponent().equals(key.getPublicExponent());
105: }
106:
107: public String toString() {
108: StringBuffer buf = new StringBuffer();
109: String nl = System.getProperty("line.separator");
110:
111: buf.append("RSA Public Key").append(nl);
112: buf.append(" modulus: ").append(
113: this .getModulus().toString(16)).append(nl);
114: buf.append(" public exponent: ").append(
115: this .getPublicExponent().toString(16)).append(nl);
116:
117: return buf.toString();
118: }
119: }
|