# Crypto libary with Commutative Encryption

I want to implement Mental Poker in JavaScript as a toy program and lead in to a larger serious project so I did my research and found Massey-Omura cryptosystem and the Shamir three-pass protocol however I couldn't find any implementations of them, and the information on how to do SRA is scant. Would anyone be able to shed some light on how to implement Commutative Encryption with SRA(RSA) or know of a Massey-Omura/Shamir library?

• This question is a bit broad. Note that JavaScript is not often the preferred language to implement specific cryptographic algorithms in. – Maarten Bodewes Nov 1 '14 at 23:30
• @owlstead An example in how to do it in any framework would be a step in the right direction. I investigated; NaCl, Crypto++ and GNU Crypto and came up with nothing. It wouldn't be the first time that I have studded one well documented library so I could augment another. And option z is I reimplement one of the above ciphers. – xanth Nov 2 '14 at 0:22

So you have two users, $A$ and $B$, who want to play. One user, say $A$, selects $p \cdot q = n$, as per $RSA$, and sends that information to $B$. Both $A$ and $B$ independently choose large primes (or large numbers they confirm are coprime to $\phi(n)$) $e_A$ and $e_B$, and then compute $d_ae_a \equiv 1 \pmod n$ and $d_be_b \equiv 1 \pmod n$. Then, $E_A = m^{e_A} \pmod n$, and so on.
Any crypto library with $RSA$ support should allow for calling the encrypt and decrypt functions with your choice of exponent and modulus. The one downside is needing to generate a reasonably large new public exponent for each hand, but that isn't too bad on a modern system.
• The product of public and private exponent must be 1 modulo $\phi(n)$, not $n$! And if you have a public key for $n$ and also know $\phi(n)$, you can always calculate the private key - the difficult part is calculating $\phi(n)$ when you don't know have the factorization of $n$. The problem with this mental poker variant is that you'd need some trusted third party to generate the key pairs, so no participant knows $\phi(n)$ - which means it wouldn't be mental poker anymore. – Stefan Jul 6 '17 at 11:49