from Crypto.Cipher import Blowfish
from Crypto import Random
from struct import pack
from sys import argv
def encriptar(key, plaintext):
bs = Blowfish.block_size
iv = Random.new().read(bs)
bf = Blowfish.new(key, Blowfish.MODE_CBC, iv)
plen = bs - (len(plaintext) % bs)
padding = [plen]*plen
padding = pack('b'*plen, *padding)
msg = iv + bf.encrypt(plaintext + padding)
return msg
def recuperar(key, ciphertext):
bs = Blowfish.block_size
v = Random.new().read(bs)
uncipher = Blowfish.new(key,Blowfish.MODE_CBC, v)
m = uncipher.decrypt(ciphertext)
return m[8:]
if __name__ == '__main__':
msg = ''
for i in xrange(1,len(argv)):
if argv[i].lower() == '-p':
key = argv[i+1]
elif argv[i].lower() == '-m':
msg = argv[i+1]
elif argv[i].lower() == '--test':
print msg
ct = encriptar(key,msg)
pt = recuperar(key, ct)
print 'Texto Cifrado:\t %s' % ct
print 'Texto Original:\t %s' % pt