public SshKeyPair toKeyPair(String passphrase) throws IOException,
			InvalidPassphraseException {

		byte[] keyblob = getKeyBlob(formattedkey);
		boolean wasEncrypted = false;
		ByteArrayReader bar = new ByteArrayReader(keyblob);

		try {
			long magic = bar.readInt();

			if (magic != 0x3f6ff9eb)
				throw new IOException(
						"Invalid ssh.com key! Magic number not found");

			bar.readInt();
			String type = bar.readString();
			String cipher = bar.readString();
			byte[] blob = bar.readBinaryString();

		
			if (!cipher.equals("none")) {
				if (!cipher.equals("3des-cbc")) {
					throw new IOException("Unsupported cipher type "
							+ cipher + " in ssh.com private key");
				}

				SshCipher c = new TripleDesCbc();

				byte[] iv = new byte[32];
				byte[] keydata = makePassphraseKey(passphrase);

				c.init(SshCipher.DECRYPT_MODE, iv, keydata);

				c.transform(blob);
				wasEncrypted = true;

			}


			ByteArrayReader data = new ByteArrayReader(blob, 4, blob.length - 4);

			try {

				if (type.startsWith("if-modn{sign{rsa")) {

					BigInteger e = data.readMPINT32();
					BigInteger d = data.readMPINT32();
					BigInteger n = data.readMPINT32();
					BigInteger u = data.readMPINT32();
					BigInteger p = data.readMPINT32();
					BigInteger q = data.readMPINT32();

					SshKeyPair pair = new SshKeyPair();

					pair.setPublicKey(ComponentManager.getInstance()
							.createRsaPublicKey(n, e));
					pair.setPrivateKey(ComponentManager.getInstance()
							.createRsaPrivateCrtKey(n, e, d, p, q, u));

					return pair;

				} else if (type.startsWith("dl-modp{sign{dsa")) {

					long predefined = data.readInt();

					if (predefined != 0)
						throw new IOException(
								"Unexpected value in DSA key; this is an unsupported feature of ssh.com private keys");

					BigInteger p = data.readMPINT32();
					BigInteger g = data.readMPINT32();
					BigInteger q = data.readMPINT32();
					BigInteger y = data.readMPINT32();
					BigInteger x = data.readMPINT32();

					SshKeyPair pair = new SshKeyPair();

					SshDsaPublicKey pub = ComponentManager.getInstance()
							.createDsaPublicKey(p, q, g, y);
					pair.setPublicKey(pub);
					pair.setPrivateKey(ComponentManager.getInstance()
							.createDsaPrivateKey(p, q, g, x, pub.getY()));

					return pair;
				} else
					throw new IOException("Unsupported ssh.com key type "
							+ type);
			} catch (Throwable t) {
				if (wasEncrypted)
					throw new InvalidPassphraseException();
				throw new IOException("Bad SSH.com private key format!");
			} finally {
				data.close();
			}
		} finally {
			bar.close();
		}
	}