Pyro.699
October 16th, 2009, 08:09 PM
Hello everyone,
I was looking on the net for an easy way to store passwords in plain text files by encrypting them, then later decrypting them for use. I did find a few examples but nothing that could really help me.
So i built this script, its not perfect but it gets the job done. I was wondering if you could provide some feedback on it for ways i might be able to improve it. Currently you MUST have an even number of encryption values inorder for the script to work properly; I'm not sure why that it but it is something that i would like to fix.
import re
def encrypt(string, encrypt_values):
#Turn the string into a list
split = list(string)
#Declare the variable
password = ""
#This will be used to calculate the added length of the initial string (int values)
x = 0
for char in split:
#Turn the character into ascii integer
ascii = ord(char)
#Add to the total lengeh
x += ascii
'''
This is hard to explain, but i will try my best...
It creates a loop that runs from 0 to the length of encrypt_values.
If the modulo of ascii and the length of encrypt_values is equal to the loop number add it to the password
'''
for i in range(0,len(encrypt_values)):
if ascii%(len(encrypt_values)) == i:
password += "("+str( ((ascii + i) * int(encrypt_values[i]) /2) )+","+str(i)+")."
break
#Complete the password
password = "{"+str(x)+"}"+password.rstrip(".")
return password
def decrypt(string, encrypt_values):
#Get the total length
total = int(re.search("\{[0-9]*\}", string).group(0).lstrip("{").rstrip("}"))
#Remove the total length from the main password
string = re.sub("\{[0-9]*\}", "", string)
#Initiiate the character list
char_list = ['']
#Seperate by the periods
list = string.split(".")
for set in list:
#Get the inside portion
##0 represents the index number within the list
##1 represents the value given at that number
list2 = set.split(",")
#Cheep way to asign each value
ascii = ""
even = ""
for char in list2:
if ascii == "":
ascii = char.lstrip("(")
else:
even = char.rstrip(")")
#Preform the backwards algorythm to get the first value
for i in range(0,len(encrypt_values)):
if even == str(i):
ascii = ((int(ascii) * 2) / encrypt_values[i]) - i
break
char_list.insert(-1, ascii)
#Rebuild the password
password = ""
x = 0
for val in char_list:
if val != "":
x += int(val)
password += chr(int(val))+""
#Only return the work if the totals match up
if (x == total):
return password
else:
return -1
values = [43,32,5,34,32,1]
encrypts = ['test', 't3st with numb3rs', '(001 $$$']
for e in encrypts:
enc = encrypt(e, values)
dec = decrypt(enc, values)
print "Original: %s\nEncrypted: %s\nDecrypted: %s\n" % (e, enc, dec)
Original: test
Encrypted: {448}(295,2).(53,5).(1856,1).(295,2)
Decrypted: test
Original: t3st with numb3rs
Encrypted: {1620}(295,2).(918,3).(1856,1).(295,2).(85,2).(62, 5).(1836,3).(295,2).(265,2).(85,2).(280,2).(2040,3 ).(1760,1).(250,2).(918,3).(2451,0).(1856,1)
Decrypted: t3st with numb3rs
Original: (001 $$$
Encrypted: {325}(704,4).(1032,0).(1032,0).(800,1).(85,2).(774 ,0).(774,0).(774,0)
Decrypted: (001 $$$
Thank you and i look forward to your responses.
~Cody Woolaver
I was looking on the net for an easy way to store passwords in plain text files by encrypting them, then later decrypting them for use. I did find a few examples but nothing that could really help me.
So i built this script, its not perfect but it gets the job done. I was wondering if you could provide some feedback on it for ways i might be able to improve it. Currently you MUST have an even number of encryption values inorder for the script to work properly; I'm not sure why that it but it is something that i would like to fix.
import re
def encrypt(string, encrypt_values):
#Turn the string into a list
split = list(string)
#Declare the variable
password = ""
#This will be used to calculate the added length of the initial string (int values)
x = 0
for char in split:
#Turn the character into ascii integer
ascii = ord(char)
#Add to the total lengeh
x += ascii
'''
This is hard to explain, but i will try my best...
It creates a loop that runs from 0 to the length of encrypt_values.
If the modulo of ascii and the length of encrypt_values is equal to the loop number add it to the password
'''
for i in range(0,len(encrypt_values)):
if ascii%(len(encrypt_values)) == i:
password += "("+str( ((ascii + i) * int(encrypt_values[i]) /2) )+","+str(i)+")."
break
#Complete the password
password = "{"+str(x)+"}"+password.rstrip(".")
return password
def decrypt(string, encrypt_values):
#Get the total length
total = int(re.search("\{[0-9]*\}", string).group(0).lstrip("{").rstrip("}"))
#Remove the total length from the main password
string = re.sub("\{[0-9]*\}", "", string)
#Initiiate the character list
char_list = ['']
#Seperate by the periods
list = string.split(".")
for set in list:
#Get the inside portion
##0 represents the index number within the list
##1 represents the value given at that number
list2 = set.split(",")
#Cheep way to asign each value
ascii = ""
even = ""
for char in list2:
if ascii == "":
ascii = char.lstrip("(")
else:
even = char.rstrip(")")
#Preform the backwards algorythm to get the first value
for i in range(0,len(encrypt_values)):
if even == str(i):
ascii = ((int(ascii) * 2) / encrypt_values[i]) - i
break
char_list.insert(-1, ascii)
#Rebuild the password
password = ""
x = 0
for val in char_list:
if val != "":
x += int(val)
password += chr(int(val))+""
#Only return the work if the totals match up
if (x == total):
return password
else:
return -1
values = [43,32,5,34,32,1]
encrypts = ['test', 't3st with numb3rs', '(001 $$$']
for e in encrypts:
enc = encrypt(e, values)
dec = decrypt(enc, values)
print "Original: %s\nEncrypted: %s\nDecrypted: %s\n" % (e, enc, dec)
Original: test
Encrypted: {448}(295,2).(53,5).(1856,1).(295,2)
Decrypted: test
Original: t3st with numb3rs
Encrypted: {1620}(295,2).(918,3).(1856,1).(295,2).(85,2).(62, 5).(1836,3).(295,2).(265,2).(85,2).(280,2).(2040,3 ).(1760,1).(250,2).(918,3).(2451,0).(1856,1)
Decrypted: t3st with numb3rs
Original: (001 $$$
Encrypted: {325}(704,4).(1032,0).(1032,0).(800,1).(85,2).(774 ,0).(774,0).(774,0)
Decrypted: (001 $$$
Thank you and i look forward to your responses.
~Cody Woolaver