public PublicKey getRawPublicKey() throws SshException { try { PublicKey key; String keyAlg = getString(); if (KeyPairProvider.SSH_RSA.equals(keyAlg)) { BigInteger e = getMPInt(); BigInteger n = getMPInt(); KeyFactory keyFactory = SecurityUtils.getKeyFactory("RSA"); key = keyFactory.generatePublic(new RSAPublicKeySpec(n, e)); } else if (KeyPairProvider.SSH_DSS.equals(keyAlg)) { BigInteger p = getMPInt(); BigInteger q = getMPInt(); BigInteger g = getMPInt(); BigInteger y = getMPInt(); KeyFactory keyFactory = SecurityUtils.getKeyFactory("DSA"); key = keyFactory.generatePublic(new DSAPublicKeySpec(y, p, q, g)); } else { throw new IllegalStateException("Unsupported algorithm: " + keyAlg); } return key; } catch (InvalidKeySpecException e) { throw new SshException(e); } catch (NoSuchAlgorithmException e) { throw new SshException(e); } catch (NoSuchProviderException e) { throw new SshException(e); } }
public KeyPair getKeyPair() throws SshException { try { PublicKey pub; PrivateKey prv; String keyAlg = getString(); if (KeyPairProvider.SSH_RSA.equals(keyAlg)) { BigInteger e = getMPInt(); BigInteger n = getMPInt(); BigInteger d = getMPInt(); BigInteger qInv = getMPInt(); BigInteger q = getMPInt(); BigInteger p = getMPInt(); BigInteger dP = d.remainder(p.subtract(BigInteger.valueOf(1))); BigInteger dQ = d.remainder(q.subtract(BigInteger.valueOf(1))); KeyFactory keyFactory = SecurityUtils.getKeyFactory("RSA"); pub = keyFactory.generatePublic(new RSAPublicKeySpec(n, e)); prv = keyFactory.generatePrivate(new RSAPrivateCrtKeySpec(n, e, d, p, q, dP, dQ, qInv)); } else if (KeyPairProvider.SSH_DSS.equals(keyAlg)) { BigInteger p = getMPInt(); BigInteger q = getMPInt(); BigInteger g = getMPInt(); BigInteger y = getMPInt(); BigInteger x = getMPInt(); KeyFactory keyFactory = SecurityUtils.getKeyFactory("DSA"); pub = keyFactory.generatePublic(new DSAPublicKeySpec(y, p, q, g)); prv = keyFactory.generatePrivate(new DSAPrivateKeySpec(x, p, q, g)); } else { throw new IllegalStateException("Unsupported algorithm: " + keyAlg); } return new KeyPair(pub, prv); } catch (InvalidKeySpecException e) { throw new SshException(e); } catch (NoSuchAlgorithmException e) { throw new SshException(e); } catch (NoSuchProviderException e) { throw new SshException(e); } }