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

Function decode

recipes/Python/578797_Public_Key_Encryption_RSA/recipe-578797.py:133–146  ·  view source on GitHub ↗
(bcipher, privkey, verbose=False)

Source from the content-addressed store, hash-verified

131
132
133def decode(bcipher, privkey, verbose=False):
134 chunksize = int(log(privkey.modulus, 256))
135 outchunk = chunksize + 1
136 outfmt = '%%0%dx' % (chunksize * 2,)
137 result = []
138 for start in range_func(0, len(bcipher), outchunk):
139 bcoded = bcipher[start: start + outchunk]
140 coded = int(hexlify(bcoded), 16)
141 plain = pow(coded, *privkey)
142 chunk = unhexlify((outfmt % plain).encode())
143 if verbose:
144 print('Decode:', chunksize, chunk, plain, coded, bcoded)
145 result.append(chunk)
146 return b''.join(result).rstrip(b'\x00').decode('utf-8')
147
148
149def key_to_str(key):

Callers 1

mainFunction · 0.70

Calls 7

printFunction · 0.85
logFunction · 0.50
powFunction · 0.50
encodeMethod · 0.45
appendMethod · 0.45
decodeMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected