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
(self, content: str, key: int = 0)
| 142 | return ans |
| 143 | |
| 144 | def decrypt_string(self, content: str, key: int = 0) -> str: |
| 145 | """ |
| 146 | input: 'content' of type string and 'key' of type int |
| 147 | output: decrypted string 'content' |
| 148 | if key not passed the method uses the key by the constructor. |
| 149 | otherwise key = 1 |
| 150 | |
| 151 | Empty list |
| 152 | >>> XORCipher().decrypt_string("", 5) |
| 153 | '' |
| 154 | |
| 155 | One key |
| 156 | >>> XORCipher().decrypt_string("hallo welt", 1) |
| 157 | 'i`mmn!vdmu' |
| 158 | |
| 159 | Normal key |
| 160 | >>> XORCipher().decrypt_string("HALLO WELT", 32) |
| 161 | 'hallo\\x00welt' |
| 162 | |
| 163 | Key greater than 255 |
| 164 | >>> XORCipher().decrypt_string("hallo welt", 256) |
| 165 | 'hallo welt' |
| 166 | """ |
| 167 | |
| 168 | # precondition |
| 169 | assert isinstance(key, int) |
| 170 | assert isinstance(content, str) |
| 171 | |
| 172 | key = key or self.__key or 1 |
| 173 | |
| 174 | # make sure key is an appropriate size |
| 175 | key %= 256 |
| 176 | |
| 177 | # This will be returned |
| 178 | ans = "" |
| 179 | |
| 180 | for ch in content: |
| 181 | ans += chr(ord(ch) ^ key) |
| 182 | |
| 183 | return ans |
| 184 | |
| 185 | def encrypt_file(self, file: str, key: int = 0) -> bool: |
| 186 | """ |