Python & Cryptographie – Cheatsheet

Variables

value = 10000  //entier
value = "ABCD"  //String
value = 0b10101010  //binaire
value = 0xffffffff  //hexadecimal
value  = b'ABC'  //bytes

Conversion

Depuis Entier

value = (16).to_bytes(2, 'big')  //bytes (ici 2 bytes big endian)
value = str( 16 )  //string
value = hex( 16 )  //hexadecimal
value = bin( 16 )    //binaire (string): 0b10000
value = "{0:08b}".format(16)  //binaire (string): 00010000 (8bits)

Depuis Binaire

value = int("11000", 2); //entier
value = hex(int("11000", 2)); //hexadecimal
value = bytes.fromhex( hex( int("11000", 2) )[2:] )
//string (ASCII)
binary_int = int("11000010110001001100011", 2);
byte_number = binary_int.bit_length() + 7 // 8
binary_array = binary_int.to_bytes(byte_number, "big")
value = binary_array.decode()

Depuis Hexadécimal / Hexadécimal (String)

value = bytes.fromhex("ff")  //bytes
value = bin( 0xff )  //binaire
value = int( 0xff )  //entier
value = int("eff01dhd", 16)  //entier

Depuis String

value = bytes("ABC", 'utf-8')  //bytes
value = hex( int("ff", 16) )  //hexadecimal (string)
value = int("55")  //entier

Depuis Bytes

value = b'ABC'.decode("utf-8")   //string
value = b'ABC'.hex()   //hexadecimal
value = int(b'A'.hex(), 16) //entier

Table des opérations logiques bitwise (Python)

A AND Ba & b
A OR Ba | b
A XOR Ba ^ b
NOT A ~a
n-bit LEFT SHIFT A a << n
n-bit RIGHT SHIFT A a >> n
A = A AND B a &= b
A = A OR Ba |= b
A = A XOR Ba ^= b
A = n-bit LEFT SHIFT Aa <<= n
A = n-bit RIGHT SHIFT Aa >>= n