ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【语音加密】基于matlab混沌+AES语音加密解密【含Matlab源码 1593期】

2021-12-13 20:33:19  阅读:208  来源: 互联网

标签:end idx 16 iv 源码 语音 sbit input 加密


一、混沌语音加密简介

语音的数据安全是网络语音通信的重要问题之一,混沌序列由于具有类随机性常被用作加密密钥。

二、部分源代码

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

三、运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1]韩纪庆,张磊,郑铁然.语音信号处理(第3版)[M].清华大学出版社,2019.
[2]柳若边.深度学习:语音识别技术实践[M].清华大学出版社,2019.
[3]葛秀梅,仲伟波,李忠梅,范东升.基于DSP的混沌语音加密解密系统[J].实验室研究与探索. 2014,33(09)

标签:end,idx,16,iv,源码,语音,sbit,input,加密
来源: https://blog.csdn.net/TIQCmatlab/article/details/121913153

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有