Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def gene(d, n):
- i = 0
- for i in range(1, n+1):
- if ((i*d)%n == 1):
- return i
- return 0
- def encrypt(text, e, n):
- ctext = []
- for a in text:
- if (ord(a) >= 97 and ord(a) <= 122):
- c = int(((ord(a)-96)**e)%n)
- ctext.append(c)
- elif (a == ' '):
- ctext.append(0)
- return ctext
- def decrypt(ctext, d, n):
- text = ''
- for c in ctext:
- if (c != 0):
- a = (c**d)%n
- text += chr(a+96)
- else:
- text += ' '
- return text
- choose = int(raw_input("1 - encrypt; 2 - decrypt: "))
- if choose == 1:
- p = int(input('input p: '))
- q = int(input('input q: '))
- n = p*q
- fin = (p-1)*(q-1)
- d = int(input('input d: '))
- e = gene(d, fin)
- with open("input.txt", "r") as f:
- text = f.read().lower()
- ct = encrypt(text, e, n)
- text = ""
- for el in ct:
- text += str(el) + ' '
- fo = open("output.txt", "w")
- fo.write(text)
- fo.close()
- print('n = ' + str(n))
- elif (choose == 2):
- with open("input.txt", "r") as f:
- d = int(input('input d: '))
- n = int(input('input n: '))
- ctext = f.read()
- ctext = ctext.split()
- ct = []
- for c in ctext:
- ct.append(int(c))
- tx = decrypt(ct, d, n)
- print(tx)
Advertisement
Add Comment
Please, Sign In to add comment