Post

Beginner Crypto Toolkit

Beginner Crypto Toolkit

When it comes to CTF challenges and cybersecurity, having a proper toolkit becomes an essential part; enabling one to use the underlying knowledge with ease. Personally I rather centralize my practice of cryptography in Python.

An inseparable part of cryptography in the internet environment is encoding; Integer, hexadecimal, binary, Ascii and so on.
In this post I’m gonna provide a set of commands and a few tricks, to convert one encoding to another and perform the desired transformations, resulting in a handy platform to deal with in practice cryptography. Enjoy :)

Basic Encodings


In each section we’ll see the commands converting other encodings to the entitled one.

Integer

  • Numbers

To Convert a number to an integer.

CommandFormatParametersReturn Type
intint(x, base)x: str
base: int
int

Examples:

1
2
>> int("0a", 16)
Output: 10
1
2
>> int("110", 2)
Output: 6
1
2
>> int("123")
Output: 123
  • Character

To return the Unicode code point for a one-character string.

CommandFormatParametersReturn Type
ordord(c)c: one-character strint

Examples:

1
2
3
4
>> ord("A")
Output: 65
>> ord("\n")
Output: 10
  • Bytes

Using the bytes object as a list, we can access each byte as an integer value.

Examples:

1
2
3
4
5
6
7
8
9
>> d = b'\x90\x90Hi'
>> print(d[0])
Output: 144
>> print(d[1])
Output: 144
>> print(d[2])
Output: 72
>> print(d[3])
Output: 105

Hex

  • Integer

To return the hexadecimal representation of an integer.

CommandFormatParametersReturn Type
hexhex(x)x: intstr

Examples:

1
2
>> hex(100)
Output: '0x64'

Bytes

  • Hex

To create a bytes object from a string of hexadecimal numbers.

CommandFormatParametersReturn Type
bytes.fromhexbytes.fromhex(s)s: strbytes object

Examples:

1
2
>> bytes.fromhex("0a")
Output: b'\n'
1
2
>> bytes.fromhex("41 0a")
Output: b'A\n'
  • Integer

To convert an iterable of ints to bytes.

CommandFormatParametersReturn Type
bytesbytes(x)x: an iterable of intsbytes object

Examples:

1
2
3
>> i = [72, 101, 108, 108, 111]
>> bytes(i)
Output: b'Hello'

Ascii

  • Bytes

To decode the bytes using the codec registered for encoding.

CommandFormatParametersReturn Type
bytes.decodebytes.decode(self, encoding)encoding: strstr

Examples:

1
2
3
4
5
6
7
>> d = bytes.fromhex("41")
>> d.decode("ascii")
Output: 'A'
>> d.decode("utf-8")
Output: 'A'
>> d.decode()
Output: 'A'
  • Integer

To return a Unicode string of one character.

CommandFormatParametersReturn Type
chrchr(i)i: intstr

Examples:

1
2
3
4
>> chr(110)
Output: 'n'
>> chr(123)
Output: '{'

One for All


There is a library in python named codecs. We can use the decode and encode functions to perform various text and binary transformations.

CommandFormatParametersReturn Type
decodedecode(obj, encoding)obj: depending on the encoding
encoding: str
depending on the encoding

The command format of encode is similar.

List of useful encodings:

CodecMeaning
asciiConvert the operand to ascii.
base64Convert the operand to multiline MIME base64 (the result always includes a trailing ‘\n’).
bz2Compress the operand using bz2.
hexConvert the operand to hexadecimal representation, with two digits per byte.
zipCompress the operand using gzip.
rot13Return the Caesar-cypher encryption of the operand.

You can see the full list here.

Examples:

1
2
3
4
5
6
7
8
>> from codecs import encode
>> d = b'Hello'
>> encode(d, "base64")
Output: b'SGVsbG8=\n'
>> encode(d, "zip")
Output: b'x\x9c\xf3H\xcd\xc9\xc9\x07\x00\x05\x8c\x01\xf5'
>> encode(d, "hex")
Output: b'48656c6c6f'
1
2
3
4
5
6
>> from codecs import decode
>> d = b'Hello'
>> decode(d, "ascii")
Output: 'Hello'
>> decode(b'48656c6c6f', "hex")
Output: b'Hello'

A few Tricks


zip

Using the zip python command we can construct tuples out of our inputs, suitable to perform binary operation upon them.

CommandFormatParametersReturn Type
zipzip(iterables)iterablesA zip object yielding tuples until an input is exhausted.

Examples:

1
2
3
4
5
6
>> list(zip('abcdefg', range(3), range(4)))
Output: [('a', 0, 0), ('b', 1, 1), ('c', 2, 2)]
>> list(zip('AAA', 'BBBB'))
Output: [('A', 'B'), ('A', 'B'), ('A', 'B')]
>> list(zip(b'AAA', b'BBBB'))
Output: [(65, 66), (65, 66), (65, 66)]

Application:

Using zip to implement a byte-by-byte xor to a ciphertext with a key.

1
2
3
4
>> key = b'happy'
>> cipher =  b'hellothereiamheretotestthiscommand'
>> bytes([c ^ k for c, k in zip(cipher, key)])
Output: b'\x00\x04\x1c\x1c\x16'

Resources


  • Python Documents
This post is licensed under CC BY 4.0 by the author.