From cc51a667e1e932e7fa4403e62642755fad480452 Mon Sep 17 00:00:00 2001 From: psyas Date: Tue, 12 Jun 2018 14:58:06 +0900 Subject: [PATCH 1/3] modify README.md by psyas for just test --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b394e001..199fe52d4 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ### All algorithms implemented in Python (for education) These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons. - +test ## Sort Algorithms From 602eae756ebc3a1fe1b8fc04f20073ddcb32fee7 Mon Sep 17 00:00:00 2001 From: psyas Date: Tue, 12 Jun 2018 21:18:50 +0900 Subject: [PATCH 2/3] Elgamal cipher key generator code - (initial) --- README.md | 2 +- ciphers/elgamal_key_generator.py | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 ciphers/elgamal_key_generator.py diff --git a/README.md b/README.md index 199fe52d4..9b394e001 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ### All algorithms implemented in Python (for education) These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons. -test + ## Sort Algorithms diff --git a/ciphers/elgamal_key_generator.py b/ciphers/elgamal_key_generator.py new file mode 100644 index 000000000..0524980d6 --- /dev/null +++ b/ciphers/elgamal_key_generator.py @@ -0,0 +1,62 @@ +import os +import random +import sys +import rabin_miller as rabinMiller, cryptomath_module as cryptoMath + +min_primitive_root = 3 + + +def main(): + print('Making key files...') + makeKeyFiles('elgamal', 2048) + print('Key files generation successful') + + +# I have written my code naively same as definition of primitive root +# however every time I run this program, memory exceeded... +# so I used 4.80 Algorithm in Handbook of Applied Cryptography(CRC Press, ISBN : 0-8493-8523-7, October 1996) +# and it seems to run nicely! +def primitiveRoot(p_val): + print("Generating primitive root of p") + while True: + g = random.randrange(3,p_val) + if pow(g, 2, p_val) == 1: + continue + if pow(g, p_val, p_val) == 1: + continue + return g + + +def generateKey(keySize): + print('Generating prime p...') + p = rabinMiller.generateLargePrime(keySize) # select large prime number. + e_1 = primitiveRoot(p) # one primitive root on modulo p. + d = random.randrange(3, p) # private_key -> have to be greater than 2 for safety. + e_2 = cryptoMath.findModInverse(pow(e_1, d, p), p) + + publicKey = (keySize, e_1, e_2, p) + privateKey = (keySize, d) + + return publicKey, privateKey + + +def makeKeyFiles(name, keySize): + if os.path.exists('%s_pubkey.txt' % name) or os.path.exists('%s_privkey.txt' % name): + print('\nWARNING:') + print('"%s_pubkey.txt" or "%s_privkey.txt" already exists. \n' + 'Use a different name or delete these files and re-run this program.' % + (name, name)) + sys.exit() + + publicKey, privateKey = generateKey(keySize) + print('\nWriting public key to file %s_pubkey.txt...' % name) + with open('%s_pubkey.txt' % name, 'w') as fo: + fo.write('%d,%d,%d,%d' % (publicKey[0], publicKey[1], publicKey[2], publicKey[3])) + + print('Writing private key to file %s_privkey.txt...' % name) + with open('%s_privkey.txt' % name, 'w') as fo: + fo.write('%d,%d' % (privateKey[0], privateKey[1])) + + +if __name__ == '__main__': + main() \ No newline at end of file From 0c8707d11ca70fc3ccae5ff2d3cb11cca1573199 Mon Sep 17 00:00:00 2001 From: psyas Date: Tue, 12 Jun 2018 21:21:38 +0900 Subject: [PATCH 3/3] Elgamal cipher key generator code - (initial) --- ciphers/elgamal_key_generator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ciphers/elgamal_key_generator.py b/ciphers/elgamal_key_generator.py index 0524980d6..6a8751f69 100644 --- a/ciphers/elgamal_key_generator.py +++ b/ciphers/elgamal_key_generator.py @@ -59,4 +59,5 @@ def makeKeyFiles(name, keySize): if __name__ == '__main__': - main() \ No newline at end of file + main() + \ No newline at end of file