In this case we generate an RSA key pair. With this we have two prime numbers (\(p\) and \(q\)), and compute the modulus (\(N=pq\)). We then pick an encryption key value (\(e=0x010001\)) and then compute \(d=e^{-1} \pmod \phi\)), and where \(\phi=(p-1)(q-1)\). To encrypt a message (\(M\)), we create a cipher \(c=M^e \pmod N\), and then decrypt with \(M=c^d \pmod N\). In this case we will prove the computations with the generation of a message value and an RSA key pair.
Using dP, dQ and InvQ with RSA in PowerShell |
Method
In this case we will generate an RSA key pair and encode the key pair into the CBOR representation. With this we have two prime numbers (\(p\) and \(q\)), and compute the modulus (\(N\)):
\(N=pq\)
We then pick an encryption key value (\(e=0x010001\)) and then compute:
\(d=e^{-1} \pmod \phi\))
and where:
\(\phi=(p-1)(q-1)\)
The public key is then \((e,N)\) and the private key is \((d,N)\). To encrypt a message (\(M\)), we create a cipher:
\(c=M^e \pmod N\)
and then decrypt with:
\(M=c^d \pmod N\)
The key pair thus contains \(e, N, d, p\) and \(q\) for a key pair of \((e,N)\) and \((d,N)\).
The coding is:
$M=[System.Numerics.BigInteger]$Args[0] $size=[int]$Args[1] $rsa1=[System.Security.Cryptography.RSA]::Create($size) "`nSize: "+$Args[1] $a=$rsa1.ExportParameters($true) # Set $false for only public parameters $E=[System.Numerics.BigInteger]::Parse("0"+[System.Convert]::ToHexString($a.Exponent),'AllowHexSpecifier') $D=[System.Numerics.BigInteger]::Parse("0"+[System.Convert]::ToHexString($a.D),'AllowHexSpecifier') $N=[System.Numerics.BigInteger]::Parse("0"+[System.Convert]::ToHexString($a.Modulus),'AllowHexSpecifier') $P=[System.Numerics.BigInteger]::Parse("0"+[System.Convert]::ToHexString($a.P),'AllowHexSpecifier') $Q=[System.Numerics.BigInteger]::Parse("0"+[System.Convert]::ToHexString($a.Q),'AllowHexSpecifier') "M: "+$M "E: "+$E "D: "+$D "N: "+$N "P: "+$P "Q: "+$Q "`nChecking first for encryption and decryption with e, d and N" "Message: "+$M $C=[System.Numerics.BigInteger]::ModPow($M,$E,$N) "Cipher: "+$C $Mnew=[System.Numerics.BigInteger]::ModPow($C,$D,$N) "Decipher: "+$Mnew "`nChecking for Modulus = PxQ" $Nnew=[System.Numerics.BigInteger]::Multiply($P,$Q) "N=PxQ: "+$Nnew if ($M=$Mnew) { "`nProven the cipher and decipher process" } if ($N=$Nnew) { "`nProven N=PxQ" }
A sample run for a 1,024-bit RSA key pair:
Size: 1024 M: 12 E: 65537 D: 100163166188260755195371362814604697833287665808789987960940009030772978017628467071720696860562126333370573899978390789355276460594882247531153914432123390126342267175919485556605113629861547151950371979358435477094509821690356359841841901545075455775914957603620884828703974834613483839484886713572675169925 N: 156343473515136711678354085902320910803348030440628514563939442490527274164415577356785655325473600721892664436203677261104073960940452946303599544862411500479390977194093362926290186964646890741665888084434713702016433074394468626114766677958507304392216727076465275009748688350216457775379026050927427473141 P: 11768401340148405707317494584122113372134936284035869968452442197853885046648317722145713334781618912193349674628410631749669074808750771901656231189915323 Q: 13285022238472123568512236445675785805845699486907277828207100543597289832944168664493148318493069202345514522517677813382848715805539997101755607652695567 Checking first for encryption and decryption with e, d and N Message: 12 Cipher: 24663629632431299105000772937090071194754743486874983680502584008040852964716925626658789687749884483257595325810495126499344420430088632855912408439303543795109240448793285606993519287133193463690303851049613482673210120605453604364397838366486232653780326108838056516945309550995362517155455139941534418041 Decipher: 12 Checking for Modulus = PxQ N=PxQ: 156343473515136711678354085902320910803348030440628514563939442490527274164415577356785655325473600721892664436203677261104073960940452946303599544862411500479390977194093362926290186964646890741665888084434713702016433074394468626114766677958507304392216727076465275009748688350216457775379026050927427473141 Proven the cipher and decipher process Proven N=PxQ Recovered: 12