Last active
June 21, 2024 19:48
-
-
Save syedrakib/241b68f5aeaefd7ef8e2 to your computer and use it in GitHub Desktop.
Revisions
-
syedrakib revised this gist
Mar 25, 2016 . 1 changed file with 3 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,14 +8,13 @@ def generate_keys(): # RSA modulus length must be a multiple of 256 and >= 1024 modulus_length = 256*4 # use larger value in production privatekey = RSA.generate(modulus_length, Random.new().read) publickey = privatekey.publickey() return privatekey, publickey def encrypt_message(a_message , publickey): encrypted_msg = publickey.encrypt(a_message, 32)[0] encoded_encrypted_msg = base64.b64encode(encrypted_msg) # base64 encoded strings are database friendly return encoded_encrypted_msg def decrypt_message(encoded_encrypted_msg, privatekey): -
syedrakib revised this gist
Mar 25, 2016 . 1 changed file with 10 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,28 +8,29 @@ def generate_keys(): # RSA modulus length must be a multiple of 256 and >= 1024 modulus_length = 256*4 # use larger value in production privatekey = RSA.generate(modulus_length, Random.new().read) # the keypair itself is the private key used for decryption publickey = privatekey.publickey() # the publickey is used for encryption return privatekey, publickey def encrypt_message(a_message , publickey): encrypted_msg = publickey.encrypt(a_message, 32)[0] # base64 encoded strings are database friendly encoded_encrypted_msg = base64.b64encode(encrypted_msg) return encoded_encrypted_msg def decrypt_message(encoded_encrypted_msg, privatekey): decoded_encrypted_msg = base64.b64decode(encoded_encrypted_msg) decoded_decrypted_msg = privatekey.decrypt(decoded_encrypted_msg) return decoded_decrypted_msg ########## BEGIN ########## a_message = "The quick brown fox jumped over the lazy dog" privatekey , publickey = generate_keys() encrypted_msg = encrypt_message(a_message , publickey) decrypted_msg = decrypt_message(encrypted_msg, privatekey) print "%s - (%d)" % (privatekey.exportKey() , len(privatekey.exportKey())) print "%s - (%d)" % (publickey.exportKey() , len(publickey.exportKey())) print " Original content: %s - (%d)" % (a_message, len(a_message)) print "Encrypted message: %s - (%d)" % (encrypted_msg, len(encrypted_msg)) -
syedrakib revised this gist
Mar 21, 2016 . 1 changed file with 1 addition and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,4 @@ # Inspired from http://coding4streetcred.com/blog/post/Asymmetric-Encryption-Revisited-(in-PyCrypto) # PyCrypto docs available at https://www.dlitz.net/software/pycrypto/api/2.6/ from Crypto import Random -
syedrakib revised this gist
Mar 21, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,8 @@ # inspired from # http://coding4streetcred.com/blog/post/Asymmetric-Encryption-Revisited-(in-PyCrypto) # PyCrypto docs available at https://www.dlitz.net/software/pycrypto/api/2.6/ from Crypto import Random from Crypto.PublicKey import RSA import base64 -
syedrakib revised this gist
Mar 21, 2016 . No changes.There are no files selected for viewing
-
syedrakib revised this gist
Mar 21, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,9 @@ # inspired from # http://coding4streetcred.com/blog/post/Asymmetric-Encryption-Revisited-(in-PyCrypto) from Crypto import Random from Crypto.PublicKey import RSA import base64 def generate_keys(): # RSA modulus length must be a multiple of 256 and >= 1024 -
syedrakib created this gist
Mar 21, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ # inspired from # http://coding4streetcred.com/blog/post/Asymmetric-Encryption-Revisited-(in-PyCrypto) import base64, hashlib, json, random, from Crypto import Random from Crypto.PublicKey import RSA def generate_keys(): # RSA modulus length must be a multiple of 256 and >= 1024 modulus_length = 256*4 # use larger value in production keypair = RSA.generate(modulus_length, Random.new().read) publickey = keypair.publickey() return keypair, publickey def encrypt_message(a_message , publickey): encrypted_msg = publickey.encrypt(a_message, 32)[0] encoded_encrypted_msg = base64.b64encode(encrypted_msg) return encoded_encrypted_msg def decrypt_message(encoded_encrypted_msg, keypair): decoded_encrypted_msg = base64.b64decode(encoded_encrypted_msg) decrypted_msg = keypair.decrypt(decoded_encrypted_msg) return decrypted_msg ########## BEGIN ########## a_message = "The quick brown fox jumped over the lazy dog" keypair , publickey = generate_keys() encrypted_msg = encrypt_message(a_message , publickey) decrypted_msg = decrypt_message(encrypted_msg, keypair) print "%s - (%d)" % (keypair.exportKey() , len(keypair.exportKey())) print "%s - (%d)" % (publickey.exportKey() , len(publickey.exportKey())) print " Original content: %s - (%d)" % (a_message, len(a_message)) print "Encrypted message: %s - (%d)" % (encrypted_msg, len(encrypted_msg)) print "Decrypted message: %s - (%d)" % (decrypted_msg, len(decrypted_msg))