/**
  * Static method to convert Key into an instance of RSAPublicKeyImpl or RSAPrivate(Crt)KeyImpl. If
  * the key is not an RSA key or cannot be used, throw an InvalidKeyException.
  *
  * <p>Used by RSASignature and RSACipher.
  */
 public static RSAKey toRSAKey(Key key) throws InvalidKeyException {
   if ((key instanceof RSAPrivateKeyImpl)
       || (key instanceof RSAPrivateCrtKeyImpl)
       || (key instanceof RSAPublicKeyImpl)) {
     return (RSAKey) key;
   } else {
     return (RSAKey) INSTANCE.engineTranslateKey(key);
   }
 }
Beispiel #2
0
 /** Construct a key from its components. Used by the RSAKeyFactory and the RSAKeyPairGenerator. */
 RSAPrivateCrtKeyImpl(
     BigInteger n,
     BigInteger e,
     BigInteger d,
     BigInteger p,
     BigInteger q,
     BigInteger pe,
     BigInteger qe,
     BigInteger coeff)
     throws InvalidKeyException {
   this.n = n;
   this.e = e;
   this.d = d;
   this.p = p;
   this.q = q;
   this.pe = pe;
   this.qe = qe;
   this.coeff = coeff;
   RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
   // generate the encoding
   algid = rsaId;
   try {
     DerOutputStream out = new DerOutputStream();
     out.putInteger(0); // version must be 0
     out.putInteger(n);
     out.putInteger(e);
     out.putInteger(d);
     out.putInteger(p);
     out.putInteger(q);
     out.putInteger(pe);
     out.putInteger(qe);
     out.putInteger(coeff);
     DerValue val = new DerValue(DerValue.tag_Sequence, out.toByteArray());
     key = val.toByteArray();
   } catch (IOException exc) {
     // should never occur
     throw new InvalidKeyException(exc);
   }
 }
Beispiel #3
0
 /** Construct a key from its encoding. Called from newKey above. */
 RSAPrivateCrtKeyImpl(byte[] encoded) throws InvalidKeyException {
   decode(encoded);
   RSAKeyFactory.checkRSAProviderKeyLengths(n.bitLength(), e);
 }