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

Function encode

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

Source from the content-addressed store, hash-verified

93 return KeyPair(Key(public, composite), Key(private, composite))
94
95def encode(msg, pubkey, verbose=False):
96 chunksize = int(log(pubkey.modulus, 256))
97 outchunk = chunksize + 1
98 outfmt = '%%0%dx' % (outchunk * 2,)
99 bmsg = msg.encode()
100 result = []
101 for start in range(0, len(bmsg), chunksize):
102 chunk = bmsg[start:start+chunksize]
103 chunk += b'\x00' * (chunksize - len(chunk))
104 plain = int(hexlify(chunk), 16)
105 coded = pow(plain, *pubkey)
106 bcoded = unhexlify((outfmt % coded).encode())
107 if verbose: print('Encode:', chunksize, chunk, plain, coded, bcoded)
108 result.append(bcoded)
109 return b''.join(result)
110
111def decode(bcipher, privkey, verbose=False):
112 chunksize = int(log(pubkey.modulus, 256))

Callers 1

recipe-577737.pyFile · 0.70

Calls 7

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

Tested by

no test coverage detected