Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program.py --
- import random as r
- def keygen():
- string="QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm,.<>/;?:'#@~!£$€&*()1234567890 -+_=^"
- str_len=len(string)
- nst=[]
- for i in range(str_len):
- num=r.randint(0,1000)
- nst.append([string[i],num])
- nst.sort(key=lambda x:x[1])
- string=""
- for i in range(str_len):
- string+=nst[i][0]
- return string
- def get_file(string,m="r",d=0):
- try:
- file=open(string+".txt",m)
- except:
- print("Whoops... Looks like something bad happened or the file exists. Would recommend encrypting new input text, and then decrypting it.")
- if m=="r":
- d=file.read()
- return d
- elif m=="w":
- file.write(d)
- file.close()
- def crypt(d=0,k=0,m=0,p=0):
- nd=""
- if m==0:
- nf=""
- elif m==1:
- p=p.split(",")
- for i in range(len(d)):
- n=k.index(d[i])
- if m==0:
- f=r.randint(1,len(k)-1)
- nd+=k[int(n-f)]
- nf+=str(f)
- if i<len(d)-1:
- nf+=","
- elif m==1: nd+=k[int(p[i])+n-len(k)]
- if m==0: return nd,nf
- elif m==1: return nd
- def main_prog():
- choice=input("1. Encrypt text - 'en'\n2. Decrypt text - 'de'\nAnswer: ")
- if choice=="en": encrypt()
- elif choice=="de": decrypt()
- def encrypt():
- newkey=keygen()
- input_t=input("Input: ")
- newdata,newnums=crypt(input_t,newkey)
- get_file("key","w",newkey)
- get_file("padlock","w",newnums)
- get_file("text","w",newdata)
- print("Success...")
- main_prog()
- def decrypt():
- key=get_file("key")
- padlock=get_file("padlock")
- encrypted=get_file("text")
- decrypted=crypt(encrypted,key,1,padlock)
- print("Text below: ")
- print(decrypted)
- main_prog()
- main_prog()
- key.txt --
- )pts<,D=@?H1vX>Yg:U(qxKWo3$8~4C.wa+n£*T!BbM€5kF7Li^hNd /9AyurVRj#PGf2&Sz0_OeI6-'lQmZ;cEJ
- padlock.txt --
- 34,13,36,34,64
- text.txt --
- :R5FL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement