| 109 | return b''.join(result) |
| 110 | |
| 111 | def decode(bcipher, privkey, verbose=False): |
| 112 | chunksize = int(log(pubkey.modulus, 256)) |
| 113 | outchunk = chunksize + 1 |
| 114 | outfmt = '%%0%dx' % (chunksize * 2,) |
| 115 | result = [] |
| 116 | for start in range(0, len(bcipher), outchunk): |
| 117 | bcoded = bcipher[start: start + outchunk] |
| 118 | coded = int(hexlify(bcoded), 16) |
| 119 | plain = pow(coded, *privkey) |
| 120 | chunk = unhexlify((outfmt % plain).encode()) |
| 121 | if verbose: print('Decode:', chunksize, chunk, plain, coded, bcoded) |
| 122 | result.append(chunk) |
| 123 | return b''.join(result).rstrip(b'\x00').decode() |
| 124 | |
| 125 | |
| 126 | if __name__ == '__main__': |