Permalink
Browse files

More changes due to creating new unit tests.

  • Loading branch information...
1 parent d95e7da commit 39ce3a575907867af16beffa9dab5f91cc5d014a @asweigart committed Aug 29, 2012
Showing with 168 additions and 54 deletions.
  1. +2 −2 affineBreaker.py
  2. +4 −20 affineCipher.py
  3. +132 −3 codebreaker_unit_tests.py
  4. +21 −0 cryptomath.py
  5. +3 −22 makeRsaKeys.py
  6. +1 −1 nullBreaker.py
  7. +3 −3 primeSieve.py
  8. +1 −1 pyperclip.py
  9. +1 −2 rabinMiller.py
View
@@ -1,7 +1,7 @@
# Affine Cipher Breaker
# http://inventwithpython.com/codebreaker (BSD Licensed)
-import pyperclip, affineCipher, detectEnglish
+import pyperclip, affineCipher, detectEnglish, cryptomath
def main():
# You might want to copy & paste this text from the source code at
@@ -29,7 +29,7 @@ def breakAffine(message):
# brute force by looping through every possible key
for keyA in range(len(affineCipher.LETTERS)):
- if affineCipher.gcd(keyA, len(affineCipher.LETTERS)) != 1:
+ if cryptomath.gcd(keyA, len(affineCipher.LETTERS)) != 1:
continue
for keyB in range(len(affineCipher.LETTERS)):
View
@@ -1,7 +1,7 @@
# Affine Cipher
# http://inventwithpython.com/codebreaker (BSD Licensed)
-import sys, pyperclip
+import sys, pyperclip, cryptomath
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
@@ -24,30 +24,14 @@ def main():
print('Full %sed text copied to clipboard.' % (myMode))
-def gcd(a, b):
- # Return the Greatest Common Divisor of a and b using Euclid's Algorithm
- while a != 0:
- a, b = b % a, a
- return b
-
-
-def findModInverse(a, m):
- # Return the modular inverse of a mod m.
- # Uses the naive bruteforce approach to finding mod inverses.
- for b in range(m):
- if (a * b) % m == 1:
- return b
- return None # no mod inverse exists because a & m aren't relatively prime
-
-
def encryptMessage(keyA, keyB, message):
# key strength and validity checks
if keyA == 1:
sys.exit('The affine cipher becomes incredibly weak when keyA is set to 1. Choose a different key.')
if keyB == 0:
sys.exit('The affine cipher becomes incredibly weak when keyB is set to 0. Choose a different key.')
- if gcd(keyA, len(LETTERS)) != 1:
+ if cryptomath.gcd(keyA, len(LETTERS)) != 1:
sys.exit('The key (%s) and the size of the alphabet (%s) are not relatively prime. Choose a different key.' % (keyA, len(LETTERS)))
ciphertext = ''
@@ -63,11 +47,11 @@ def encryptMessage(keyA, keyB, message):
def decryptMessage(keyA, keyB, message):
- if gcd(keyA, len(LETTERS)) != 1:
+ if cryptomath.gcd(keyA, len(LETTERS)) != 1:
sys.exit('The key (%s) and the size of the alphabet (%s) are not relatively prime. Choose a different key.' % (keyA, len(LETTERS)))
plaintext = ''
- modInverseOfKeyA = findModInverse(keyA, len(LETTERS))
+ modInverseOfKeyA = cryptomath.findModInverse(keyA, len(LETTERS))
for symbol in message:
if symbol in LETTERS:
View
@@ -304,7 +304,6 @@ def test_affineCipherProgram(self):
self.assertEqual(procOut, 'Encrypted text:\nH RZPEDYBO NZDKW WBTBOIB YZ MB RHKKBW VUYBKKVLBUY VG VY RZDKW WBRBVIB H QDPHU VUYZ MBKVBIVUL YQHY VY NHT QDPHU. -HKHU YDOVUL\nFull encrypted text copied to clipboard.\n')
self.assertEqual(pyperclip.paste().decode('ascii'), expectedClipboard)
-
def test_affineCipherModule(self):
import affineCipher
@@ -320,13 +319,17 @@ def test_affineCipherModule(self):
self.assertRaises(SystemExit, affineCipher.encryptMessage, 26, 23, FOX_MESSAGE)
self.assertRaises(SystemExit, affineCipher.encryptMessage, 26, 23, FOX_MESSAGE)
+
+
+
def test_affineBreakerProgram(self):
proc = subprocess.Popen('c:\\python32\\python.exe affineBreaker.py', stdout=subprocess.PIPE, stdin=subprocess.PIPE)
procOut = proc.communicate('D\n'.encode('ascii'))[0].decode('ascii')
+ expectedOutput = 'Breaking...\n(Press Ctrl-C or Ctrl-D to quit at any time.)\nTried KeyA 1, KeyB 0... (H RZPEDYBO NZDKW WBTBOIB YZ MB RHKKBW VU)\nTried KeyA 1, KeyB 1... (G QYODCXAN MYCJV VASANHA XY LA QGJJAV UT)\nTried KeyA 1, KeyB 2... (F PXNCBWZM LXBIU UZRZMGZ WX KZ PFIIZU TS)\nTried KeyA 1, KeyB 3... (E OWMBAVYL KWAHT TYQYLFY VW JY OEHHYT SR)\nTried KeyA 1, KeyB 4... (D NVLAZUXK JVZGS SXPXKEX UV IX NDGGXS RQ)\nTried KeyA 1, KeyB 5... (C MUKZYTWJ IUYFR RWOWJDW TU HW MCFFWR QP)\nTried KeyA 1, KeyB 6... (B LTJYXSVI HTXEQ QVNVICV ST GV LBEEVQ PO)\nTried KeyA 1, KeyB 7... (A KSIXWRUH GSWDP PUMUHBU RS FU KADDUP ON)\nTried KeyA 1, KeyB 8... (Z JRHWVQTG FRVCO OTLTGAT QR ET JZCCTO NM)\nTried KeyA 1, KeyB 9... (Y IQGVUPSF EQUBN NSKSFZS PQ DS IYBBSN ML)\nTried KeyA 1, KeyB 10... (X HPFUTORE DPTAM MRJREYR OP CR HXAARM LK)\nTried KeyA 1, KeyB 11... (W GOETSNQD COSZL LQIQDXQ NO BQ GWZZQL KJ)\nTried KeyA 1, KeyB 12... (V FNDSRMPC BNRYK KPHPCWP MN AP FVYYPK JI)\nTried KeyA 1, KeyB 13... (U EMCRQLOB AMQXJ JOGOBVO LM ZO EUXXOJ IH)\nTried KeyA 1, KeyB 14... (T DLBQPKNA ZLPWI INFNAUN KL YN DTWWNI HG)\nTried KeyA 1, KeyB 15... (S CKAPOJMZ YKOVH HMEMZTM JK XM CSVVMH GF)\nTried KeyA 1, KeyB 16... (R BJZONILY XJNUG GLDLYSL IJ WL BRUULG FE)\nTried KeyA 1, KeyB 17... (Q AIYNMHKX WIMTF FKCKXRK HI VK AQTTKF ED)\nTried KeyA 1, KeyB 18... (P ZHXMLGJW VHLSE EJBJWQJ GH UJ ZPSSJE DC)\nTried KeyA 1, KeyB 19... (O YGWLKFIV UGKRD DIAIVPI FG TI YORRID CB)\nTried KeyA 1, KeyB 20... (N XFVKJEHU TFJQC CHZHUOH EF SH XNQQHC BA)\nTried KeyA 1, KeyB 21... (M WEUJIDGT SEIPB BGYGTNG DE RG WMPPGB AZ)\nTried KeyA 1, KeyB 22... (L VDTIHCFS RDHOA AFXFSMF CD QF VLOOFA ZY)\nTried KeyA 1, KeyB 23... (K UCSHGBER QCGNZ ZEWERLE BC PE UKNNEZ YX)\nTried KeyA 1, KeyB 24... (J TBRGFADQ PBFMY YDVDQKD AB OD TJMMDY XW)\nTried KeyA 1, KeyB 25... (I SAQFEZCP OAELX XCUCPJC ZA NC SILLCX WV)\nTried KeyA 3, KeyB 0... (L XRFKBIJW NRBMQ QJPJWUJ IR EJ XLMMJQ HY)\nTried KeyA 3, KeyB 1... (C OIWBSZAN EISDH HAGANLA ZI VA OCDDAH YP)\nTried KeyA 3, KeyB 2... (T FZNSJQRE VZJUY YRXRECR QZ MR FTUURY PG)\nTried KeyA 3, KeyB 3... (K WQEJAHIV MQALP PIOIVTI HQ DI WKLLIP GX)\nTried KeyA 3, KeyB 4... (B NHVARYZM DHRCG GZFZMKZ YH UZ NBCCZG XO)\nTried KeyA 3, KeyB 5... (S EYMRIPQD UYITX XQWQDBQ PY LQ ESTTQX OF)\nTried KeyA 3, KeyB 6... (J VPDIZGHU LPZKO OHNHUSH GP CH VJKKHO FW)\nTried KeyA 3, KeyB 7... (A MGUZQXYL CGQBF FYEYLJY XG TY MABBYF WN)\nTried KeyA 3, KeyB 8... (R DXLQHOPC TXHSW WPVPCAP OX KP DRSSPW NE)\nTried KeyA 3, KeyB 9... (I UOCHYFGT KOYJN NGMGTRG FO BG UIJJGN EV)\nTried KeyA 3, KeyB 10... (Z LFTYPWXK BFPAE EXDXKIX WF SX LZAAXE VM)\nTried KeyA 3, KeyB 11... (Q CWKPGNOB SWGRV VOUOBZO NW JO CQRROV MD)\nTried KeyA 3, KeyB 12... (H TNBGXEFS JNXIM MFLFSQF EN AF THIIFM DU)\nTried KeyA 3, KeyB 13... (Y KESXOVWJ AEOZD DWCWJHW VE RW KYZZWD UL)\nTried KeyA 3, KeyB 14... (P BVJOFMNA RVFQU UNTNAYN MV IN BPQQNU LC)\nTried KeyA 3, KeyB 15... (G SMAFWDER IMWHL LEKERPE DM ZE SGHHEL CT)\nTried KeyA 3, KeyB 16... (X JDRWNUVI ZDNYC CVBVIGV UD QV JXYYVC TK)\nTried KeyA 3, KeyB 17... (O AUINELMZ QUEPT TMSMZXM LU HM AOPPMT KB)\nTried KeyA 3, KeyB 18... (F RLZEVCDQ HLVGK KDJDQOD CL YD RFGGDK BS)\nTried KeyA 3, KeyB 19... (W ICQVMTUH YCMXB BUAUHFU TC PU IWXXUB SJ)\nTried KeyA 3, KeyB 20... (N ZTHMDKLY PTDOS SLRLYWL KT GL ZNOOLS JA)\nTried KeyA 3, KeyB 21... (E QKYDUBCP GKUFJ JCICPNC BK XC QEFFCJ AR)\nTried KeyA 3, KeyB 22... (V HBPULSTG XBLWA ATZTGET SB OT HVWWTA RI)\nTried KeyA 3, KeyB 23... (M YSGLCJKX OSCNR RKQKXVK JS FK YMNNKR IZ)\nTried KeyA 3, KeyB 24... (D PJXCTABO FJTEI IBHBOMB AJ WB PDEEBI ZQ)\nTried KeyA 3, KeyB 25... (U GAOTKRSF WAKVZ ZSYSFDS RA NS GUVVSZ QH)\nTried KeyA 5, KeyB 0... (R TFDGLKVI NFLCU UVJVIMV KF SV TRCCVU ZE)\nTried KeyA 5, KeyB 1... (W YKILQPAN SKQHZ ZAOANRA PK XA YWHHAZ EJ)\nTried KeyA 5, KeyB 2... (B DPNQVUFS XPVME EFTFSWF UP CF DBMMFE JO)\nTried KeyA 5, KeyB 3... (G IUSVAZKX CUARJ JKYKXBK ZU HK IGRRKJ OT)\nTried KeyA 5, KeyB 4... (L NZXAFEPC HZFWO OPDPCGP EZ MP NLWWPO TY)\nTried KeyA 5, KeyB 5... (Q SECFKJUH MEKBT TUIUHLU JE RU SQBBUT YD)\nTried KeyA 5, KeyB 6... (V XJHKPOZM RJPGY YZNZMQZ OJ WZ XVGGZY DI)\nTried KeyA 5, KeyB 7... (A COMPUTER WOULD DESERVE TO BE CALLED IN)\n\nPossible encryption break:\nKeyA: 5, KeyB: 7\nDecrypted message: A COMPUTER WOULD DESERVE TO BE CALLED INTELLIGENT IF IT COULD DECEIVE A HUMAN INTO BELIEVING THAT IT WAS HUMAN. -ALAN TURING\n\nEnter D for done, or just press Enter to continue breaking:\n> Copying broken ciphertext to clipboard:\nA COMPUTER WOULD DESERVE TO BE CALLED INTELLIGENT IF IT COULD DECEIVE A HUMAN INTO BELIEVING THAT IT WAS HUMAN. -ALAN TURING\n'
expectedClipboard = 'A COMPUTER WOULD DESERVE TO BE CALLED INTELLIGENT IF IT COULD DECEIVE A HUMAN INTO BELIEVING THAT IT WAS HUMAN. -ALAN TURING'
- self.assertEqual(procOut, 'Encrypted text:\nH RZPEDYBO NZDKW WBTBOIB YZ MB RHKKBW VUYBKKVLBUY VG VY RZDKW WBRBVIB H QDPHU VUYZ MBKVBIVUL YQHY VY NHT QDPHU. -HKHU YDOVUL\nFull encrypted text copied to clipboard.\n')
+ self.assertEqual(procOut, expectedOutput)
self.assertEqual(pyperclip.paste().decode('ascii'), expectedClipboard)
@@ -432,13 +435,139 @@ def test_vigenereBreakerProgram(self):
self.assertEqual(procOut, expectedOutput)
+ def test_freqFinderProgram(self):
+ proc = subprocess.Popen('c:\\python32\\python.exe freqFinder.py', stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+ procOut = proc.communicate()[0].decode('ascii')
+
+ expectedOutput = '12\nShakespeare\'s Sonnet #29\nWHEN, IN DISGRACE WITH FORTUNE AND MEN\'S EYES,\nI ALL ALONE BEWEEP MY OUTCAST STATE,\nAND TROUBLE DEAF HEAVEN WITH MY BOOTLESS CRIES,\nAND LOOK UPON MYSELF AND CURSE MY FATE,\nWISHING ME LIKE TO ONE MORE RICH IN HOPE,\nFEATURED LIKE HIM, LIKE HIM WITH FRIENDS POSSESSED,\nDESIRING THIS MAN\'S ART, AND THAT MAN\'S SCOPE,\nWITH WHAT I MOST ENJOY CONTENTED LEAST,\nYET IN THESE THOUGHTS MYSELF ALMOST DESPISING,\nHAPLY I THINK ON THEE, AND THEN MY STATE,\nLIKE TO THE LARK AT BREAK OF DAY ARISING\nFROM SULLEN EARTH, SINGS HYMNS AT HEAVEN\'S GATE\n\nFOR THY SWEET LOVE REMEMBERED SUCH WEALTH BRINGS,\nTHAT THEN I SCORN TO CHANGE MY STATE WITH KINGS.\n\nLetter Frequencies of Sonnet #29:\n{\'A\': 37, \'C\': 10, \'B\': 6, \'E\': 66, \'D\': 16, \'G\': 11, \'F\': 10, \'I\': 35, \'H\': 32, \'K\': 9, \'J\': 1, \'M\': 20, \'L\': 20, \'O\': 28, \'N\': 38, \'Q\': 0, \'P\': 7, \'S\': 42, \'R\': 21, \'U\': 9, \'T\': 49, \'W\': 11, \'V\': 3, \'Y\': 14, \'X\': 0, \'Z\': 0}\n\nFrequency score of Sonnet #29:\n10\n\nFrequency score of Scrambled Sonnet #29:\n10\n\nFrequency score of Lorem Ipsum text:\n4\n\nFrequency score of alphabet:\n0\n\nFrequency score of alphabet x 100:\n0\n\nFrequency score of "AAAAAAAAAAAAAAAH":\n1\n\nFrequency score of "VDIUFRFDSFEWAFDSAFLKHFDSALKFA":\n1\n\n'
+
+ self.assertEqual(procOut, expectedOutput)
+
+
+ def test_primeSieveProgram(self):
+ proc = subprocess.Popen('c:\\python32\\python.exe primeSieve.py', stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+ procOut = proc.communicate()[0].decode('ascii')
+
+ expectedOutput = ' 2 is prime: True\n 5 is prime: True\n 11 is prime: True\n 16 is prime: False\n 17 is prime: True\n101 is prime: True\n126 is prime: False\n147 is prime: False\n\n 2 is prime: True\n 5 is prime: True\n 11 is prime: True\n 16 is prime: False\n 17 is prime: True\n101 is prime: True\n126 is prime: False\n147 is prime: False\n\nTesting if both functions are consistent with each other...\nTest Passed: Both functions are consistent with each other.\n'
+ self.assertEqual(procOut, expectedOutput)
+
+ def test_primeSieveModule(self):
+ import primeSieve
+
+ self.assertTrue(primeSieve.isPrime(2))
+ self.assertTrue(primeSieve.isPrime(17))
+ self.assertTrue(primeSieve.isPrime(37))
+ self.assertFalse(primeSieve.isPrime(20))
+ self.assertFalse(primeSieve.isPrime(1))
+ self.assertFalse(primeSieve.isPrime(0))
+ self.assertFalse(primeSieve.isPrime(-1))
+
+ sieve = primeSieve.primeSieve(1000)
+ self.assertTrue(11 in sieve)
+ self.assertTrue(16 not in sieve)
+ self.assertTrue(17 in sieve)
+ self.assertTrue(147 not in sieve)
+
+
+ def test_rabinMillerProgram(self):
+ proc = subprocess.Popen('c:\\python32\\python.exe rabinMiller.py', stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+ procOut = proc.communicate()[0].decode('ascii')
+
+ expectedOutput = 'Example prime testing:\n2 is prime: True\n3 is prime: True\n5 is prime: True\n10 is prime: False\n100 is prime: False\n101 is prime: True\n5099806053 is prime: False\n5099806057 is prime: True\n'
+ self.assertEqual(procOut, expectedOutput)
+
+ def test_rabinMillerModule(self):
+ import rabinMiller, random
+
+ self.assertTrue(rabinMiller.isPrime(2))
+ self.assertTrue(rabinMiller.isPrime(17))
+ self.assertTrue(rabinMiller.isPrime(37))
+ self.assertFalse(rabinMiller.isPrime(20))
+ self.assertFalse(rabinMiller.isPrime(1))
+ self.assertFalse(rabinMiller.isPrime(0))
+ self.assertFalse(rabinMiller.isPrime(-1))
+ self.assertFalse(rabinMiller.isPrime(5099806053))
+ self.assertTrue(rabinMiller.isPrime(5099806057))
+
+ random.seed(42)
+ for keySize in (32, 64, 128, 256, 512, 600, 1024):
+ prime = rabinMiller.generateLargePrime(keySize)
+ self.assertTrue(rabinMiller.isPrime(prime))
+
+ def test_makeRsaKeysProgram(self):
+ proc = subprocess.Popen('c:\\python32\\python.exe makeRsaKeys.py', stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+ procOut = proc.communicate()[0].decode('ascii')
+
+ self.assertTrue('Key files made.' in procOut)
+
+ def test_makeRsaKeysModule(self):
+ import makeRsaKeys, os
+
+ makeRsaKeys.SILENT_MODE = True
+
+ # erase keys if they exist already
+ for filename in ('unittest_pubkey.txt', 'unittest_privkey.txt'):
+ if os.path.exists(filename):
+ os.unlink(filename)
+
+ makeRsaKeys.makeKeyFiles('unittest')
+ self.assertTrue(os.path.exists('unittest_pubkey.txt'))
+ self.assertTrue(os.path.exists('unittest_privkey.txt'))
+ # Testing the format of the key files can be done with rsaCipher.readKeyFile() later
+
+ # cleanup key files
+ for filename in ('unittest_pubkey.txt', 'unittest_privkey.txt'):
+ os.unlink(filename)
+
+ for keySize in (32, 64, 128, 256, 512, 600, 1024):
+ makeRsaKeys.generateKey(keySize)
+
+ def test_cryptomathModule(self):
+ import cryptomath, random
+ random.seed(42)
+
+ self.assertEqual(cryptomath.gcd(543, 526), 1)
+ self.assertEqual(cryptomath.gcd(184543, 825), 1)
+ self.assertEqual(cryptomath.gcd(184545, 825), 15)
+ self.assertEqual(cryptomath.gcd(30594, 8302), 2)
+
+ # create a bunch of things with expected gcds
+ for i in range(500):
+ a = random.randint(50, 100000)
+ b = random.randint(50, 100000)
+ self.assertEqual(cryptomath.gcd(a, b*a), a)
+
+ self.assertEqual(cryptomath.findModInverse(5, 7), 3)
+ self.assertEqual(cryptomath.findModInverse(5, 18), 11)
+ self.assertEqual(cryptomath.findModInverse(7, 180), 103)
+ self.assertEqual(cryptomath.findModInverse(8, 12), None)
+ self.assertEqual(cryptomath.findModInverse(51, 18), None)
+
+ # confirm that relatively prime a & m values have mod inverse of None
+ for i in range(500):
+ while True:
+ a = random.randint(50, 100000)
+ m = random.randint(10, 50000)
+ if cryptomath.gcd(a, m) != 1:
+ break
+ self.assertEqual(cryptomath.findModInverse(a, m), None)
+ # confirm that non-relatively prime a & m values do have a mod inverse
+ for i in range(500):
+ while True:
+ a = random.randint(50, 100000)
+ m = random.randint(10, 50000)
+ if cryptomath.gcd(a, m) == 1:
+ break
+ self.assertNotEqual(cryptomath.findModInverse(a, m), None)
+
+
if __name__ == '__main__':
TEST_ALL = False
if not TEST_ALL:
customSuite = unittest.TestSuite()
- customSuite.addTest(CodeBreakerUnitTests('test_vigenereBreakerProgram'))
+ customSuite.addTest(CodeBreakerUnitTests('test_cryptomathModule'))
unittest.TextTestRunner().run(customSuite)
elif TEST_ALL:
unittest.main()
View
@@ -0,0 +1,21 @@
+def gcd(a, b):
+ # Return the Greatest Common Divisor of a and b using Euclid's Algorithm
+ while a != 0:
+ a, b = b % a, a
+ return b
+
+
+def findModInverse(a, m):
+ # Returns the modular inverse of a % m, which is
+ # the number x such that a*x % m = 1
+
+ if gcd(a, m) != 1:
+ return None # no mod inverse exists if a & m aren't relatively prime
+
+ # Calculate using the Extended Euclidean Algorithm:
+ u1, u2, u3 = 1, 0, a
+ v1, v2, v3 = 0, 1, m
+ while v3 != 0:
+ q = u3 // v3 # // is the integer division operator
+ v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3
+ return u1 % m
View
@@ -1,7 +1,7 @@
# RSA Key Generator
# http://inventwithpython.com/codebreaker (BSD Licensed)
-import random, sys, os, rabinMiller
+import random, sys, os, rabinMiller, cryptomath
OVERWRITE_MODE = True # CAREFUL! If True, you may overwrite your old keys!
SILENT_MODE = False
@@ -62,14 +62,14 @@ def generateKey(keySize=DEFAULT_KEY_SIZE):
while True:
# Come up with an e that is relatively prime to (p-1)*(q-1)
e = random.randrange(2 ** (keySize - 1), 2 ** (keySize))
- if gcd(e, (p - 1) * (q - 1)) == 1:
+ if cryptomath.gcd(e, (p - 1) * (q - 1)) == 1:
break
n = p * q
# Step 3: Get the mod inverse of e.
if not SILENT_MODE:
print('Calculating d that is mod inverse of e...')
- d = getModInverse(e, (p - 1) * (q - 1))
+ d = cryptomath.findModInverse(e, (p - 1) * (q - 1))
publicKey = (n, e)
privateKey = (n, d)
@@ -83,25 +83,6 @@ def generateKey(keySize=DEFAULT_KEY_SIZE):
return (publicKey, privateKey)
-def gcd(a, b):
- # Return the Greatest Common Divisor of a and b.
- while a != 0:
- a, b = b % a, a
- return b
-
-
-def getModInverse(a, m):
- # Returns the modular inverse of a % m, which is
- # the number x such that a*x % m = 1
-
- # Calculate using the Extended Euclidean Algorithm:
- u1, u2, u3 = 1, 0, a
- v1, v2, v3 = 0, 1, m
- while v3 != 0:
- q = u3 // v3 # // is the integer division operator
- v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3
- return u1 % m
-
# If makeRsaKeys.py is run (instead of imported as a module) call
# the main() function.
View
@@ -1,7 +1,7 @@
# Null Cipher Breaker
# http://inventwithpython.com/codebreaker (BSD Licensed)
-import nullCipher, pyperclip, detectEnglish, itertools, sys
+import nullCipher, pyperclip, detectEnglish, itertools
# There are two settings our breaking program needs to limit the range of the possible keys it checks.
# MAX_KEY_NUMBER is the range of numbers it checks for each number in the key. A MAX_KEY_NUMBER value of 9 means it will check the numbers 0 through 9.
View
@@ -23,7 +23,7 @@ def main():
print('126 is prime: %s' % (126 in primes))
print('147 is prime: %s' % (147 in primes))
print()
- testPrimeFunctions()
+ _testPrimeFunctions()
def isPrime(num):
@@ -66,8 +66,8 @@ def primeSieve(sieveSize):
return primes
-def testPrimeFunctions():
- TEST_SIZE = 50000
+def _testPrimeFunctions():
+ TEST_SIZE = 20000
sievePrimes = primeSieve(TEST_SIZE)
print('Testing if both functions are consistent with each other...')
View
@@ -43,7 +43,7 @@
# 1.2 Use the platform module to help determine OS.
# 1.3 Changed ctypes.windll.user32.OpenClipboard(None) to ctypes.windll.user32.OpenClipboard(0), after some people ran into some TypeError
-import platform, os, sys
+import platform, os
def winGetClipboard():
ctypes.windll.user32.OpenClipboard(0)
Oops, something went wrong.

0 comments on commit 39ce3a5

Please sign in to comment.