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