MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / decrypt

Method decrypt

ciphers/xor_cipher.py:68–101  ·  view source on GitHub ↗

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) [] On

(self, content: str, key: int)

Source from the content-addressed store, hash-verified

66 return [chr(ord(ch) ^ key) for ch in content]
67
68 def decrypt(self, content: str, key: int) -> list[str]:
69 """
70 input: 'content' of type list and 'key' of type int
71 output: decrypted string 'content' as a list of chars
72 if key not passed the method uses the key by the constructor.
73 otherwise key = 1
74
75 Empty list
76 >>> XORCipher().decrypt("", 5)
77 []
78
79 One key
80 >>> XORCipher().decrypt("hallo welt", 1)
81 ['i', '`', 'm', 'm', 'n', '!', 'v', 'd', 'm', 'u']
82
83 Normal key
84 >>> XORCipher().decrypt("HALLO WELT", 32)
85 ['h', 'a', 'l', 'l', 'o', '\\x00', 'w', 'e', 'l', 't']
86
87 Key greater than 255
88 >>> XORCipher().decrypt("hallo welt", 256)
89 ['h', 'a', 'l', 'l', 'o', ' ', 'w', 'e', 'l', 't']
90 """
91
92 # precondition
93 assert isinstance(key, int)
94 assert isinstance(content, str)
95
96 key = key or self.__key or 1
97
98 # make sure key is an appropriate size
99 key %= 256
100
101 return [chr(ord(ch) ^ key) for ch in content]
102
103 def encrypt_string(self, content: str, key: int = 0) -> str:
104 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected