BCRSAPrivateKey.java revision e1142c149e244797ce73b0e7fad40816e447a817
1package org.bouncycastle.jcajce.provider.asymmetric.rsa; 2 3import java.io.IOException; 4import java.io.ObjectInputStream; 5import java.io.ObjectOutputStream; 6import java.math.BigInteger; 7import java.security.interfaces.RSAPrivateKey; 8import java.security.spec.RSAPrivateKeySpec; 9import java.util.Enumeration; 10 11import org.bouncycastle.asn1.ASN1Encodable; 12import org.bouncycastle.asn1.ASN1ObjectIdentifier; 13import org.bouncycastle.asn1.DERNull; 14import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; 15import org.bouncycastle.asn1.x509.AlgorithmIdentifier; 16import org.bouncycastle.crypto.params.RSAKeyParameters; 17import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil; 18import org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl; 19import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier; 20 21public class BCRSAPrivateKey 22 implements RSAPrivateKey, PKCS12BagAttributeCarrier 23{ 24 static final long serialVersionUID = 5110188922551353628L; 25 26 private static BigInteger ZERO = BigInteger.valueOf(0); 27 28 protected BigInteger modulus; 29 protected BigInteger privateExponent; 30 31 private transient PKCS12BagAttributeCarrierImpl attrCarrier = new PKCS12BagAttributeCarrierImpl(); 32 33 protected BCRSAPrivateKey() 34 { 35 } 36 37 BCRSAPrivateKey( 38 RSAKeyParameters key) 39 { 40 this.modulus = key.getModulus(); 41 this.privateExponent = key.getExponent(); 42 } 43 44 BCRSAPrivateKey( 45 RSAPrivateKeySpec spec) 46 { 47 this.modulus = spec.getModulus(); 48 this.privateExponent = spec.getPrivateExponent(); 49 } 50 51 BCRSAPrivateKey( 52 RSAPrivateKey key) 53 { 54 this.modulus = key.getModulus(); 55 this.privateExponent = key.getPrivateExponent(); 56 } 57 58 public BigInteger getModulus() 59 { 60 return modulus; 61 } 62 63 public BigInteger getPrivateExponent() 64 { 65 return privateExponent; 66 } 67 68 public String getAlgorithm() 69 { 70 return "RSA"; 71 } 72 73 public String getFormat() 74 { 75 return "PKCS#8"; 76 } 77 78 public byte[] getEncoded() 79 { 80 return KeyUtil.getEncodedPrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new org.bouncycastle.asn1.pkcs.RSAPrivateKey(getModulus(), ZERO, getPrivateExponent(), ZERO, ZERO, ZERO, ZERO, ZERO)); 81 } 82 83 public boolean equals(Object o) 84 { 85 if (!(o instanceof RSAPrivateKey)) 86 { 87 return false; 88 } 89 90 if (o == this) 91 { 92 return true; 93 } 94 95 RSAPrivateKey key = (RSAPrivateKey)o; 96 97 return getModulus().equals(key.getModulus()) 98 && getPrivateExponent().equals(key.getPrivateExponent()); 99 } 100 101 public int hashCode() 102 { 103 return getModulus().hashCode() ^ getPrivateExponent().hashCode(); 104 } 105 106 public void setBagAttribute( 107 ASN1ObjectIdentifier oid, 108 ASN1Encodable attribute) 109 { 110 attrCarrier.setBagAttribute(oid, attribute); 111 } 112 113 public ASN1Encodable getBagAttribute( 114 ASN1ObjectIdentifier oid) 115 { 116 return attrCarrier.getBagAttribute(oid); 117 } 118 119 public Enumeration getBagAttributeKeys() 120 { 121 return attrCarrier.getBagAttributeKeys(); 122 } 123 124 private void readObject( 125 ObjectInputStream in) 126 throws IOException, ClassNotFoundException 127 { 128 in.defaultReadObject(); 129 130 this.attrCarrier = new PKCS12BagAttributeCarrierImpl(); 131 } 132 133 private void writeObject( 134 ObjectOutputStream out) 135 throws IOException 136 { 137 out.defaultWriteObject(); 138 } 139} 140