在BMP图像中隐藏信息
1 clc; 2 clear; 3 fid = fopen('baboon.bmp', 'r'); 4 [a,length] = fread(fid,inf,'uint8'); 5 fclose(fid); 6 msgfid = fopen('hidden.txt','r'); 7 [msg,count] = fread(msgfid); 8 fclose(msgfid); 9 new_picture_array = a(1:54); %把文件头复制给新数组 10 new_picture_array(11) = 54 + count; %修改文件偏移 11 new_picture_array(3) = new_picture_array(3) + count;% 修改文件大小 12 j = 1; 13 for i = 55:54+count 14 new_picture_array(i) = uint8(msg(j)); %把信息隐藏在新的图像数组中 15 j = j + 1; 16 end 17 for i=55+count:length+count 18 new_picture_array(i) = a( i - count); % 把隐藏前文件的数据部分复制到新数组中 19 end 20 new_picture_array = uint8(new_picture_array); 21 fid = fopen('bmpheadhiding.bmp','wb'); %新建一个bmp图像 22 fwrite(fid, new_picture_array);%把带有隐藏信息的数组写入bmp图像 23 fclose(fid);
检测是否在头部隐藏信息
clc; clear; fid=fopen('baboonhide.bmp','r'); [a, length] = fread(fid, inf, 'uint8'); % 实际长度 fclose(fid); fid=fopen('baboonhide.bmp','r'); status = fseek(fid,2,'bof'); fileb = fread(fid, 4, 'uint8'); filelength = fileb(1)*1 + fileb(2)*256 + fileb(3)*256^2+fileb(4)*256^3; % 根据文件头算出长度 diff = length - filelength; %diff表示隐藏的信息长度如果相同,表示没有隐藏任何信息 fclose(fid);