ciphers.xor_cipher

author: Christian Bender date: 21.12.2017 class: XORCipher

This class implements the XOR-cipher algorithm and provides some useful methods for encrypting and decrypting strings and files.

Overview about methods

  • encrypt : list of char

  • decrypt : list of char

  • encrypt_string : str

  • decrypt_string : str

  • encrypt_file : boolean

  • decrypt_file : boolean

Classes

XORCipher

Module Contents

class ciphers.xor_cipher.XORCipher(key: int = 0)
decrypt(content: str, key: int) list[str]

input: ‘content’ of type list and ‘key’ of type int output: decrypted string ‘content’ as a list of chars if key not passed the method uses the key by the constructor. otherwise key = 1

Empty list >>> XORCipher().decrypt(“”, 5) []

One key >>> XORCipher().decrypt(“hallo welt”, 1) [‘i’, ‘`’, ‘m’, ‘m’, ‘n’, ‘!’, ‘v’, ‘d’, ‘m’, ‘u’]

Normal key >>> XORCipher().decrypt(“HALLO WELT”, 32) [‘h’, ‘a’, ‘l’, ‘l’, ‘o’, ‘x00’, ‘w’, ‘e’, ‘l’, ‘t’]

Key greater than 255 >>> XORCipher().decrypt(“hallo welt”, 256) [‘h’, ‘a’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘w’, ‘e’, ‘l’, ‘t’]

decrypt_file(file: str, key: int) bool

input: filename (str) and a key (int) output: returns true if decrypt process was successful otherwise false if key not passed the method uses the key by the constructor. otherwise key = 1

decrypt_string(content: str, key: int = 0) str

input: ‘content’ of type string and ‘key’ of type int output: decrypted string ‘content’ if key not passed the method uses the key by the constructor. otherwise key = 1

Empty list >>> XORCipher().decrypt_string(“”, 5) ‘’

One key >>> XORCipher().decrypt_string(“hallo welt”, 1) ‘i`mmn!vdmu’

Normal key >>> XORCipher().decrypt_string(“HALLO WELT”, 32) ‘hallox00welt’

Key greater than 255 >>> XORCipher().decrypt_string(“hallo welt”, 256) ‘hallo welt’

encrypt(content: str, key: int) list[str]

input: ‘content’ of type string and ‘key’ of type int output: encrypted string ‘content’ as a list of chars if key not passed the method uses the key by the constructor. otherwise key = 1

Empty list >>> XORCipher().encrypt(“”, 5) []

One key >>> XORCipher().encrypt(“hallo welt”, 1) [‘i’, ‘`’, ‘m’, ‘m’, ‘n’, ‘!’, ‘v’, ‘d’, ‘m’, ‘u’]

Normal key >>> XORCipher().encrypt(“HALLO WELT”, 32) [‘h’, ‘a’, ‘l’, ‘l’, ‘o’, ‘x00’, ‘w’, ‘e’, ‘l’, ‘t’]

Key greater than 255 >>> XORCipher().encrypt(“hallo welt”, 256) [‘h’, ‘a’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘w’, ‘e’, ‘l’, ‘t’]

encrypt_file(file: str, key: int = 0) bool

input: filename (str) and a key (int) output: returns true if encrypt process was successful otherwise false if key not passed the method uses the key by the constructor. otherwise key = 1

encrypt_string(content: str, key: int = 0) str

input: ‘content’ of type string and ‘key’ of type int output: encrypted string ‘content’ if key not passed the method uses the key by the constructor. otherwise key = 1

Empty list >>> XORCipher().encrypt_string(“”, 5) ‘’

One key >>> XORCipher().encrypt_string(“hallo welt”, 1) ‘i`mmn!vdmu’

Normal key >>> XORCipher().encrypt_string(“HALLO WELT”, 32) ‘hallox00welt’

Key greater than 255 >>> XORCipher().encrypt_string(“hallo welt”, 256) ‘hallo welt’

__key