Given two large primes and a public exponent I'd like to calculate the onion address of a tor service using java. Given the following code:
//a couple of large primes from https://primes.utm.edu/lists/small/small2.html
BigInteger p = new BigInteger("14083359469338511572632447718747493405040362318205860500297736061630222431052998057250747900577940212317413063");
BigInteger q = new BigInteger("76921421106760125285550929240903354966370431827792714920086011488103952094969175731459908117375995349245839343");
BigInteger publicExponent = new BigInteger("65537");
//get the private key
RSAPrivateCrtKeySpec crtKeySpec = new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, p, q, primeExponentP, primeExponentQ, crtCoefficient);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(crtKeySpec);
System.out.println(getPrivateKeyString(privateKey));
//get the public key -> onion address
RSAPublicKeySpec publicSpec = new RSAPublicKeySpec(modulus, publicExponent);
PublicKey publicKey = keyFactory.generatePublic(publicSpec);
System.out.println(getOnionAddress(publicKey));
Using the following method:
public static String getPrivateKeyString(PrivateKey privateKey) {
Base64.Encoder encoder = Base64.getEncoder(); //using import java.util.Base64;
String privateKeyEncoded = encoder.encodeToString(privateKey.getEncoded());
StringBuffer sb = new StringBuffer();
sb.append("-----BEGIN PRIVATE KEY-----\n");
sb.append(privateKeyEncoded);
sb.append("\n-----END PRIVATE KEY-----\n");
String result = sb.toString();
return result;
}
I get the hostname of xvufqn6goj4qh5uc. However when I use the generated private key in a tor service the hostname generated is 6qbn26dres64uon3.onion.
I have previously asked a similar question, but I think my public key is in a different encoding and have tried different massaging to get the expected result but to no avail.
How do I generate the correct onion address in my code?