DES算法的matlab实现
生活随笔
收集整理的這篇文章主要介紹了
DES算法的matlab实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
主函數
keyword = 'AABBCCDD'; % 密碼,8位ascii碼 text = 'woleilee'; % 明文8位ascii碼 bin_text = convert_mess2bin(text); keys = generate_keys(keyword); % 計算加密密鑰 bin_result = DES(bin_text,keys); result = convert_bin2mess(bin_result); % 密文 disp(['加密后的密文為:',result]); de_keys = flipud(keys); % 解密密鑰 bin_de_result = DES(bin_result,de_keys); % 解密后的二進制 de_result = convert_bin2mess(bin_de_result); % 解密 disp(['解密后的密文為:',de_result]); if strcmp(text,de_result)==1disp('DES加密后解密成功!'); elsedisp('DES加密后解密失敗!'); end接下來幾個是函數
DES.m
%%該代碼僅僅用于信息網絡安全技術的學習%%function [ ciphertext ] = DES( ciphertext,keys ) % 對稱密碼算法 分組加密 % ciphertext 待加密明文 % key 密鑰 % ciphertext 加密以后的密文 % 按照字符串處理ciphertext_ip = transform( ciphertext ); % 進行IP置換 text = ciphertext_ip; Ltext = text(:,1:1:32); % 左半部分 Rtext = text(:,33:1:64); % 右半部分 for i=1:16 % 16輪結構變換NextL = Rtext; % 下一輪的L是上一輪的Rnow_key = keys(i,:); % 這一輪加密的密鑰Rtext_E = Entend_box(Rtext); % 32位數據放入E盒擴展成48位for j=1:48Rtext_E(j)=dec2bin(xor(bin2dec(Rtext_E(j)),bin2dec(now_key(j)))); % 與密鑰異或處理endRtext = Substitute_box(Rtext_E); % S盒壓縮Rtext = P_box(Rtext) ; % 對S盒輸出進行IP置換 for j=1:32Rtext(j)=dec2bin(xor(bin2dec(Rtext(j)),bin2dec(Ltext(j)))); % 與上一輪L異或處理endLtext = NextL;end% text = [Ltext,Rtext]; 這樣是錯的text = [Rtext,Ltext];ciphertext = anti_transform(text); endfunction [ ciphertext_ip ] = transform( ciphertext ) % 對明文進行IP置換 % ciphertext 明文 % ciphertext_ip IP置換后的明文 ciphertext_ip = blanks(64); % 預分配內存64bit IP_table = [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]; % IP置換表 for i=1:64ciphertext_ip(i)=ciphertext(IP_table(i)); end endfunction [Rtext_E] = Entend_box(Rtext) % 對右半部分擴展成48位便于異或操作 % Rtext 待擴展數據 % Rtext_E E擴展后的數據Rtext_E = blanks(48); % 預分配內存48bit ex_tab = [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]; % E擴展表 for i=1:48Rtext_E(i)=Rtext(ex_tab(i)); % 32位擴展成48位 end endfunction [Rtext] = Substitute_box(Rtext_E) % S盒壓縮成32位 stab(:,:,1)=[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7;0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8;4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0;15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]; stab(:,:,2)=[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10;3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5;0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15;13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]; stab(:,:,3)=[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8;13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1;13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7;1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]; stab(:,:,4)=[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15;13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9;10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4;3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14]; stab(:,:,5)=[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9;14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6;4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14;11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3]; stab(:,:,6)=[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11;10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8;9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6;4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13]; stab(:,:,7)=[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1;13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6;1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2;6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]; stab(:,:,8)=[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7;1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2;7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8;2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]; % 8個S盒的表 Rtext=''; for i=1:8s = stab(:,:,i);str = Rtext_E(:,i*6-5:i*6);m = bin2dec([str(1),str(6)]); % S盒的行列計數都是從0開始n = bin2dec(str(:,2:5)); temp = dec2base(s(m+1,n+1),2,4); % s盒第m行n列,4位二進制Rtext = [Rtext,temp]; end endfunction [ stext_ip ] = P_box( stext ) % 對S盒的輸出進行IP置換 % ciphertext 明文 % ciphertext_ip IP置換后的明文 stext_ip = blanks(32); % 預分配內存32bit IP_table = [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]; % IP置換表for i=1:32stext_ip(i)=stext(IP_table(i)); end endfunction [ text_ip ] = anti_transform( text ) % 對最終結果進行IP逆置換 text_ip = blanks(64); % 預分配內存64bit IP_table = [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,];% IP置換表 for i=1:64text_ip(i)=text(IP_table(i)); end endgenerate_keys.m
function [ keys ] = generate_keys( keyword ) % 根據密鑰生成16輪子密鑰if length(keyword)~=8disp(['給個八位的密碼行不行啊!!!'])returnendbinkwd='';bin_keyword = dec2base(keyword,2,8); % 轉8位二進制for i=1:8binkwd = [binkwd,bin_keyword(i,:)];endbkwd = PC_1(binkwd); % pc-1打亂次序mov_bit=[1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1]; % 每一輪循環移位的位數C0 = bkwd(1:28);D0 = bkwd(29:56);for i=1:16C0=[C0(1+mov_bit(i):end),C0(1:mov_bit(i))];D0=[D0(1+mov_bit(i):end),D0(1:mov_bit(i))]; % 循環移位bfkey = [C0,D0];keys(i,:)=PC_2(bfkey);end endfunction [Bkwd]=PC_1(bkwd) % PC_1置換 PC1_tab = [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,64,53,45,37,29,21,13, 5,28,20,12,4]; for i=1:56Bkwd(i)=bkwd(PC1_tab(i)); end end function [key]=PC_2(bfkey) % PC_2置換 PC2_tab = [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,29,32]; for i=1:48key(i)=bfkey(PC2_tab(i)); end endconvert_mess2bin.m
function [ text ] = convert_mess2bin( mess ) % 把明文轉成二進制if length(mess)~=8disp(['給個八位的明文行不行啊!!!'])returnendtext='';mess = dec2base(mess,2,8); % 轉8位二進制for i=1:8text = [text,mess(i,:)];end endconvert_bin2mess
function [ mess ] = convert_bin2mess( binn )mess = '';for i=1:8a =(i-1)*8+1;str = binn(a:a+7);mess = [mess,char(bin2dec(str))];end end總結
以上是生活随笔為你收集整理的DES算法的matlab实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从多个Word文档中批量取值,整理到Ex
- 下一篇: 【老生谈算法】matlab实现模糊K-均