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

Function encode

recipes/Python/578797_Public_Key_Encryption_RSA/recipe-578797.py:115–130  ·  view source on GitHub ↗
(msg, pubkey, verbose=False)

Source from the content-addressed store, hash-verified

113
114
115def encode(msg, pubkey, verbose=False):
116 chunksize = int(log(pubkey.modulus, 256))
117 outchunk = chunksize + 1
118 outfmt = '%%0%dx' % (outchunk * 2,)
119 bmsg = msg if isinstance(msg, binary_type) else msg.encode('utf-8')
120 result = []
121 for start in range_func(0, len(bmsg), chunksize):
122 chunk = bmsg[start:start + chunksize]
123 chunk += b'\x00' * (chunksize - len(chunk))
124 plain = int(hexlify(chunk), 16)
125 coded = pow(plain, *pubkey)
126 bcoded = unhexlify((outfmt % coded).encode())
127 if verbose:
128 print('Encode:', chunksize, chunk, plain, coded, bcoded)
129 result.append(bcoded)
130 return b''.join(result)
131
132
133def decode(bcipher, privkey, verbose=False):

Callers 3

mainFunction · 0.70
_runMethod · 0.50
_runMethod · 0.50

Calls 6

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

Tested by

no test coverage detected