I am trying to compute some values for a Diffie-Hellman key exchange. I understand the concepts, but the data that I'm working with makes me question the method.
I have been given 5 values to work with p and g:
// p and g
BigInteger p = new BigInteger(DH_PRIME, 16);
BigInteger g = new BigInteger(DH_GEN, 16);
// My Private Key
BigInteger a = new BigInteger(
"5E8B02F2B2E9C96E0C359ECD14EB1B29EBDD61E70A61E4" +
"2F0836A5974963E96D91F1462B699C222BC92BC068E9DC" +
"E5C78E4349D28DDCB6D0ED2C41F7CD8AF2418C8AE27B69" +
"09484DED7F0C5B4C286D9C36518FA5953974741B3A6F75" +
"7B59A41A5CA0B74EFD919BB7ED8CCEC9CB3BC4B4F8D15D" +
"16DC4642E54691904B2F35B969", 16);
// My Public Key
BigInteger A = new BigInteger(
"85F04DD00345642AD12B65BD1A7C38728BFF0B8E281DDB" +
"6AC4F2739E82A02145DAABF23D173C933913B1F8440597" +
"10E9125591569DE427EAE1D269ACCBFA3305069DEB7622" +
"D1DA3AD9820D11BD24FDCCE5381D2DF99BDA314394738D" +
"FCBE210EAE247B1303E79297FF746CD919E189F6A5776E" +
"6ECC24C8900DE0F38F159072DE", 16);
// Their Private Key
BigInteger b = new BigInteger(
"42111D3A7ECAA6A83E503825F38629AD9754D93370D681" +
"AEFEE152329D8DAE6C20732C5A7B6393DEDDB62753CEEF" +
"AE0A5E1BD037A5A32364CE1375442E58997C2918563EE5" +
"D7452373847AABAD5A5D02DF289B3A0B9096A375AE509F" +
"16363B4573A5CCCDFFF2B60459D52C0E5280853000CE62" +
"68560A95111723AF5916CC8376", 16);
// Their Public Key
BigInteger B = new BigInteger(
"71257BA7758CDE21480706CA55861F5FE6122E5B879420" +
"80F3E384890284FD62341B90A1B60FB44ADD61031D6AAC" +
"3D5B267F1435B0765AC289040B63B242EED82863FD18BB" +
"637757EDF44BA4589E0CE99D192E902C16EF1A89E7E7C1" +
"C2EB5A6A8AB3E3E4F6B8A9CACCA4B8F6C4E20D12626797" +
"5406CF9151D57BEEAE32C33CD8", 16);
// The Shared Secret
BigInteger secret = new BigInteger(
"834A9D0434D817735589F22A4633FB6DD3E530DBA1EA2B" +
"BB9E1ACCB438084513087F5DA00EA86ED53164D8893B81" +
"A9C8DF65BC189CF6830D271E1A3E504CBCB25714164B51" +
"9C75F6E0ADF41BB07E7F8C4FB7B9960D813E6577A73252" +
"EEA9C139CDDA606D51122170E71636E7849149618C8238" +
"A226128821F0B668490BDCA82F", 16);
Using this post as a guide: https://security.stackexchange.com/questions/45963/diffie-hellman-key-exchange-in-plain-english
1) Are my assumptions correct that 'a' is the secret number on my side and 'b' is the secret number on their side?
2) Given the example data, am I correct in transforming the hex representation to a decimal value? Here are my results:
a = 66390362007035946406218401401207761013475548285294105112105139639190607639308478292266684826306137190981682856073453566963077570668348041252633322979431955395580457467551843233313476557017872071709128383858840166213588753166690401421078658676432871207970713843501321433541674906101053126631545366776741476713
A = 94054944803568781365809132293092437685411466113799940774655767034465070801688227674438324625217997304495304798217968020105674208988107083532555808216365925310845077730469696561179874023824545031639895643922574243862372138887930362438243722132412662233732339999845565810626073575967836399703588412025249755870
b = 46393721373616931425713842738733091490641584087681350012011014830147740618840619997608221051635856275110730538390991643443828273505863608898989702862901548035408503167446323013096534700981785904886213090032981937144110592283933751272337196723467356342510104910060731712678136878986285012929934142772165247862
B = 79454116791030243835993774846060329772022864579356590515844871048029546733381452221063689783318067017491406528317462838099472851672248798338610594948647929971365872809557181659701202351270701590108182485073251154126367917793952098022309258299793944660722596621675214128052755659890352643244396810687568100568
secret = 92195997420654412005403859326763427963568159942225029128672319592580542441945382083896651021886036463236361034857638584808142466991971457292885469363097913358292550515467126779618590503912963769256823553075620480922531451229861911226149279841977690852350735509236250226638964983746622786925283953012622796847
3) Do these values make sense when it comes to computing the result? I am having a hard time getting this function to work with such large numbers:
A = g^a mod p
Given that 'a' is some 308 digits long, it seems too large to make any sense. I cannot set such a large number as the exponent on the BigInteger as it will only allow me to pass an int value, where my 'a' is most definitely out of range.
You are correct on your first two questions.
For the 3rd one, use the modPow(BigInteger exponent, BigInteger modulo)
, so
BigInteger A = g.modPow(a,p);