Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook Block Ciphers - Jupyter.ipynb

233 views
Kernel: Python 2
# Example of block cipher usage with the pycrypto library. Ignore the warning about libgmp from sage import * from Crypto.Cipher import AES from Crypto import Random import binascii def createKey(string): string = str(string) if len(string) > 16: return string[0:16] else: return string + '0'*(16 - len(string)) key = createKey("password") IV = '0123456789abcdef' AEScipher = AES.new(key, AES.MODE_CFB, IV) #Credit for this universal truth goes to Neal Stephenson - Cryptonomicon message = """This made him a grad student, and grad students existed not to learn things but to relieve the tenured faculty members of tiresome burdens such as educating people and doing research.""" c = IV + AEScipher.encrypt(message) print "Original message: ", message,"\n" print "Original bytes: ",binascii.hexlify(message),"\n" print "Ciphertext bytes: ",binascii.hexlify(c[16:]),"\n" decryptor = AES.new(key, AES.MODE_CFB, IV) decryptedMessage = AEScipher.decrypt(c) print "Original IV : ", IV, "\n" print "Extracted IV from c: ", c[0:16], "\n" #Note the IV is prepended to encryptions, so we select from 16 bytes onwards. print "Decrypted message: ",decryptedMessage[16:],"\n"
Original message: This made him a grad student, and grad students existed not to learn things but to relieve the tenured faculty members of tiresome burdens such as educating people and doing research. Original bytes: 54686973206d6164652068696d206120677261642073747564656e742c20616e6420677261642073747564656e74732065786973746564206e6f7420746f206c6561726e207468696e67730a62757420746f2072656c69657665207468652074656e7572656420666163756c7479206d656d62657273206f662074697265736f6d652062757264656e73207375636820617320656475636174696e672070656f706c6520616e6420646f696e672072657365617263682e Ciphertext bytes: 4f595bab29afa2bcc53c2177665161dd54562ebff23d4e26e913b56ad599f15804f2cbb4c09e33ec4c04573320de1db59850d3837e07ace8253791de2e304acec8db2df12546183af43dc453183eff5d9642666f1ea332692f21760ff1c51458205039bcaf89d1d33c45a924abb6dab8d3b66787ff822546d668896b8cfe742df60fb5947bbb9776e3f9b9202563cba18b128fea3e640c9a6c64a93dde0fc0110dd4de4622c048021fa5c4c14fbacbabbc87fb644955df Original IV : 0123456789abcdef Extracted IV from c: 0123456789abcdef Decrypted message: This made him a grad student, and grad students existed not to learn things but to relieve the tenured faculty members of tiresome burdens such as educating people and doing research.
/projects/sage/sage-6.9/local/lib/python2.7/site-packages/Crypto/Util/number.py:57: PowmInsecureWarning: Not using mpz_powm_sec. You should rebuild using libgmp >= 5 to avoid timing attack vulnerability. _warn("Not using mpz_powm_sec. You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)
# Other options include md5 import hashlib H = hashlib.sha256() print H.hexdigest() H.update("") print H.hexdigest() H.update("World-class cereal-eating is a dance of fine compromises.") print H.hexdigest() H.update("The giant heaping bowl of sodden cereal, awash in milk, is the mark of the novice.") print H.hexdigest() print "\n" #May want to convert to an integer for use in algorithms. #usage : int(NumberInBase,base) intDigest = H.hexdigest() intDigest = int(H.hexdigest(),16) print H.hexdigest(), "in base 10: ", intDigest
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 c3cf840c6bb39a4eeb0d6bc72f22329c27e4be5b593b7fa79aa4219b4a53e007 aa53b316ac7572a9c8f4763894b000c744576b91d237b0ba9db4bd5ff6a48253 aa53b316ac7572a9c8f4763894b000c744576b91d237b0ba9db4bd5ff6a48253 in base 10: 77041068589408823543040667942386006997048194066943838391446772426139330970195