Tuesday, February 8, 2011

.:Python Encryption and Storage:. | .:A Guide:.

Contents

  • Introduction
  • Encrypting
  • Storage
  • Making a *.pyc file
  • Single *.exe file
  • Closing
This is a guide to how to encrypt, or disguise, your text so people cannot read it, ways you can store information from a program for future use; and how to make your Python a single *.exe file.

There are many ways to hide data,

    * Encrypt the coding
    * Convert to a Python compiled file (*.pyc)
    * Convert to an *.exe file

Encrypting

The first is with the bz2 module. It comes native with Python, so you will not need any extra libraries. First you need to import it.
import bz2

Now, I will create a variable, and its contents will be the text I want to encrypt,

import bz2
x = raw_input("Enter Text To Encrypt:  ")

Now anything you enter in the raw_input will be stored in the local variable 'x'. The next bit of code does all the work. It encrypts your data and stores it into a variable.

import bz2
x = raw_input("Enter Text To Encrypt:  ")
y = bz2.compress(x)
That is it. Now the variable 'x' is encrypted and stored in the variable 'y'.

Here is an example. I will run this program and type 'Password' to be stored in the variable 'x'.




Now, you just need to make the text readable again. Use this code to decrypt it:

import bz2
x = raw_input("Enter Text To Encrypt:  ")
y = bz2.compress(x)
z= bz2.decompress(y)


Very simple. 4 Lines, and it can still be condensed if you need it to be!

There is one flaw in using this method, it is easily identified, as the encrypted text always begins with 'BZ'. Therefore, I have another method you can use.

This method works in quite the same way. You have to:

    * Import the module
    * Declare a variable that you want encrypted
    * Encrypt it

Here is the code for the second method:

import base64
x = raw_input("Enter Text To Encrypt:  ")
base64.b64encode(x)

Again, just 3 lines to encrypt it. Here is an example of how this one works:




As you can see, the text is not readable at all! Now to get it back to your text:
import base64
x = raw_input("Enter Text To Encrypt:  ")
y = base64.b64encode(x)
z = base64.b64decode(y)

4 lines. Very Very simple. If you do not just want to use 1, you CAN use both!

import base64, bz2
x = raw_input("Enter Text To Encrypt:  ")
y = base64.b64encode(x)
z = bz2.compress(x)
Now you can have a bit more security if you do not want your code released.


Storage


Many of you want to store information (or variables) from one program so another can use it later on. Well, the Pickle Module is great for that. It stores data in a way that only the Pickle Module can read. Pickle is great, but a faster version was created, cPickle. They work the same way, except that cPickle is fast. This module contains a faster reimplementation of the pickle module.

Here is how to load it:




import cPickle as pickle
If that does not work, you will have to use the native Pickle.





import pickle

Next, assign the text you want stored as a variable.

import cPickle as pickle
x = raw_input("Text you want stored:  ")


The next part may be a bit more complex than what you have read so far in this tutorial, so let me explain it,
    * The addition to the code above (see below) creates a new file, with the extension of *.p
    * It takes the variable 'x' and stores it in the file specified. If the file does not exist, it will be created in the current directory (unless specified otherwise).
    * The "w" means write. It will delete all contents of the *.p file specified and replace it with the information stored in variable 'x'. If you want to append data, use an "a" instead of "w".

import cPickle as pickle
x = raw_input("Text you want stored:  ")
pickle.dump(x, open("stored.p", "w"))
When you run this code, you will see the file 'stored.p' appear in the current directory. You will not be able to open it, it is used for pickle.
To read the information stored in the file, use the following code:
import cPickle as pickle
x = raw_input("Text you want stored:  ")
pickle.dump(x, open("stored.p", "w"))
y = pickle.load(open("stored.p"))
Note** I stored the loaded *.p file as 'y'.
This method is a great storage method. It is fast, and the *.p files cannot be read without pickle. It can also help to protect your code.
Making a *.pyc file
Many of you fear that if you leave your program as a *.py file, the source will be ripped, without credits. Well, another safety precaution is making it a *.pyc file. Automatically, a *.py file is created a a *.pyc the first time it is called upon (or imported).
So to create a *.pyc file, make a new file, and import the according *.py file you want converted to *.pyc.
Example:
I have a file, myCode.py, that I want as a *.pyc file. So I create a new file and type:
Save that file in the same directory as the *.py file you want converted.
Run it, and the program will now also be in *.pyc form!
Note** For those worried, you will still have the *.py file.
Also, you will have to delete the *.pyc everytime you want to update the *.py. You have to delete the *.pyc, close Python COMPLETELY. Then run it again with the updated features and it will make a new *.pyc. You will have to do this every time you update your program. Otherwise you will be like "I just fixed that! Why is it still doing the same thing?!" Single *.exe file ..............Tutorial on this available soon!

1 comment:

  1. Base64 is a pretty weak encryption method. Look into PyCrypto or Google's Keyczar. AES is a much better encryption method.

    ReplyDelete