| Hosted by CoCalc | Download
# Alice wishes to send Bob the following message x = 1976620216402300889624482718775150 # example taken from http://doctrina.org/How-RSA-Works-With-Examples.html # Bob creates a public key # 1) Bob chooses two large primes p = 12131072439211271897323671531612440428472427633701410925634549312301964373042085619324197365322416866541017057361365214171711713797974299334871062829803541 q = 12027524255478748885956220793734512128733387803682075433653899983955179850988797899869146900809131611153346817050832096022160146366346391812470987105415233 n = p*q phi_n = (p-1)*(q-1) e = 65537 #e = p gcd(phi_n, e) d = inverse_mod(e, phi_n) mod(d*e, phi_n) print "Public key n = " + str(n) print "Public key e = " + str(e) y = power_mod(x, e, n) print "ciphertext y = " + str(y) xx = power_mod(y, d, n) print "comparing messages" xx x
1 1 Public key n = 145906768007583323230186939349070635292401872375357164399581871019873438799005358938369571402670149802121818086292467422828157022922076746906543401224889672472407926969987100581290103199317858753663710862357656510507883714297115637342788911463535102712032765166518411726859837988672111837205085526346618740053 Public key e = 65537 ciphertext y = 35052111338673026690212423937053328511880760811579981620642802346685810623109850235943049080973386241113784040794704193978215378499765413083646438784740952306932534945195080183861574225226218879827232453912820596886440377536082465681750074417459151485407445862511023472235560823053497791518928820272257787786 comparing messages 1976620216402300889624482718775150 1976620216402300889624482718775150
Eve sees the public key n = 145906768007583323230186939349070635292401872375357164399581871019873438799005358938369571402670149802121818086292467422828157022922076746906543401224889672472407926969987100581290103199317858753663710862357656510507883714297115637342788911463535102712032765166518411726859837988672111837205085526346618740053 b = 65537 Excellent choice of b! Alice has chosen a lovely message! ciphertext y = 35052111338673026690212423937053328511880760811579981620642802346685810623109850235943049080973386241113784040794704193978215378499765413083646438784740952306932534945195080183861574225226218879827232453912820596886440377536082465681750074417459151485407445862511023472235560823053497791518928820272257787786 1976620216402300889624482718775150 1976620216402300889624482718775150