Professional Documents
Culture Documents
/usr/bin/env python
# -*- coding: UTF-8 -*import easygui,wave
import os, random, struct
from Crypto.Cipher import AES
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appeari
ng
#filename = askopenfilename() # show an "Open" dialog
def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024):
""" Encrypts a file using AES (CBC mode) with the
given key.
key:
The encryption key - a string that must be
either 16, 24 or 32 bytes long. Longer keys
are more secure.
in_filename:
Name of the input file
out_filename:
If None, '<in_filename>.enc' will be used.
chunksize:
Sets the size of the chunk which the function
uses to read and encrypt the file. Larger chunk
sizes can be faster for some files and machines.
chunksize must be divisible by 16.
"""
if not out_filename:
out_filename = in_filename + '.w'
iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16))
encryptor = AES.new(key, AES.MODE_CBC, iv)
filesize = os.path.getsize(in_filename)
with open(in_filename, 'rb') as infile:
with open(out_filename, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(iv)
while True:
chunk = infile.read(chunksize)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
outfile.write(encryptor.encrypt(chunk))
return out_filename
def decrypt_file(key, in_filename, out_filename=None, chunksize=24*1024):
""" Decrypts a file using AES (CBC mode) with the
given key. Parameters are similar to encrypt_file,
with one difference: out_filename, if not supplied
will be in_filename without its last extension
(i.e. if in_filename is 'aaa.zip.enc' then
indexDataHidden = 0
while indexDataHidden < sizeFileHidden:
bytesFileOculto.append(bytesFileAudioBase[indexSampleWav])
indexSampleWav += saltos
indexDataHidden += 1
nameFileResult = "pepe"
ficheroRecuperado = open(nameFileResult, 'wb')
newFileByteArray = bytearray(bytesFileOculto)
ficheroRecuperado.write(newFileByteArray)
ficheroRecuperado.close()
decrypt_file(ke,"pepe")
#os.remove("recuperado.cip")
easygui.textbox("hecho , archivo decodificado nombre recuperado")
os.remove("pepe")
else :
in_file= askopenfilename(title = "selecciona archivo a ocultar") # show an "
Open" dialog
pathFileToHide = encrypt_file(ke,in_file)
#cantidad = float(input("Dgame una cantidad en euros (hasta con 2 decimales):
"))
# DATOS DE LOS FICHEROS NECESARIOS:
#pathFileToHideCipher = pathFileToHide + ".cipher"
pathFileWav = askopenfilename(title = "selecciona archivo wav para ocultacio
n")
pathFileWavResult = pathFileToHide + 'av' # nombre de fichero de salida (el
que ocultar el contenido)
#
# COMPOSICIN DE LA TRAMA DE DATOS OCULTOS:
#
# (CABECERA)->(CIFRADO)->(TAMAO ARCHIVO)->(BYTES ARCHIVO)
#
#TRATANDO CON LOS FICHEROS:
# FICHERO BASE WAV DONDE LO VAMOS A OCULTAR
fileWav = wave.open(pathFileWav,'rb')
bytesfileWav = bytearray(fileWav.readframes(fileWav.getnframes())) # guardam
os los bytes (del muestreo) en un array de bytes
channels = fileWav.getnchannels() # canales de audio (1 mono, 2 stereo)
sampleWidth = fileWav.getsampwidth() # nmero de bytes para las muestras del s
amplerate
frameRate = fileWav.getframerate() # obtenemos el nmero de muestras (frames)
por segundo que realiza
numTotalFrames = fileWav.getnframes() # obtenemos el nmero total de muestras
disponibles (frames)
bytesfileWav[indexSampleWav] = cipher
indexSampleWav += jump
# grabamos el tamao bytes que ocupa el fichero oculto en los bytes asignados
para ello
# utilizamos desplazamiento de bits para guardarlo en bloques de un octeto (
byte) y luego poder recomponerlo
desplazamiento = 0
for i in range(numFraSize):
#print bytes(len(bytesFileToHide)>>24 & 0xFF) , bytes(len(bytesFileToHid
e)>>16 & 0xFF) , bytes(len(bytesFileToHide)>>8 & 0xFF) , bytes(len(bytesFileToHi
de)>>0 & 0xFF)
bytesfileWav[indexSampleWav] = len(bytesFileToHide)>>desplazamiento & 0x
FF
desplazamiento += 8
indexSampleWav += jump
# grabamos los bytes del archivo oculto en el audio
while indexFileToHide < len(bytesFileToHide):
bytesfileWav[indexSampleWav] = bytesFileToHide[indexFileToHide]
indexSampleWav += jump
indexFileToHide += 1
print indexFileToHide
# GRABAMOS EL FICHERO DE AUDIO RESULTANTE:
outEstegoWAV = wave.open(pathFileWavResult, "wb")
outEstegoWAV.setnchannels(channels)
outEstegoWAV.setsampwidth(sampleWidth)
outEstegoWAV.setframerate(frameRate)
outEstegoWAV.writeframes(bytesfileWav)
outEstegoWAV.close()
easygui.textbox("hecho , archivo codificado junto al original")
fileToHide.close()
os.remove(pathFileToHide)