#!/usr/bin/python3
from utilities import parseArguments, findRandWithNoZero, removePadding

args = parseArguments('dec')


keyFilename    = args.k[0]
inputFilename  = args.i[0]
outputFilename = args.o[0]
plaintext      = ''

NBits = 0
N     = 0
d     = 0

with open(inputFilename, 'r') as fin:
    ciphertext = int(fin.readline())

with open(keyFilename, 'r') as fin:
    NBits = int(fin.readline())
    N = int(fin.readline())
    d = int(fin.readline())

msgLength = int(NBits/8)

# print(f'NBits:{NBits}\nN:{N}\ne:{d}')

mint = pow(ciphertext, d, N) 
m    = mint.to_bytes(msgLength, 'big')
# print('cycled int', mint)
# print('cycled message', m)

m = removePadding(m)

with open(outputFilename, 'w+') as fout:
    fout.write(str(m))