Implement Diffie–Hellman key exchange with BPeripherals?

Started by ComputerErika, Jan 09, 2022, 01:07 PM

Previous topic - Next topic

ComputerErika

So BPeripherals has a Cryptography block/peripheral that adds a bunch of well, cryptography stuff
The functions added right now are:
encodeBase64 decodeBase64
encryptAES decryptAES
randomBytes generateRSAKeys
encryptRSA decryptRSA
hashMD5 hashSHA512
hmacMD5 hmacSHA512
Now all we need to use RSA Keys to get shared Symetic keys is a Diffie–Hellman key exchange method
Such a function isn't in the mod (potentially yet) so I wanted to implement it via Lua
I've been searching online for how DHKE works, but I've only found explanations that don't use RSA Keys
Does anyone know how to implement DHKE in lua? Or maybe know of an explanation online for RSA Keys?
Or maybe just help me translate RSA Keys to work with the simpler explanations?
Or perhaps this is just not possible without the mod updating...
Idk
Any help would be appreciated,
Because one day I would love to implement this all on the Lua side of things so I understand it better :3
P.S.
I'm new to CC and the forums, coming from OpenComputers

Lupus590

All I can advise is to look for existing implementations (even if they are in other languages) and try to copy those.

Here's what I've found after a quick search, it includes C, Java, Python, and JavaScript. https://www.geeksforgeeks.org/implementation-diffie-hellman-algorithm/ You'll need to scroll halfway down the page to get to the example implementation.

ComputerErika

#2
Yea I saw the page too, but I don't have a P and G or even just a P to then find a primitive root for,
I have A.Pub and B.Pub and A.Priv and B.Priv. and I haven't found anywhere that describes the relation of them

Lemme think...
Now the function I used in OpenComputers was like
ecdh(a.pub, b.priv) = ecdh(b.pub, a.priv)Now to run only this function, they never exchanged the "Generated keys" like in the example on the website
This leads me to believe the "Generated Keys" are actually the Public Keys, or can be derived from somehow, since those are the keys that are swapped, But the thing about the Public Keys is they never agreed on P or G beforehand
Now G must be the same beforehand, or else they'd have to encrypt their private number again and share, which in the function doesn't do, so I might be able to assume G is the same, and try to find P
Now how do they land on the same number for P? They don't combine their public and the other public to arrive on the same P, because their own public is not used in the equation...

Yea Im still so confused...
I might be being an idiot tho

EDIT:
First of all, I forgot that the public and private key are actually two numbers that share 1 number between them... So yea gonna think more on this

EDIT2:
Nevermind it doesn't work, I'm an idiot

RSA Keygen doesn't work with Diffie–Hellman key exchange
The reason I assumed it did was that in OpenComputer (The 1.12 Computer mod I came from) the Keygen function doesn't specify that it's not RSA, but actually EC, and because of my limited experience in cryptography, I assumed it was RSA.

I forked the 1.16.5 version of the mod and tried adding a Diffie–Hellman key exchange function myself, spent all morning on it, and from what I can tell the only way to have the DHKE is to have another keygen. So yea I was just dumb