CoCalc Public FilesCrypytography / 00_Basics.ipynb
Author: phonchi chung
Views : 66
Description: Jupyter notebook Crypytography/00_Basics.ipynb
Compute Environment: Ubuntu 18.04 (Deprecated)

# Overview

## Pycrypto

Pycrypto是一個完整的密碼學library，不過只支援到2.7版本，我們來看一下如何在python中使用他來做加解密 (only pairing x)

In [159]:
from Crypto.Cipher import AES
plaintext = 'Martinet is god!'

print len(key), len(plaintext)

encobj = AES.new(key, AES.MODE_ECB) # 128-bit 安全度的AES~
ciphertext = encobj.encrypt(plaintext)

print len(ciphertext), ciphertext.encode('hex') # 印出好看用!

16 16 16 292138362368e0cdd76508781b3f3b82

In [160]:
import binascii

ciphertext = binascii.unhexlify('292138362368e0cdd76508781b3f3b82')

decobj = AES.new(key, AES.MODE_ECB)
plaintext = decobj.decrypt(ciphertext)

# Resulting plaintext
print plaintext

Martinet is god!

## Cryptography

In [166]:
from cryptography.hazmat.primitives.ciphers import (
Cipher, algorithms, modes
)
from cryptography.hazmat.backends import default_backend
plaintext = 'Martinet is god!'
encryptor = Cipher(algorithms.AES(key),modes.ECB(),backend=default_backend()).encryptor()
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
print ciphertext.encode('hex')

292138362368e0cdd76508781b3f3b82
In [167]:
decryptor = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend()).decryptor()
plaintext = decryptor.update(ciphertext) #+ decryptor.finalize() finalized後不可再call~
print decryptor.update(ciphertext), plaintext

Martinet is god! Martinet is god!

## BitVector

Python pip 中還有很多好用的library來幫助處理bit level的運算，像是BitVector

In [156]:
import sys
import BitVector
if BitVector.__version__ < '3.2':
sys.exit("You need BitVector module of version 3.2 or higher" )
from BitVector import *

message = ""

# Initialize hashcode for the first block. Subsequetnly, the
# output for each 512-bit block of the input message becomes
# the hashcode for the next block of the message.
h0 = BitVector(hexstring='67452301')
h1 = BitVector(hexstring='efcdab89')
h3 = BitVector(hexstring='10325476')
h4 = BitVector(hexstring='c3d2e1f0')

bv = BitVector(textstring = message)
length = bv.length()
bv1 = bv + BitVector(bitstring="1")
length1 = bv1.length()
howmanyzeros = (448 - length1) % 512
zerolist = [0] * howmanyzeros
bv2 = bv1 + BitVector(bitlist = zerolist)
bv3 = BitVector(intVal = length, size = 64)
bv4 = bv2 + bv3

words = [None] * 80

for n in range(0,bv4.length(),512):
block = bv4[n:n+512]
words[0:16] = [block[i:i+32] for i in range(0,512,32)]
for i in range(16, 80):
words[i] = words[i-3] ^ words[i-8] ^ words[i-14] ^ words[i-16]
words[i] << 1
a,b,c,d,e = h0,h1,h2,h3,h4
for i in range(80):
if (0 <= i <= 19):
f = (b & c) ^ ((~b) & d)
k = 0x5a827999
elif (20 <= i <= 39):
f = b ^ c ^ d
k = 0x6ed9eba1
elif (40 <= i <= 59):
f = (b & c) ^ (b & d) ^ (c & d)
k = 0x8f1bbcdc
elif (60 <= i <= 79):
f = b ^ c ^ d
k = 0xca62c1d6
a_copy = a.deep_copy()
T = BitVector( intVal = (int(a_copy << 5) + int(f) + int(e) + int(k) + \
int(words[i])) & 0xFFFFFFFF, size=32 )
e = d
d = c
b_copy = b.deep_copy()
b_copy << 30
c = b_copy
b = a
a = T
h0 = BitVector( intVal = (int(h0) + int(a)) & 0xFFFFFFFF, size=32 )
h1 = BitVector( intVal = (int(h1) + int(b)) & 0xFFFFFFFF, size=32 )
h2 = BitVector( intVal = (int(h2) + int(c)) & 0xFFFFFFFF, size=32 )
h3 = BitVector( intVal = (int(h3) + int(d)) & 0xFFFFFFFF, size=32 )
h4 = BitVector( intVal = (int(h4) + int(e)) & 0xFFFFFFFF, size=32 )

message_hash = h0 + h1 + h2 + h3 + h4
hash_hex_string = message_hash.getHexStringFromBitVector()
sys.stdout.writelines((hash_hex_string, "\n"))


da39a3ee5e6b4b0d3255bfef95601890afd80709
In [ ]: