import random, truerandom
alpha = map(chr, range(97,123))
num = range(26)
def conv2num(string):
convmesg = []
for x in range(len(list(string))):
if list(string)[x] in alpha:
for y in num:
if alpha[y] == list(string)[x]:
convnum = y
break
convmesg.append(convnum)
return convmesg
def conv2alpha(string):
convmesg = []
for x in range(len(list(string))):
if list(string)[x] in num:
for y in num:
if num[y] == list(string)[x]:
convchar = alpha[y]
break
convmesg.append(convchar)
return convmesg
def encrypt(mesg):
key = []
convmesg = conv2num(mesg)
mylist = truerandom.getnum(0,1000,len(mesg))
for x in range(len(mylist)):
keynum = mylist[x] % 26
key.append(keynum)
newkey = []
for z in length:
value = key[z] + convmesg[z]
while value >= 26:
value = value % 26
newkey.append(value)
crypto = \"\"
for a in length:
crypto += alpha[newkey[a]]
alphakey1 = conv2alpha(key)
alphakey = \"\"
for d in length:
alphakey += alphakey1[d]
return crypto, alphakey
def decrypt(key,crypto):
key = conv2num(key)
crypto = conv2num(crypto)
newval = []
for a in length:
newval.append(crypto[a]-key[a])
message = []
for b in length:
message.append(newval[b] % 26)
message = conv2alpha(message)
mesg = \'\'
for c in length:
mesg += message[c]
return mesg
print \'\\t\\t\\tOTP Algorithm\'
print \'\\t\\t\\t\\tBy: PhreakerD7\'
invalid = list(\'~!@#$%^&*()`1234567890,./?<>;:\\\'\"\\\\=+-_ \\n\\t\')
while True:
option = raw_input(\"\\nTo encrypt text, hit (1)\\nTo decrypt a message, hit (2)\\nTo encrypt a file, hit (3)\\nTo decrypt a file, hit (4)\\nTo exit, hit (5)\\n\\n>> \")
if option == \'1\':
message = raw_input(\"Message: \").lower()
checkmsg = list(message)
for x in range(len(checkmsg)):
if checkmsg[x] in invalid:
checkmsg[x] = \'\'
mesg = \'\'
for l in range(len(checkmsg)):
mesg += checkmsg[l]
length = range(len(mesg))
results = encrypt(mesg)
print \"Crypted Text:\", results[0]
print \"Key:\", results[1]
file2 = open(\'OTPtemp.txt\', \'w\')
file2.write(\'Crypted Text: \' + results[0] + \'\\n\\nKey: \' + results[1])
file2.close()
elif option == \'2\':
crypto = raw_input(\"Crypted Text: \")
key = raw_input(\"Key: \")
length = range(len(crypto))
text = decrypt(key, crypto)
print \"Decrypted Text:\", text
elif option == \'3\':
filename = raw_input(\'File name>> \')
file2 = open(filename, \'r\')
data = file2.read().lower()
file2.close()
checkmsg = list(data)
for x in range(len(checkmsg)):
if checkmsg[x] in invalid:
checkmsg[x] = \'\'
mesg = \'\'
for l in range(len(checkmsg)):
mesg += checkmsg[l]
length = range(len(mesg))
results = encrypt(mesg)
encryptfile = filename.split(\'.\')[0] + \'.enc\'
keyfile = filename.split(\'.\')[0] + \'.key\'
file2 = open(encryptfile, \'w\')
file2.write(results[0])
file2.close()
file2 = open(keyfile, \'w\')
file2.write(results[1])
file2.close()
print \'[*] Encrypted file at:\', encryptfile
print \'[*] Key file at:\', keyfile
elif option == \'4\':
filename = raw_input(\'Encrypted File name>> \')
keyfile = raw_input(\'Key file name>> \')
file2 = open(filename, \'r\')
data = file2.read()
file2.close()
file2 = open(keyfile, \'r\')
crypto = file2.read()
file2.close()
length = range(len(data))
text = decrypt(crypto,data)
decryptfile = filename.split(\'.\')[0] + \'.dec\'
file2 = open(decryptfile, \'w\')
file2.write(text)
file2.close()
print \'[*] Plain-Text file at:\', decryptfile
elif option == \'5\':
break