Encryption Code Example:

  This is an encryption with PyCrypto example tutorial. You will find that PyCrypto is THE go-to source of encryption with python for just about everything. An example of this is the Paramiko SSH module for python, which uses PyCrypto as a dependency to encrypt information. PyCrypto is very simple, yet extremely powerful and useful for encryption within the Python programming language. Knowledge of the basics of encryption is also a very useful skill to have.  

Video Tutorial:

 

The Code:

from Crypto.Cipher import AES
#base64 is used for encoding. dont confuse encoding with encryption#
#encryption is used for disguising data
#encoding is used for putting data in a specific format
import base64
# os is for urandom, which is an accepted producer of randomness that
# is suitable for cryptology.
import os
def encryption(privateInfo):
	#32 bytes = 256 bits
	#16 = 128 bits
	# the block size for cipher obj, can be 16 24 or 32. 16 matches 128 bit.
	BLOCK_SIZE = 16
	# the character used for padding
	# used to ensure that your value is always a multiple of BLOCK_SIZE
	PADDING = '{'
	# function to pad the functions. Lambda
	# is used for abstraction of functions.
	# basically, its a function, and you define it, followed by the param
	# followed by a colon,
	# ex = lambda x: x+5
	pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
	# encrypt with AES, encode with base64
	EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
	# generate a randomized secret key with urandom
	secret = os.urandom(BLOCK_SIZE)
	print 'encryption key:',secret
	# creates the cipher obj using the key
	cipher = AES.new(secret)
	# encodes you private info!
	encoded = EncodeAES(cipher, privateInfo)
	print 'Encrypted string:', encoded
       

Decryption Example:

from Crypto.Cipher import AES
import base64
import os
def decryption(encryptedString):
	PADDING = '{'
	DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
	#Key is FROM the printout of 'secret' in encryption
	#below is the encryption.
	encryption = encryptedString
	key = ''
	cipher = AES.new(key)
	decoded = DecodeAES(cipher, encryption)
	print decoded
 
 

Want more tutorials? Head to the