2

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?

user728785
  • 532
  • 1
  • 4
  • 18
  • mate,if you think encoding is different, why don't u save them in a file then from ur java code read them from file, this way u ensure both ur code and submitted to tor encoding are the same. – nafas Apr 27 '18 at 11:21
  • That is what I did for the private key. The problem I'm having is with the public key and from that getting the correct onion address. – user728785 Apr 27 '18 at 13:50
  • 1
    Looks correct to me, my guess is you are not using the RSA key you think you are. – President James K. Polk Apr 27 '18 at 21:15
  • @JamesKPolk, so I started up a different machine, re-ran the same code (git pull), scp'd the private key to a different vm running a tor service and got another totally different address! Did a primality check of p and q, which looked ok, but bit length of resulting public key was less than 1024. Chose two new values for p and q which would result in a bit length of 1024 and got the expected onion results. I appreciated the confidence you had in my code. My guess is that I ran into problems due to the bit length of the public key being too short. – user728785 Apr 28 '18 at 07:28

0 Answers0