MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / XORCipher

Class XORCipher

ciphers/xor_cipher.py:19–181  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17 - decrypt_file : boolean
18"""
19class XORCipher(object):
20
21 def __init__(self, key = 0):
22 """
23 simple constructor that receives a key or uses
24 default key = 0
25 """
26
27 #private field
28 self.__key = key
29
30 def encrypt(self, content, key):
31 """
32 input: 'content' of type string and 'key' of type int
33 output: encrypted string 'content' as a list of chars
34 if key not passed the method uses the key by the constructor.
35 otherwise key = 1
36 """
37
38 # precondition
39 assert (isinstance(key,int) and isinstance(content,str))
40
41 key = key or self.__key or 1
42
43 # make sure key can be any size
44 while (key > 255):
45 key -= 255
46
47 # This will be returned
48 ans = []
49
50 for ch in content:
51 ans.append(chr(ord(ch) ^ key))
52
53 return ans
54
55 def decrypt(self,content,key):
56 """
57 input: 'content' of type list and 'key' of type int
58 output: decrypted string 'content' as a list of chars
59 if key not passed the method uses the key by the constructor.
60 otherwise key = 1
61 """
62
63 # precondition
64 assert (isinstance(key,int) and isinstance(content,list))
65
66 key = key or self.__key or 1
67
68 # make sure key can be any size
69 while (key > 255):
70 key -= 255
71
72 # This will be returned
73 ans = []
74
75 for ch in content:
76 ans.append(chr(ord(ch) ^ key))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected