encryption key validation in Audio Steganography project

3 views (last 30 days)
I am trying to embed text inside audio using a password as a secret key and with the very same password i want to extract text as well. I found the two password is different although i have provided the same password.
lsb_enc.m
function lsb_enc(wavin, wavout, text, pass)
if (nargin<2)
wavout='stego.wav';
end
if (nargin<3)
text = 'Text to be hidden';
end
if nargin<4
pass = 'password123';
end
%Header of wav = 1:40, Length of wav = 41:43, Data part = 44:end
fid = fopen(wavin,'r');
header = fread(fid,40,'uint8=>char');
dsize = fread(fid,1,'uint32');
[cover,len_cover] = fread(fid,inf,'uint16');
fclose(fid);
bin = d2b(double(text),8);
[m,n] = size(bin);
M = d2b(m,40)';
len_msg = m*n;
%Encrypting message with pseudorandom sequence
bitx = 1*xor(reshape(bin,m*n,1), prng(pass, len_msg));
binx = reshape(bitx, m, n);
if (len_cover >= len_msg+48)
%Control variable is encoded(1:8)
control = d2b(mod(sum(double(pass)),256),8)';
cover(1:8) = bitset(cover(1:8),1,control(1:8));
%Length of message is encoded (9:48)
cover(9:48) = bitset(cover(9:48),1,M(1:40));
%Message is encoded (49:48+len)
cover(49:48+len_msg)=bitset(cover(49:48+len_msg),1,binx(1:len_msg)');
%Stego file is saved
out = fopen(wavout,'w');
fwrite(out,header,'uint8');
fwrite(out,dsize,'uint32');
fwrite(out,cover,'uint16');
fclose(out);
else
error('Message is too long!');
end
end
function b = d2b( d, n )
%D2B Minimal implentation of de2bi function
if nargin<2
n = floor ( log (max (max (d), 1)) ./ log (p) ) + 1;
end
d = d(:);
power = ones (length (d), 1) * (2 .^ (0 : n-1) );
d = d * ones (1, n);
b = floor (rem(d, 2*power) ./ power);
end
function out = prng( key, L )
%PRNG Pseudorandom number generator for encryption/decryption
pass = sum(double(key).*(1:length(key)));
rand('seed', pass);
out = (rand(L, 1)>0.5);
end
lsb_dec.m
function out = lsb_dec(wavin, pass)
if (nargin<2)
pass = 'password123';
end
%Header = 1:40, Length = 41:43, Data = 44:end
fid = fopen(wavin,'r');
header = fread(fid,40,'uint8=>char');
dsize = fread(fid,1,'uint32');
stego = fread(fid,inf,'uint16');
fclose(fid);
%Control variable is read (1:8)
control = bitget(stego(1:8),1)';
disp(b2d(control))
disp(mod(sum(double(pass)),256))
if b2d(control) == mod(sum(double(pass)),256)
%Length of message is read (9:48)
m = bitget(stego(9:48),1);
len = b2d(m')*8;
%Hidden message is read and decrypted (49:len)
dat = xor(bitget(stego(49:48+len),1),prng(pass,len));
bin = reshape(dat,len/8,8);
out = char(b2d(bin))';
else
warning('Password is wrong or message is corrupted!');
out = [];
end
end
function d = b2d(b)
%B2D Minimal implentation of bi2de function
if isempty(b)
d = [];
else
d = b * (2 .^ (0:length(b(1,:)) - 1)');
end
end
function out = prng( key, L )
%PRNG Pseudorandom number generator for encryption/decryption
pass = sum(double(key).*(1:length(key)));
rand('seed', pass);
out = (rand(L, 1)>0.5);
end
the code should go to the if condition of lsb_dec.m function but it is not going there

Answers (1)

Swastik Sarkar
Swastik Sarkar on 24 Sep 2024
An issue has been identified when writing character arrays as uint8 using fwrite, which results in unnecessary characters being written alongside the matrix due to high Unicode values. Specifically, when writing the header, only the length of the header should be written, but extra characters such as "Â" appear when attempting to read the header after writing. To avoid this issue, it is advisable not to convert the header to a character array when reading it from the WAV file.
The line of code:
header = fread(fid,40,'uint8=>char');
should be modified to:
header = fread(fid,40,'uint8');
This modification ensures that no extra bytes are written, resulting in the expected output.
Refer to the following MATLAB execution for expected results:
(Note: The txt format is used in this example to facilitate file upload and display results on this platform; however, this code has been tested using WAV files.)
lsb_enc("wavin.txt")
lsb_dec("stego.wav")
9 9
ans = 'Text to be hidden'
Hope this helps.

Categories

Find more on Audio Processing Algorithm Design in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!