语音的数据安全是网络语音通信的重要问题之一,混沌序列由于具有类随机性常被用作加密密钥。
clear all close all clc %% %aes加密 %利用密钥定义S盒 keyh = {'2b' '7e' '15' '16' '28' 'ae' 'd2' 'a6'... 'ab' 'f7' '15' '88' '09' 'cf' '4f' '3c'}; key = hex2dec(keyh); s = aesinit(key); %切分语音矩阵 x=audioread('s.wav'); x7=ceil(100*x(:,2))+127; n=floor(length(x7)/16); for i=1:n eval(['x',num2str(i+7),'=','x7(',num2str((i-1)*16+1),':',num2str(i*16),');']) eval(['ct',num2str(i),'=','aesjiami(s,x',num2str(i+7),');']) eval(['pt',num2str(i),'=','aesjiemi(s,ct',num2str(i),');']) end ct=[]; pt=[]; for i=1:n eval(['ct=[ct ct',num2str(i),'];']) eval(['pt=[pt pt',num2str(i),'];']) end t=(0:16*n-1)/8000; figure(6) subplot(3,1,1); plot(t,x7(1:16*n))%原始语音信号 grid on; axis tight; title('原始语音信号'); xlabel('time(s)'); ylabel('幅度'); subplot(3,1,2); plot(t,ct)%加密语音信号 grid on; axis tight; title('加密语音信号'); xlabel('time(s)'); ylabel('幅度'); subplot(3,1,3); plot(t,pt)%加密语音信号 grid on; axis tight; title('解密语音信号'); xlabel('time(s)'); ylabel('幅度'); function [output] = aes(s, oper, mode, input, iv, sbit) % AES 加密/解密矩阵处理 % output = aes(s, oper, mode, input, iv, sbit) % 加密/解密密钥标准:AES-128, AES-192, AES-256. % NIST SP800-38A标准的密钥都可以适用(e.g. ECB, CBC, OFB, CFB, CTR). % 解密例子: out = aesdecrypt(s, 'dec', 'ecb', data) % s: AES结构(由aesinit产生) % oper: 操作选项: % 'e', 'enc', 'encrypt', 'E',... = encrypt加密 % 'd', 'dec', 'decrypt', 'D',... = decrypt解密 % mode: 操作模式 % 'ecb' = Electronic Codebook Mode电子密码本模式 % 'cbc' = Cipher Block Chaining Mode密码块链接模式 % 'cfb' = Cipher Feedback Mode密码反馈方式, 加密回馈模式 % 'ofb' = Output Feedback Mode输出反馈模式 % 'ctr' = Counter Mode计数器模式 % 如需要使用counter mode,则使用另一函数AES_GET_COUNTER() % input: 16位明文/密文 % iv: 初始化向量 % sbit: CFB模式的参数长度 % output: 输出密文/明文 % % 参考文献: % Morris Dworkin, Recommendation for Block Cipher Modes of Operation % Methods and Techniques % NIST Special Publication 800-38A, 2001 Edition error(nargchk(4, 6, nargin)); validateattributes(s, {'struct'}, {}); validateattributes(oper, {'char'}, {}); validateattributes(mode, {'char'}, {}); validateattributes(input, {'numeric'}, {'real', 'vector', '>=', 0, '<', 256}); if (nargin >= 5) validateattributes(iv, {'numeric'}, {'real', 'vector', '>=', 0, '<', 256}); if (length(iv) ~= 16) error('Length of ''iv'' must be 16.'); end end if (nargin >= 6) validateattributes(sbit, {'numeric'}, {'real', 'scalar', '>=', 1, '<=', 128}); end if (mod(length(input), 16)) error('Length of ''input'' must be multiple of 16.'); end switch lower(oper) case {'encrypt', 'enc', 'e'} oper = 0; case {'decrypt', 'dec', 'd'} oper = 1; otherwise error('Bad ''oper'' parameter.'); end blocks = length(input)/16; input = input(:); switch lower(mode) case {'ecb'} % Electronic Codebook Mode % ------------------------ output = zeros(1,length(input)); idx = 1:16; for i = 1:blocks if (oper) % 解密 output(idx) = aesdecrypt(s,input(idx)); else % 加密 output(idx) = aesencrypt(s,input(idx)); end idx = idx + 16; end case {'cbc'} % Cipher Block Chaining Mode % -------------------------- if (nargin < 5) error('Missing initialization vector ''iv''.'); end output = zeros(1,length(input)); ob = iv; idx = 1:16; for i = 1:blocks if (oper) % 解密 in = input(idx); output(idx) = bitxor(ob(:), aesdecrypt(s,in)'); ob = in; else % 加密 ob = bitxor(ob(:), input(idx)); ob = aesencrypt(s, ob); output(idx) = ob; end idx = idx + 16; end % 储存用于块传递的iv s.iv = ob; case {'cfb'} % Cipher Feedback Mode if (nargin < 6) error('Missing ''sbit'' parameter.'); end % 读取输入明文/密文的长度 bitlen = 8*length(input); % 循环计数器 rounds = round(bitlen/sbit); % 检测错误 if (rem(bitlen, sbit)) error('Message length in bits is not multiple of ''sbit''.'); end % 将输入转换为比特流 inputb = reshape(de2bi(input,8,2,'left-msb')',1,bitlen); % 预设init ib = iv; ibb = reshape(de2bi(ib,8,2,'left-msb')',1,128); % 预设输出二进制流 outputb = zeros(size(inputb)); for i = 1:rounds iba = aesencrypt(s, ib); % 移位 ibab = reshape(de2bi(iba,8,2,'left-msb')',1,128); ibab = ibab(1:sbit); inpb = inputb((i - 1)*sbit + (1:sbit)); outb = bitxor(ibab, inpb); outputb((i - 1)*sbit + (1:sbit)) = outb; if (oper) % 解密 % 定义新移位方式 ibb = [ibb((1 + sbit):end) inpb]; else % 加密 ibb = [ibb((1 + sbit):end) outb]; end % 返回字节 ib = bi2de(vec2mat(ibb,8),'left-msb'); % 循环 end output = bi2de(vec2mat(outputb,8),'left-msb'); % 储存用于块传递的iv s.iv = ib; case {'ctr'} % Counter Mode if (nargin < 5) iv = 1; end output = zeros(1,length(input)); idx = 1:16; for i = (iv):(iv + blocks - 1) ib = AES_GET_COUNTER(i); ib = aesencrypt(s, ib); output(idx) = bitxor(ib(:), input(idx)); idx = idx + 16; end s.iv = iv + blocks; otherwise error('Bad ''oper'' parameter.'); end
1 matlab版本
2014a
2 参考文献
[1]韩纪庆,张磊,郑铁然.语音信号处理(第3版)[M].清华大学出版社,2019.
[2]柳若边.深度学习:语音识别技术实践[M].清华大学出版社,2019.
[3]葛秀梅,仲伟波,李忠梅,范东升.基于DSP的混沌语音加密解密系统[J].实验室研究与探索. 2014,33(09)