Problem encoding and decoding a message in an audio file

8 views (last 30 days)
Hello everyone. As the question says, I have encountered a problem while running the codes below. The goal of these codes is to input a message, encode it using the Vigenere method and then replacing the last bit of the sound wave's amplitude with the corresponding encoded message transformed to binary. So for example, we have the message 'Hello', we encode it using Vigenere with the key word 'maths' obtaining another string of characters, then we first transform each character to ascii and then to the corresponding binary, taking each of those bits and putting them in the audio file one by one, leaving a space of 16 bits between one letter and the following. I think that the problem has to do with the saving or the reading of the audio file, as if some of the bits introduced changed somehow, but I don't know why this happens.
Thank you very much.
Merry Christmas and Happy New Year!!
Here go both codes. There are plenty of lines uncommented, so feel free to ask if there's anything you don't understand.
Encoding
clear all
clc
[road,fs]=wavread('road.wav');
left=road(:,1);
right=road(:,2);
tamano=length(road);
mensaje=input('Introduzca el mensaje a codificar') % We ask the user to write the message to encode
ascii_mensaje=mensaje-0 % We convert the input to the equivalent ascii
% Vigenere encryption
ascii_mensaje=ascii_mensaje-31; % We want to avoid getting a modulo between 0 and 31, as these do not belong to the printable characters.
clave='mates'-0-31;%palabra clave en ascii
for i=0:length(clave):length(ascii_mensaje)
if (length(ascii_mensaje)-i)<length(clave)
codigo_vigenere(i+1:length(ascii_mensaje))=mod(ascii_mensaje(i+1:length(ascii_mensaje))+clave(1:(length(ascii_mensaje)-i)),127-31);
else
codigo_vigenere(i+1:i+length(clave))=mod(ascii_mensaje(i+1:i+length(clave))+clave,127-31);
end
end
codigo_vigenere=codigo_vigenere+31
ascii_mensaje_binario=dec2bin(codigo_vigenere)
size1=size(ascii_mensaje_binario);
fila=1;
time=(1/fs)*length(left);
t=linspace(0,time,length(left));
plot(t,road)
negativo=1;
for i=1:16:tamano
for j=1:7
if road(i+j-1,1)<0
negativo=-1;
end
aux=dec2bin(abs(round(road(i+j-1,1)*10000)))
aux(end)=ascii_mensaje_binario(fila,j)
road(i+j-1,1)=negativo*bin2dec(char(aux))/10000;
negativo=1;
end
fila=fila+1;
if fila>size1(1) % We break the loop once the message has been placed in the audio file.
break
end
end
wavwrite(road,fs,'prueba(modificada).wav');
Decoding
clear all
[road,fs]=wavread('prueba(modificada).wav');
left=road(:,1);
right=road(:,2);
tamano=length(road);
fila=1;
time=(1/fs)*length(left);
t=linspace(0,time,length(left));
plot(t,road)
for i=1:16:tamano
for j=1:7
aux1=num2str(dec2bin(abs(road(i+j-1,1)*10000)));
ascii_mensaje_binario(fila,j)=aux1(end)
end
fila=fila+1;
if fila>size1(1)
break
end
end
ascii_mensaje=bin2dec(num2str(ascii_mensaje_binario))'
comp= char(ascii_mensaje)
% Vigenere Decoding
ascii_mensaje=ascii_mensaje-31;
clave='mates'-0-31;
for i=0:length(clave):length(ascii_mensaje)
if (length(ascii_mensaje)-i)<length(clave)
codigo_vigenere(i+1:length(ascii_mensaje))=mod(ascii_mensaje(i+1:length(ascii_mensaje))-clave(1:(length(ascii_mensaje)-i)),127-31);
else
codigo_vigenere(i+1:i+length(clave))=mod(ascii_mensaje(i+1:i+length(clave))-clave,127-31);
end
end
codigo_vigenere=codigo_vigenere+31;
char(codigo_vigenere)

Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!