Runescape Bits & Bytes
https://www.rsbandb.com/forums/

text file encryption/decryption program! rate it!
https://www.rsbandb.com/forums/viewtopic.php?f=38&t=27196
Page 1 of 1

Author:  kingofrogue0 [ February 23rd, 2006, 8:44 pm ]
Post subject:  text file encryption/decryption program! rate it!

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 :twisted:

Author:  Adbot [ February 23rd, 2006, 8:44 pm ]
Post subject:  Register and login to get these in-post ads to disappear


Author:  jrdgames [ February 26th, 2006, 4:47 pm ]
Post subject: 

I cant execute the .py file on my windows pc

Author:  Jeff [ February 26th, 2006, 5:17 pm ]
Post subject: 

I don't know what this encryption program's purpose is.
If you encrypt it, someone can crack it really easy seeing the algorithm your using there.
Or even faster if they get hold of this script . :wink:

Author:  jrdgames [ February 27th, 2006, 8:37 am ]
Post subject: 

youve got a good point there Jeff, maybe you should make this code pm access only kingofrogue0?

Author:  kingofrogue0 [ March 7th, 2006, 7:26 pm ]
Post subject: 

point well taken... :oops:

it is kinda pointless though. I was just...REALLY bored :P

you can change the way it encrypts by going to the place in the "scrambleFile" and "unscrambleFile" functions where it says to shift the number over by 15 or whatever and change it to the number you like. meaning try this:

Code:
#Shift that character over by a number and wrap it
            num=num-x
            if num<0:
                num=num+256


do that in both of the functions and replace "x" with the number you want. make sure it is the SAME NUMBER though. I'm not very good at explaining troubleshooting :( so if you have trouble, I don't know if I can help you...but I can try :)

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/