public static KeyPair loadKey(String pemString, String passwd) throws IOException, GeneralSecurityException { Object key = PEMDecoder.decode(pemString.toCharArray(), passwd); if (key instanceof com.trilead.ssh2.signature.RSAPrivateKey) { com.trilead.ssh2.signature.RSAPrivateKey x = (com.trilead.ssh2.signature.RSAPrivateKey)key; return x.toJCEKeyPair(); } if (key instanceof com.trilead.ssh2.signature.DSAPrivateKey) { com.trilead.ssh2.signature.DSAPrivateKey x = (com.trilead.ssh2.signature.DSAPrivateKey)key; KeyFactory kf = KeyFactory.getInstance("DSA"); return new KeyPair( kf.generatePublic(new DSAPublicKeySpec(x.getY(), x.getP(), x.getQ(), x.getG())), kf.generatePrivate(new DSAPrivateKeySpec(x.getX(), x.getP(), x.getQ(), x.getG()))); } throw new UnsupportedOperationException("Unrecognizable key format: " + key); }
public static RSASignature generateSignature(byte[] message, RSAPrivateKey pk) throws IOException { SHA1 md = new SHA1(); md.update(message); byte[] sha_message = new byte[md.getDigestLength()]; md.digest(sha_message); byte[] der_header = new byte[] { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 }; int rsa_block_len = (pk.getN().bitLength() + 7) / 8; int num_pad = rsa_block_len - (2 + der_header.length + sha_message.length) - 1; if (num_pad < 8) throw new IOException("Cannot sign with RSA, message too long"); byte[] sig = new byte[der_header.length + sha_message.length + 2 + num_pad]; sig[0] = 0x01; for (int i = 0; i < num_pad; i++) { sig[i + 1] = (byte) 0xff; } sig[num_pad + 1] = 0x00; System.arraycopy(der_header, 0, sig, 2 + num_pad, der_header.length); System.arraycopy(sha_message, 0, sig, 2 + num_pad + der_header.length, sha_message.length); BigInteger m = new BigInteger(1, sig); BigInteger s = m.modPow(pk.getD(), pk.getN()); return new RSASignature(s); }
/** * Converts this to a JCE API representation of the RSA key pair. * * @return the key pair * @throws GeneralSecurityException the general security exception */ public KeyPair toJCEKeyPair() throws GeneralSecurityException { KeyFactory kf = KeyFactory.getInstance("RSA"); return new KeyPair( kf.generatePublic(new RSAPublicKeySpec(getN(), getE())), kf.generatePrivate(new RSAPrivateKeySpec(getN(), getD()))); } }
/** * Extracts a SSH public key from a PEM-encoded SSH private key. * @param pem The PEM-encoded string (either RSA or DSA). * @param passPhrase The passphrase to decrypt the private key (may be null, if the key is not encrypted). * @return A public key string in the form "<pubkey-type> <pubkey-base64>" * @throws IOException if pem could not be decoded properly. */ public static String extract(final String pem, final String passPhrase) throws IOException { final Object priv = PEMDecoder.decode(pem.toCharArray(), passPhrase); if (priv instanceof RSAPrivateKey) { return "ssh-rsa " + DatatypeConverter.printBase64Binary(RSASHA1Verify.encodeSSHRSAPublicKey(((RSAPrivateKey)priv).getPublicKey())); } if (priv instanceof DSAPrivateKey) { return "ssh-dss " + DatatypeConverter.printBase64Binary(DSASHA1Verify.encodeSSHDSAPublicKey(((DSAPrivateKey)priv).getPublicKey())); } throw new IOException("should never happen"); } }
BigInteger d = dr.readInt(); return new RSAPrivateKey(d, e, n);
@Deprecated public static RSASignature generateSignature(byte[] message, RSAPrivateKey pk) throws IOException { SHA1 md = new SHA1(); md.update(message); byte[] sha_message = new byte[md.getDigestLength()]; md.digest(sha_message); byte[] der_header = new byte[] { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 }; int rsa_block_len = (pk.getN().bitLength() + 7) / 8; int num_pad = rsa_block_len - (2 + der_header.length + sha_message.length) - 1; if (num_pad < 8) throw new IOException("Cannot sign with RSA, message too long"); byte[] sig = new byte[der_header.length + sha_message.length + 2 + num_pad]; sig[0] = 0x01; for (int i = 0; i < num_pad; i++) { sig[i + 1] = (byte) 0xff; } sig[num_pad + 1] = 0x00; System.arraycopy(der_header, 0, sig, 2 + num_pad, der_header.length); System.arraycopy(sha_message, 0, sig, 2 + num_pad + der_header.length, sha_message.length); BigInteger m = new BigInteger(1, sig); BigInteger s = m.modPow(pk.getD(), pk.getN()); return new RSASignature(s); }
byte[] pk_enc = RSASHA1Verify.encodeSSHRSAPublicKey(pk.getPublicKey());
BigInteger d = dr.readInt(); return new RSAPrivateKey(d, e, n);
public static RSASignature generateSignature(byte[] message, RSAPrivateKey pk) throws IOException { SHA1 md = new SHA1(); md.update(message); byte[] sha_message = new byte[md.getDigestLength()]; md.digest(sha_message); byte[] der_header = new byte[] { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 }; int rsa_block_len = (pk.getN().bitLength() + 7) / 8; int num_pad = rsa_block_len - (2 + der_header.length + sha_message.length) - 1; if (num_pad < 8) throw new IOException("Cannot sign with RSA, message too long"); byte[] sig = new byte[der_header.length + sha_message.length + 2 + num_pad]; sig[0] = 0x01; for (int i = 0; i < num_pad; i++) { sig[i + 1] = (byte) 0xff; } sig[num_pad + 1] = 0x00; System.arraycopy(der_header, 0, sig, 2 + num_pad, der_header.length); System.arraycopy(sha_message, 0, sig, 2 + num_pad + der_header.length, sha_message.length); BigInteger m = new BigInteger(1, sig); BigInteger s = m.modPow(pk.getD(), pk.getN()); return new RSASignature(s); }
byte[] pk_enc = RSASHA1Verify.encodeSSHRSAPublicKey(pk.getPublicKey());
BigInteger d = dr.readInt(); return new RSAPrivateKey(d, e, n);