Skip to content

Instantly share code, notes, and snippets.

@shmookey
Created May 2, 2016 04:40
Show Gist options
  • Save shmookey/0acde779151305a4f9e54ccd04bdc95f to your computer and use it in GitHub Desktop.
Save shmookey/0acde779151305a4f9e54ccd04bdc95f to your computer and use it in GitHub Desktop.
rsa experiments in haskell
module Main where
import Math.NumberTheory.Powers
import Math.NumberTheory.Moduli
import Data.Maybe
e = 65537
φ p q = (p - 1) * (q - 1)
d p q = fromJust $ invertMod e (φ p q)
-- Original message "Hello, world!" padded by spaces to 1024 bits
m = 0x48656c6c6f2c20776f726c642120202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020
-- Reference ciphertexts generated by OpenSSL
cK = 0x2c60ab081cc63dd1b7773347f1e448843f4cf37821ef7f5d2e4f2d73b6f2bddd90f9b1a4a5459837c04a5a5ef3e837c754383261fcee5c90a26386ad5bf3719eedf2fd8ec5e0ce498057afc3b94e1b6677833c7a1c6efc916e42fc9606686660effaa917ac83499a478e94e27dc88ec32f44f4c075b4557699d686d9889561d0
cKJ = 0x053fd8a322d3fb04e76a099a6207d3a9ab50842968ff8014a0d7ce7d637fbb6f41af5fddf7c00873b027f653f9b9fa08608a0ddd3c9629e70b611428aa799eb9e1b0ba05a07c20e0b46dd23978e0498d19f2ba7e23f209a1687f690db6aef4c45818553be30b29073c92d4a54c8f186d3e7743ed10b77f8f5fee17e3c76cae64
cJ = 0x242c770e7af821e81e3619899a4ee9e9f865a35f36dbd98e948c43617211e8c9e997096a4226f1e07f2de48868bb4e151811ad51772759e3854f8eda50b6bc8cb393e660fdd35b9f8a18613195a6112e3e9491c9fb1849ba2ae6c4d51914bc5dc5de65bebaa628d47b388be039734739ab34dd6e63fab90ea8f49067789656e1
-- First key: `k`
pK = 0xEF10E8D7E444DD110342612FE8DF018D2FC8C838EF9DBFD7D7BA02DC5352393A10B50A709D8C0EF8F4AA40A3668C109D9EC8DDB855099C133CD7F1D14638267D
qK = 0xE5DCE8CCFC36B15CA0848CE62AF8A3A3544E3EEE6CCD79B10FCBE6C29E439DF93B54CA82EEB2F92110C77AB90748B4A83EF602997498914C1C17D67A94E5FAF1
dK = d pK qK
nK = pK * qK
-- Second key: `j`
pJ = 0xFB10C193BEA5AF3107FE8B5BCACBA0E043E414018921AC117DE93C5E163C2B652E9B07D00234A20C1CD102D66F55AFEEF4934C2EB8A81E4AED587CFD11BDE20F
qJ = 0xEBFFD73F80F920D0B8D4F46F7F78CDCE34430D06B436F04167DF44E471A343A5C764AC0D1F86DF8B58953FA280F7D6B9C299AE6F688D2DE01232834AEC73FE7F
dJ = d pJ qJ
nJ = pJ * qJ
main = do
putStrLn $ "m = " ++ (show m)
putStrLn $ "e = " ++ (show e)
putStrLn $ "cK = " ++ (show cK)
putStrLn $ "cJ = " ++ (show cJ)
putStrLn $ "cKJ = " ++ (show cKJ)
putStrLn $ "cK = enc(m, nK) = m^e (mod nK) = " ++
(show $ powerMod m e nK )
putStrLn $ "m = dec(cK, dK) = cK^dK (mod nK) = " ++
(show $ powerMod cK dK nK )
putStrLn $ "cJ = enc(m, nJ) = m^e (mod nJ) = " ++
(show $ powerMod m e nJ )
putStrLn $ "m = dec(cJ,dJ) = cJ^dJ (mod nJ) = " ++
(show $ powerMod cJ dJ nJ )
putStrLn $ "cKJ = enc(cK, nJ) = cK^e (mod nJ) = (m^e (mod nK))^e (mod nJ) " ++
(show $ powerMod cK e nJ )
putStrLn $ "dec(cKJ,dK) = cKJ^dK (mod nK) = " ++
(show $ powerMod cKJ dK nK )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment