MCPcopy Create free account
hub / github.com/ActiveState/code / encrypt

Function encrypt

recipes/Python/572196_RSA/recipe-572196.py:150–163  ·  view source on GitHub ↗
(string,power,n,bits=128)

Source from the content-addressed store, hash-verified

148 return ret
149
150def encrypt(string,power,n,bits=128):
151 string=string.replace('/',"/s").replace('\0',"/0") # escape \x00
152 # because crypt() ignores leading zeros
153 bytes=bits//8
154 lst=[]
155 while string:
156 lst.append(string[:bytes-1]) # string must have a lesser value than
157 string=string[bytes-1:] # n, so truncate and pad
158 for i in range(len(lst)):
159 lst[i]=crypt(lst[i],power,n)
160 lst[i]='\0'*(bytes-len(lst[i]))+lst[i] # pad with zeros
161 # don't worry about removing these later: crypt() ignores
162 # leading zeros already, so they are always removed
163 return ''.join(lst) # the length of this must be a multiple of bytes
164
165def decrypt(string,power,n,bits=128):
166 bytes=bits//8

Callers 2

writeMethod · 0.70
flushMethod · 0.70

Calls 5

rangeFunction · 0.85
cryptFunction · 0.70
replaceMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected