Last active
December 16, 2015 00:58
-
-
Save GabLeRoux/5350990 to your computer and use it in GitHub Desktop.
RSA Encryption in python
Only keys being generated atm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Finds the greatest comon divisor of a and b | |
def Euclide(a, b): | |
while a: | |
a, b = b%a, a | |
return b | |
# Beside finding the greatest comon divisor of a and b, | |
# it also finds integer x and y that satisfy Bésout's identity | |
def ExtendedEuclide(a, b): | |
a1 = 1 | |
a2 = 0 | |
a3 = a | |
b1 = 0 | |
b2 = 1 | |
b3 = b | |
while b3 != 0: | |
quotient = a3 / b3 | |
temp1 = a1 - quotient * b1 | |
temp2 = a2 - quotient * b2 | |
temp3 = a3 - quotient * b3 | |
a1 = b1 | |
a2 = b2 | |
a3 = b3 | |
b1 = temp1 | |
b2 = temp2 | |
b3 = temp3 | |
return a1, a2, a3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) 2013 the authors listed at the following URL, and/or | |
# the authors of referenced articles or incorporated external code: | |
# http://en.literateprograms.org/Miller-Rabin_primality_test_(Python)?action=history&offset=20110413052045 | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the | |
# "Software"), to deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so, subject to | |
# the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be | |
# included in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# | |
# Retrieved from: http://en.literateprograms.org/Miller-Rabin_primality_test_(Python)?oldid=17104 | |
# Edited by GabLeRoux, used Crypto.Random for a better Random and defined a main function | |
import sys | |
from Crypto.Random import random | |
def miller_rabin_pass(a, s, d, n): | |
a_to_power = pow(a, d, n) | |
if a_to_power == 1: | |
return True | |
for i in xrange(s-1): | |
if a_to_power == n - 1: | |
return True | |
a_to_power = (a_to_power * a_to_power) % n | |
return a_to_power == n - 1 | |
def miller_rabin(n): | |
d = n - 1 | |
s = 0 | |
while d % 2 == 0: | |
d >>= 1 | |
s += 1 | |
for repeat in xrange(20): | |
a = 0 | |
while a == 0: | |
a = random.randrange(n) | |
if not miller_rabin_pass(a, s, d, n): | |
return False | |
return True | |
def main(arg1, arg2): | |
if arg1 == "test": | |
n = long(arg2) | |
print (miller_rabin(n) and "PRIME" or "COMPOSITE") | |
elif arg1 == "genprime": | |
nbits = int(arg2) | |
while True: | |
p = random.getrandbits(nbits) | |
p |= 2**nbits | 1 | |
if miller_rabin(p): | |
return p | |
if __name__ == "__main__": | |
main(sys.argv[1], sys.argv[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: RSA Key Generation :: | |
-- Step 1 -- | |
p 5812384961 | |
q 6174582059 | |
-- Step 2 -- | |
n 35889047900192014699 | |
-- Step 3 -- | |
phyN 35889047888205047680 | |
e 2360553337086107521 | |
-- Step 4 -- | |
Public key: (2360553337086107521L, 35889047900192014699L) | |
Secret key: (0, 35889047900192014699L) | |
[Finished in 0.1s] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import MillerRabin | |
import Euclide | |
from Crypto.Random import random | |
import base64 | |
import os | |
import sys | |
def GenerateKeys(nbBits, verbose=False): | |
if nbBits < 2: | |
sys.exit("Need more bits") | |
if verbose: print ":: RSA Key Generation ::" | |
# Step 1 | |
if verbose: print "-- Step 1 --" | |
while True: | |
p = MillerRabin.main("genprime", nbBits) | |
q = MillerRabin.main("genprime", nbBits) | |
if p != q: | |
break; | |
if verbose: print "p ", p | |
if verbose: print "q ", q | |
# p, q = 47, 71 | |
# Step 2 | |
if verbose: print "\n-- Step 2 --" | |
n = p * q | |
if verbose: print "n ", n | |
# Step 3 | |
if verbose: print "\n-- Step 3 --" | |
phyN = (p-1) * (q-1) | |
if verbose: print "phyN ", phyN | |
while True: | |
# We choose a random e < phyN | |
e = random.randrange(0, phyN-1) | |
if e % 2 == 0: | |
e += 1 | |
if Euclide.Euclide(e, phyN) == 1: | |
break | |
if verbose: print "e", e | |
# Step 4 | |
if verbose: print "\n-- Step 4 --" | |
d, a, b = Euclide.ExtendedEuclide(e, phyN) | |
# Key pairs (P: Public key, S: Secret key) | |
P = e, n | |
S = d, n | |
if verbose: print "Public key:", P | |
if verbose: print "Secret key:", S | |
return P, S | |
# The RSA Encryption Algorithm | |
def RSA(M, key): | |
pass | |
M = "Bonjour, encrypte-moi" | |
P, S = GenerateKeys(32, True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment