%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%----------------------------- 主函数 -----------------------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function data_decrypt = DES_Decrypt(r_encrypt,key2,r_length)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 功能: DES解密
% 输入: r_encrypt 加密数据信号
% key2 解密密钥
% r_length 原始数据信号长度
% 输出: data_decrypt 解密后的数据信号
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%---------------- 变量赋初值 ----------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global irt endrt ert iprt pt
%%%%%%%--- DES加(解)密置换表 ----%%%%%%%
irt = ... % 明文初始置换表
[58 50 42 34 26 18 10 2 ...
60 52 44 36 28 20 12 4 ...
62 54 46 38 30 22 14 6 ...
64 56 48 40 32 24 16 8 ...
57 49 41 33 25 17 9 1 ...
59 51 43 35 27 19 11 3 ...
61 53 45 37 29 21 13 5 ...
63 55 47 39 31 23 15 7];
endrt = ... % 明文逆置换表
[40 8 48 16 56 24 64 32 ...
39 7 47 15 55 23 63 31 ...
38 6 46 14 54 22 62 30 ...
37 5 45 13 53 21 61 29 ...
36 4 44 12 52 20 60 28 ...
35 3 43 11 51 19 59 27 ...
34 2 42 10 50 18 58 26 ...
33 1 41 9 49 17 57 25];
ert = ... % 明文扩展置换表
[32 1 2 3 4 5 4 5 ...
6 7 8 9 8 9 10 11 ...
12 13 12 13 14 15 16 17 ...
16 17 18 19 20 21 20 21 ...
22 23 24 25 24 25 26 27 ...
28 29 28 29 30 31 32 1];
iprt = ... % 密钥初始置换表
[57 49 41 33 25 17 9 1 ...
58 50 42 34 26 18 10 2 ...
59 51 43 35 27 19 11 3 ...
60 52 44 36 63 55 47 39 ...
31 23 15 7 62 54 46 38 ...
30 22 14 6 61 53 45 37 ...
29 21 13 5 28 20 12 4];
pt = ... % P置换表
[16 7 20 21 29 12 28 17 ...
1 15 23 26 5 18 31 10 ...
2 8 24 14 32 27 3 9 ...
19 13 30 6 22 11 4 25];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%----------------- DES解密 -----------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%--- 读取需要解密的数据 ---%%%%%%%%%
data = r_encrypt; % 提取需要解密的数据
data_length = length(data); % 求取字符长度
adds = 64-mod(data_length,64); % 求取需要补充的字符数
if adds ~= 0
for i = 1:adds
data(data_length+i) = 0; % 不足64位的分组数据后面补零
end
data_length1 = data_length+adds; % 补零后的位数
end
temp = data;
data = [];
for i = 1:data_length1
data = [data,dec2bin(temp(i))]; % 将双精度二进制数据转换为字符串格式
end
%%%%%%%%%%%%---- 读取密钥 -----%%%%%%%%%%%%
key = key2; % 提取密钥字符
keylength = length(key); % 求取字符长度
keytemp0 = uint8(key); % 将字符转换为八位二进制整型
keys = [149 57 208 147 21 183 27]; % 密钥不足7位时的补充密钥
%%%%%%%%%%--- 将密钥调整为7位 ---%%%%%%%%%%
if keylength < 7 % 延长小于七位的密钥
for i = keylength+1:7
keytemp0(i) = keys(i);
end
end
for i=1:7 % 缩短大于七位的密钥
keyusetemp(i) = keytemp0(i);
end
keyuse = char(keyusetemp); % 将密钥转换字符型数据
%%%%%%%%%%--- 形成16轮子密钥 ---%%%%%%%%%%%
pwb = str2bin(keyuse,7,2); % 密钥转二进制,并加入偶校验位
pwb = rebit(pwb,iprt); % 密钥初始置换
ki = gerkey(pwb); % 计算16轮子密钥
ki = flipud(ki); % 对16轮子密钥进行倒置
%%%%%%%%%%%--- 解密文件数据 ---%%%%%%%%%%%%
times = data_length1/64; % 分组解密的组数
for i = 0:times-1
for j = 1:64
tempdata(j) = data(64*i+j); % 提取第i组数据
end
decrydata = des(tempdata,ki); % 调用des函数利用密钥ki对tempdata解密
data_decrypt(i*64+1:(i+1)*64) = decrydata;
end
temp = data_decrypt;
data_decrypt = zeros(1,r_length); % 解密后的二进制数据(双精度格式)
for i = 1:r_length
if temp(i) == '1'
data_decrypt(i) = 1;
else
data_decrypt(i) = 0;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%----------------------------- 子函数 -----------------------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%-------------- 子函数str2bin --------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function bin = str2bin(str,k,flag)
% 功能:字符串转二进制串,字符串中每个字符对应8位二进制字符
% 输入:str 字符向量 ASCII字符串
% k 正整数 参与每次奇偶校验位运算的bit位数
% flag 正整数 用以说明是否在二进制串中每隔k位加一位奇偶校验位,0表示不加,1表示加奇校验位,2表示加偶校验位
% 输出:bin 字符向量 二进制字符串
% 说明:当length(str)*8 mod k!=0时,对bin二进制串最后length(str)*8 mod k 位补零到k位后再做奇校验
l = length(str);
bin = [];
temp = [];
for x = 1:l
temp = [temp,dec2bin(str(x),8)]; % 将字符串转换为二进制串
end
if flag ~= 0 % 判断是否需要加奇偶校验位
n = ceil(l*8/k); % 校验位总位数
rb = mod(l*8,k);
sb = 0;
if rb ~= 0
sb = k-rb; % 算出当二进制串做奇偶校验位时,在串尾补零的个数
for i = 1:sb
z(i) = '0';
end
temp = [temp z];
end
for x = 0:n-1
temp1 = temp(x*k+1:(x+1)*k);
lone = length(find(temp1 == '1')); % 求k位二进制串中1的个数
if flag == 1 % 计算奇校验位值
if mod(lone,2) == 0
opb = '1';
else
opb = '0';
end
else if flag == 2 % 计算偶校验位值
if mod(lone,2) == 0
opb = '0';
else
opb = '1';
end
end
end
temp1 = [temp1 opb]; % 添加校验位
bin = [bin temp1];
end
else
bin = temp;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%--------------- 子函数rebit ---------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function so = rebit(si,k)
%功能:将si中的位根据置换表k进行置换
%输入:si 一维字符类型向量 要做位置换处理的串
% k 一维字符类型向量 置换表
%输出:so 一维字符类型向量 si置换后的结果
lk=length(k);
for i=1:lk
so(i)=si(k(i));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%--------------- 子函数gerkey ---------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ki=gerkey(k)
%功能:实现des加密过程中从56位的K密钥产生16个48位Ki子密钥
%输入:k 56位长的行向量 存储56位主密钥K
% mt 矩阵 大小2*16 每轮循环左移位数表
% eg.
% mt=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16;1 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1];
% 移位表第一行为轮数,第二行为第一行对应轮数左移的位数
% rt 一维向量 长度48 置换表
%输出:ki 16*48的矩阵存储16个48位子密钥,ki的第一行代表第一个子密钥,第二行代表第二个子密钥......
mt = ... % 移位表(第一行为轮数,第二行为第一行对应轮数左移的位数)
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16;
1 1 2 2 2 2 2 2 1 2 2 2 2 2 2 1];
rt = ... % 56位到48位压缩置换表
[14 17 11 24 1 5 3 28 ...
15 6 21 10 23 19 12 4 ...
26 8 16 7 27 20 13 2 ...
41 52 31 37 47 55 30 40 ...
51 45 33 48 44 49 39 56 ...
34 53 46 42 50 36
没有合适的资源?快使用搜索试试~ 我知道了~
【数字信号加密解密】基于matlab DES二进制数字信号加密解密【含Matlab源码 3785期】.zip
共4个文件
m:3个
jpg:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 132 浏览量
2024-01-15
18:07:58
上传
评论
收藏 35KB ZIP 举报
温馨提示
CSDN海神之光上传的代码均可运行,亲测可用,直接替换数据即可,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描博客文章底部QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信
资源推荐
资源详情
资源评论
收起资源包目录
【数字信号加密解密】基于matlab DES二进制数字信号加密解密【含Matlab源码 3785期】.zip (4个子文件)
【数字信号加密解密】基于matlab DES二进制数字信号加密解密【含Matlab源码 3785期】
运行结果1.jpg 36KB
DES_Decrypt.m 13KB
main.m 2KB
DES_Encrypt.m 13KB
共 4 条
- 1
资源评论
海神之光
- 粉丝: 5w+
- 资源: 6110
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功