AES ENCRYPTION AND DECRYPTION

import base64
from Crypto import Random
from Crypto.Cipher import AES
inp=raw_input(“Enter the raw input : “);
key=raw_input(“Enter anything to generate key \nEnter exact 32 digits key or press enter for auto generate : “);
if key==”:
key=Random.new().read(32)
bs=32 # size requirement of our input

def encrypt(raw):
raw =pad(raw) #padding to match the size
iv = Random.new().read(AES.block_size) # random 16 bytes chars ie: AES.block_size
cipher = AES.new(key, AES.MODE_OFB, iv)
return base64.b64encode(iv + cipher.encrypt(raw)) # 64 bytes

def decrypt(enc):
enc = base64.b64decode(enc) # 48 bytes
iv = enc[:AES.block_size] #first 16 bytes
cipher = AES.new(key, AES.MODE_OFB, iv)
return unpad(cipher.decrypt(enc[AES.block_size:])).decode()

def pad(s):
return s + (bs – len(s) % bs) * chr(bs – len(s) % bs) # padding the raw specific chars to fit inside the block

def unpad(s):
return s[:-ord(s[len(s)-1:])]

print “-“100 cipher= encrypt(inp) print cipher print len(cipher) print “-“100
raw=decrypt(cipher)
print raw

DES ENCRYPTION AND DECRYPTION

import base64
from Crypto import Random
from Crypto.Cipher import DES3
inp=raw_input(“Enter the raw input : “);
key=raw_input(“Enter anything to generate key \nEnter exact 16 digits key or press enter for auto generate : “);
if key==”:
key=Random.new().read(16)
bs=16 # size requirement of our input

def encrypt(raw):
raw =pad(raw) #padding to match the size
iv = Random.new().read(DES3.block_size) # random 8 bytes chars ie: DES3.block_size
cipher = DES3.new(key, DES3.MODE_OFB, iv)
return base64.b32encode(iv + cipher.encrypt(raw)) # 32 bytes

def decrypt(enc):
enc = base64.b32decode(enc) # 24 bytes
iv = enc[:DES3.block_size] #first 16 bytes
cipher = DES3.new(key, DES3.MODE_OFB, iv)
return unpad(cipher.decrypt(enc[DES3.block_size:])).decode()

def pad(s):
return s + (bs – len(s) % bs) * chr(bs – len(s) % bs) # padding the raw specific chars to fit inside the block

def unpad(s):
return s[:-ord(s[len(s)-1:])]

print “-“100 cipher= encrypt(inp) print cipher print len(cipher) print “-“100
raw=decrypt(cipher)
print raw

PBKDF2

!/usr/bin/python

import hashlib,binascii
import os

def pbkdf2(password,Hash,iteration) :
if Hash==2: hash_f=”sha512″
else :hash_f=”sha256″
salt=os.urandom(16)
return hashlib.pbkdf2_hmac(hash_f,password,salt,iteration)

print “-“100 password=raw_input(“Enter the Password : “) print “-“100
Hash=input(“Enter 1. Sha256 (Default) 2. Sha512 :”)
print “-“100 iteration=input(“Enter the no. Iteration (minimum 1000) : “) print “-“100
res=pbkdf2(password,Hash,iteration)
res=binascii.hexlify(res)
print “PBKDF2 key :”,res
print “-“*100

LEAVE A REPLY

Please enter your comment!
Please enter your name here