1 package org.bouncycastle.jce.provider; 2 3 import org.bouncycastle.asn1.ASN1Sequence; 4 import org.bouncycastle.asn1.DERNull; 5 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; 6 import org.bouncycastle.asn1.x509.AlgorithmIdentifier; 7 import org.bouncycastle.asn1.x509.RSAPublicKeyStructure; 8 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; 9 import org.bouncycastle.crypto.params.RSAKeyParameters; 10 11 import java.io.IOException; 12 import java.math.BigInteger; 13 import java.security.interfaces.RSAPublicKey; 14 import java.security.spec.RSAPublicKeySpec; 15 16 public class JCERSAPublicKey 17 implements RSAPublicKey 18 { 19 static final long serialVersionUID = 2675817738516720772L; 20 21 private BigInteger modulus; 22 private BigInteger publicExponent; 23 JCERSAPublicKey( RSAKeyParameters key)24 JCERSAPublicKey( 25 RSAKeyParameters key) 26 { 27 this.modulus = key.getModulus(); 28 this.publicExponent = key.getExponent(); 29 } 30 JCERSAPublicKey( RSAPublicKeySpec spec)31 JCERSAPublicKey( 32 RSAPublicKeySpec spec) 33 { 34 this.modulus = spec.getModulus(); 35 this.publicExponent = spec.getPublicExponent(); 36 } 37 JCERSAPublicKey( RSAPublicKey key)38 JCERSAPublicKey( 39 RSAPublicKey key) 40 { 41 this.modulus = key.getModulus(); 42 this.publicExponent = key.getPublicExponent(); 43 } 44 JCERSAPublicKey( SubjectPublicKeyInfo info)45 JCERSAPublicKey( 46 SubjectPublicKeyInfo info) 47 { 48 try 49 { 50 RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure((ASN1Sequence)info.getPublicKey()); 51 52 this.modulus = pubKey.getModulus(); 53 this.publicExponent = pubKey.getPublicExponent(); 54 } 55 catch (IOException e) 56 { 57 throw new IllegalArgumentException("invalid info structure in RSA public key"); 58 } 59 } 60 61 /** 62 * return the modulus. 63 * 64 * @return the modulus. 65 */ getModulus()66 public BigInteger getModulus() 67 { 68 return modulus; 69 } 70 71 /** 72 * return the public exponent. 73 * 74 * @return the public exponent. 75 */ getPublicExponent()76 public BigInteger getPublicExponent() 77 { 78 return publicExponent; 79 } 80 getAlgorithm()81 public String getAlgorithm() 82 { 83 return "RSA"; 84 } 85 getFormat()86 public String getFormat() 87 { 88 return "X.509"; 89 } 90 getEncoded()91 public byte[] getEncoded() 92 { 93 // BEGIN android-changed 94 SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKeyStructure(getModulus(), getPublicExponent()).getDERObject()); 95 // END android-changed 96 97 return info.getDEREncoded(); 98 } 99 hashCode()100 public int hashCode() 101 { 102 return this.getModulus().hashCode() ^ this.getPublicExponent().hashCode(); 103 } 104 equals(Object o)105 public boolean equals(Object o) 106 { 107 if (o == this) 108 { 109 return true; 110 } 111 112 if (!(o instanceof RSAPublicKey)) 113 { 114 return false; 115 } 116 117 RSAPublicKey key = (RSAPublicKey)o; 118 119 return getModulus().equals(key.getModulus()) 120 && getPublicExponent().equals(key.getPublicExponent()); 121 } 122 toString()123 public String toString() 124 { 125 StringBuffer buf = new StringBuffer(); 126 String nl = System.getProperty("line.separator"); 127 128 buf.append("RSA Public Key").append(nl); 129 buf.append(" modulus: ").append(this.getModulus().toString(16)).append(nl); 130 buf.append(" public exponent: ").append(this.getPublicExponent().toString(16)).append(nl); 131 132 return buf.toString(); 133 } 134 } 135