%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%----------------------------- 主函数 -----------------------------%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
海神之光
- 粉丝: 5w+
- 资源: 6476
最新资源
- 1Cr18Ni9Ti钢板焊接接头冷弯开裂的试验研究.pdf
- 1Cr18Ni9Ti钎焊接头组织和性能影响研究 - .pdf
- 2.25 Cr-1 Mo钢制加氢反应器的焊接裂纹(二)-对焊接裂纹性质的试验研究.pdf
- 1Crl8Ni9Ti不锈钢复合管系的焊接 - .pdf
- 2.25Cr-1Mo-0.25V低合金钢用焊接材料的检验 - .pdf
- 2.25-1Mo钢焊后产生焊接裂纹的原因分析及其返修措施.pdf
- 2.25 Cr-1 Mo钢制加氢反应器的焊接裂纹(一)-在制过程中出现的焊接裂纹.pdf
- 2.25Cr-1Mo-0.25V钢焊接材料的再热裂纹敏感性试验方法探讨.pdf
- 2A12厚板铝合金搅拌摩擦焊接头组织性能研究 - .pdf
- 2万立立式液氢高压容器的焊接研制.pdf
- 2A14铝合金法兰焊接裂纹修复.pdf
- 3.5%Ni钢焊接接头的低温韧性研究三──熔敷金属碳含量等因素的影响.pdf
- 3.5%Ni钢焊接技术的最近进展.pdf
- 3.5%Ni钢焊接接头低温韧性的研究二──焊后热处理母材及焊材的影响.pdf
- 3.5%Ni钢焊接接头低温韧性的研究一──线能量层间温度的影响.pdf
- 3.5Ni钢低温设备的制造和焊接.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈