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
(self,content, key = 0)
| 79 | |
| 80 | |
| 81 | def encrypt_string(self,content, key = 0): |
| 82 | """ |
| 83 | input: 'content' of type string and 'key' of type int |
| 84 | output: encrypted string 'content' |
| 85 | if key not passed the method uses the key by the constructor. |
| 86 | otherwise key = 1 |
| 87 | """ |
| 88 | |
| 89 | # precondition |
| 90 | assert (isinstance(key,int) and isinstance(content,str)) |
| 91 | |
| 92 | key = key or self.__key or 1 |
| 93 | |
| 94 | # make sure key can be any size |
| 95 | while (key > 255): |
| 96 | key -= 255 |
| 97 | |
| 98 | # This will be returned |
| 99 | ans = "" |
| 100 | |
| 101 | for ch in content: |
| 102 | ans += chr(ord(ch) ^ key) |
| 103 | |
| 104 | return ans |
| 105 | |
| 106 | def decrypt_string(self,content,key = 0): |
| 107 | """ |