####################################################################################
##
## author : dipankar dutta
## module need: http://www.voidspace.org.uk/downloads/pycrypto-2.3.win32-py2.7.zip
##
##
########################################################################################
from Crypto.Hash import MD5
from Crypto.Cipher import DES
from Crypto.Cipher import DES3
from Crypto import Random
from Crypto.PublicKey import RSA
import os
def hash(x):
m=MD5.new()
m.update(x)
print m.hexdigest()
def hashfile(filename):
h = MD5.new()
chunk_size = 8192
with open(filename, 'r') as f:
while True:
chunk = f.read(chunk_size)
if len(chunk) == 0:
break
h.update(chunk)
return h.hexdigest()
def encrypt_file(in_filename, out_filename, chunk_size, key, iv):
des3 = DES3.new(key, DES3.MODE_CFB, iv)
with open(in_filename, 'r') as in_file:
with open(out_filename, 'w') as out_file:
while True:
chunk = in_file.read(chunk_size)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
out_file.write(des3.encrypt(chunk))
def decrypt_file(in_filename, out_filename, chunk_size, key, iv):
des3 = DES3.new(key, DES3.MODE_CFB, iv)
with open(in_filename, 'r') as in_file:
with open(out_filename, 'w') as out_file:
while True:
chunk = in_file.read(chunk_size)
if len(chunk) == 0:
break
out_file.write(des3.decrypt(chunk))
def keygen():
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
print key.can_encrypt(),key.can_sign(),key.has_private()
return key
def RSAencrypt_file(in_filename, out_filename,pubkey):
with open(in_filename, 'r') as in_file:
with open(out_filename, 'w') as out_file:
chunk = in_file.read()
cipher=public_key.encrypt(chunk, 32) # encyption..
print cipher
out_file.write(str(cipher)) # strored as string hece tuple-->string
def RSAdecrypt_file(in_filename, out_filename, key):
with open(in_filename, 'r') as in_file:
with open(out_filename, 'w') as out_file:
string = in_file.read()
#-need convert string-->tuple
if string[0] + string[-1] == "()":
items = string[1:-1]
items = items.split(',')
print items
tuples=tuple(items)
print tuples
out_file.write(key.decrypt(tuples)) # decription..
def sign(msg,key):
hash = MD5.new(msg).digest()
signature = key.sign(hash, '')
return signature
def varify(msg,sign,pubkey):
hash = MD5.new(msg).digest()
return pubkey.verify(hash, sign)
#-simple hash--------
hash('dipankar')
hash('dipankar')
#---hash file---
print hashfile('input.txt');
#-----sysmmentic key encryption-----IV and Key is shared.......
##iv = Random.get_random_bytes(8)
##key='1234567890123456' # 16 byte.........
##f=open('input.txt', 'r')
##print 'input.txt: %s' % f.read()
##
##encrypt_file('input.txt', 'to_enc.enc', 8192, key, iv)
##f=open('to_enc.enc', 'r')
##print 'to_enc.enc: %s' % f.read()
##
##
##decrypt_file('to_enc.enc', 'to_enc.dec', 8192, key, iv)
##f=open('to_enc.dec', 'r')
##print 'to_enc.dec: %s' % f.read()
##
#-------Asymetic key encription..............
##
##key =keygen()
##print key
##
##f=open('input.txt', 'r')
##print 'Org here:: %s' % f.read()
##
##public_key = key.publickey()
##RSAencrypt_file('input.txt', 'to_enc.enc',public_key )
##f=open('to_enc.enc', 'r')
##print '\n\n\n\nCipher here:: %s' % f.read()
##
##
##RSAdecrypt_file('to_enc.enc', 'to_enc.dec', key)
##f=open('to_enc.dec', 'r')
##print '\n\nhurree get back here: %s' % f.read()
##
#--------------Digital signature-----------
text = 'dipankar'
key = keygen()
public_key=key.publickey()
signature = sign(text,key);
print varify(text,signature,public_key)
No comments:
Post a Comment