my programming teacher and I came up with a "scramble" and "unscramble" python code that allows people to encrypt their text files so that they are unreadable, and then fix them again at any time. here is how to do it:
copy the code below into notepad or a python editor; for mac users, i think you use "textedit" or a python editor; i am unsure how to do this for linux; and save as a .py in the same folder as the file you wish to alter
run the file like you would an application, or open a text file: just double-click on it
follow the input menus and press "enter" or "return" after you enter the information at each prompt
voila! an encrypted/decrypted file!
oh, and here is the code:
Code:
#Text encryption program.
#Written by Asa S. and Ben A.
#----------------------------------
import binascii, string
#This function converts an integer to hexadecimal
def iToHex(a):
div=a/16
rem=a%16
if rem==10: rem="a"
if rem==11: rem="b"
if rem==12: rem="c"
if rem==13: rem="d"
if rem==14: rem="e"
if rem==15: rem="f"
if div > 0:
return (str(iToHex(div)) + str(rem))
else:
return (str(rem))
#This funciton converts an ascii character to an integer
def aTob(as):
bs=binascii.b2a_hex(as)
return (int(bs,16))
#This function converts an integer to an ascii character
def bToa(sb):
e=iToHex(sb)
if len(e)%2:
e="0"+e
return (binascii.a2b_hex(e))
# Scramble file function
#--------------------------------------------
def scrambleFile (srcName, destName):
#Open src
f=open(srcName, "r")
#Read its contents
s=f.read()
#Close src
f.close()
#Open dest
f=open(destName, "w")
#Write the contents
for x in s:
if x<>"":
#Convert our unencrypted text to a number
num=aTob(x)
#Shift that character over by 15 and wrap it
num=num+10
if num>255:
num=num-256
#Convert the new encrypted number back to a character
ascii=bToa(num)
#Write that character into the file
f.write(ascii)
#Close dest
f.close()
#--------------------------------------------
#Unscramble file function
#--------------------------------------------
def unscrambleFile (srcName, destName):
#Open src
f=open(srcName, "r")
#Read its contents
s=f.read()
#Close src
f.close()
#Open dest
f=open(destName, "w")
#Write the contents
for x in s:
#If "x" isn't blank:
if x!="":
#Convert our encyrpted text into a number
num=aTob(x)
#Shift that character over by 15 and wrap it
num=num-10
if num<0:
num=num+256
#Convert the new unencrypted number back to a character
ascii=bToa(num)
#Write that character back into the file
f.write(ascii)
#Close dest
f.close
#---------------------------------------
#Call functions in an infinite loop
while True:
#Ask what the user wants to do
function=input("Enter 1 to encrypt; enter 2 to decrypt; enter 3 to quit. >>> ")
#See if the user wants to encrypt a file
if function==1:
x=raw_input("Enter the name of the file you wish to encrypt. >>> ")
print ("Encrypting file...")
scrambleFile(x, x)
print ("Encryption successful.")
see=input("Press 1 to read " + x + "; press 2 to continue to the main menu. >>> ")
#Ask if they want to read the file
if see==1:
openx=open (x, "r")
readx=openx.read()
print (readx)
if see==2:
pass
#See if the user wants to decrypt a file
if function==2:
x=raw_input("Enter the name of the file you wish to decrypt. >>> ")
print ("Decrypting file...")
unscrambleFile(x, x)
print ("Decryption successful.")
see=input("Press 1 to read " + x + "; press 2 to continue to the main menu. >>> ")
#Ask if they want to read the file
if see==1:
openx=open (x, "r")
readx=openx.read()
print (readx)
if see==2:
pass
#See if they want to quit; if they do, then break the loop
if function==3:
break
have fun

_________________


I'm lean, I'm mean... I'm famished. Make me a sandwich.
SOTW Wins|
51|