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

Method encrypt_string

ciphers/xor_cipher.py:81–104  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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 """

Callers 1

encrypt_fileMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected