DES加密實現(c語言)
阿新 • • 發佈:2019-02-09
這學期開了密碼學課,聽老師在課堂上講各種加密演算法各種高大上,但自high過後才發現別的先不說,單是DES自己看課本完全不懂。最後是在同學推薦下看了個視訊才弄懂了是怎樣一步步進行處理的,然後一時手癢就編了出來。雖然功能實現了,但程式的健壯性不是很好。目前程式是實現八位金鑰與八位明文進行加密處理,金鑰和明文從鍵盤讀入,多於八位會緩衝掉,但少於八位的我目前還沒想到怎麼處理,還有的就是實際上明文不應該有位數限制,但由於我能力有限,這些問題(說不定還有我沒發現的問題)都沒解決,各位路過的大大有解決方法的麻煩指導一下,不勝感激~~
好了,廢話不多說了,見程式碼吧~(ps.命名不是很規範,大家將就下)
#include<stdio.h> /*各種函式定義*/ int move_num(int r); void xor(int *r_b, int *k3, int large); void s_box(int r_t[32], int r_b[48]); void erzhuanshiliu(int *k, char *key, int large); void shizhuaner(int *k, int n, int i, int large); void round(int r, int k2[56], int k3[48], int r_s[32], int r_b[48], int l[32], int k_c_two[48], int m_c_two[32], int e[48], char c_r[8], char c_l[8]); void exchange(int *k1, int *k2, int *rule, int n); void key_move(int k2[56], int n); /*十進位制轉換成二進位制*/ void shizhuaner(int *k, int n, int i,int large) /* k是存放二進位制的陣列,n是需要進行轉換的十進位制數,i是第i個十進位制數,large是陣列大小*/ { int j; //計數的j,沒什麼大的含義 large = large / 8; j = large - 1; do { k[i * large + j] = n % 2; n = n / 2; j--; } while (n != 0); while (j >= 0) { k[i * large + j] = 0; j--; } return ; } /*二進位制轉換成十六進位制*/ void erzhuanshiliu(int *k,char *key, int large) /* k是二進位制陣列,key是存放十六進位制的陣列,large是k陣列的大小*/ { int i,j; //計數的i、j,沒什麼大的含義 large = large / 4; for (i = 0; i < large; i++) { j = k[i * 4] * 8 + k[i * 4 + 1] * 4 + k[i * 4 + 2] * 2 + k[i * 4 + 3]; if (j >= 10) { switch (j) { case 10:key[i] = 'a'; break; case 11:key[i] = 'b'; break; case 12:key[i] = 'c'; break; case 13:key[i] = 'd'; break; case 14:key[i] = 'e'; break; case 15:key[i] = 'f'; break; } } else { j = j + 48; key[i] = (char)j; } } return; } /*迴圈函式*/ void round(int r, int k2[56], int k3[48], int r_s[32], int r_b[48], int l[32], int k_c_two[48], int m_c_two[32], int e[48], char c_r[8], char c_l[8]) /* r是輪數*/ /* k2是金鑰經過置換1置換完的陣列,k3是金鑰經過置換2置換完的陣列*/ /* r_s是明文處理中R的32大小時的陣列,r_b是明文處理中R的48大小時的陣列,l是明文處理中L的陣列*/ /* k_c_two是金鑰處理中置換2的置換規則陣列,m_c_two是明文處理中置換2的置換規則陣列,e是明文處理中e盒的置換規則陣列*/ /* c_r是各輪密文的r部分,c_l是各輪密文的l部分*/ { int i, n, r_t[32],r_c[32]; //計數的i,沒什麼大的含義;n儲存該輪左移的數值;r_t,r_c存放中間處理數值的32大小的陣列 printf("這是第%d輪密文:\n", r); n = move_num(r); //獲取第r輪金鑰左移的位數 key_move(k2, n); //呼叫金鑰左移函式 exchange(k2, k3, k_c_two, 48); //金鑰進行置換2運算 exchange(r_s, r_b,e,48); //明文進行e盒擴充套件置換運算 xor(r_b, k3,48); //經過金鑰置換2運算後的子金鑰與從e盒出來的子明文異或 s_box(r_t,r_b); //呼叫明文處理中的s盒運算函式 exchange(r_t, r_c, m_c_two, 32); //明文進行置換2運算 for (i = 0; i < 32; i++) r_t[i] = r_c[i]; xor(r_t, l, 32); //經過處理後的R與L異或 for (i = 0; i < 32; i++) l[i] = r_s[i]; //準備下次迴圈的L for (i = 0; i < 32; i++) r_s[i] = r_t[i]; //準備下次迴圈的R erzhuanshiliu(r_s, c_r, 32); for (i = 0; i < 8; i++) printf("%c ", c_r[i]); //第r輪R部分的密文 erzhuanshiliu(l, c_l, 32); for (i = 0; i < 8; i++) printf("%c ", c_l[i]); //第r輪L部分的密文 printf("\n"); printf("\n"); return; } /* 異或函式 */ void xor(int *r_b, int *k3,int large) { int i; //計數的i,沒什麼大的含義 for (i = 0; i < large; i++) { if (r_b[i] != k3[i]) //兩數相同結果取0,兩數不同取1 r_b[i] = 1; else r_b[i] = 0; } } /* S盒運算函式 */ void s_box(int r_t[32],int r_b[48]) { int i,hang,lie,jie; //計數的i,沒什麼大的含義;hang是S盒的行下標;lie是S盒的列下標;jie是根據行、列下標查詢出的結果 /* S1~S8盒 */ int s1[4][16] = { { 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 } }, s2[4][16] = { { 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 } }, s3[4][16] = { { 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 } }, s4[4][16] = { { 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, 3 }, { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } }, s5[4][16] = { { 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 } }, s6[4][16] = { { 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 } }, s7[4][16] = { { 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 } }, s8[4][16] = { { 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 } }; for (i = 0; i < 8; i++) { hang = r_b[i * 6] * 2 + r_b[i * 6 + 5]; //第0位與第5位決定行下標 lie = r_b[i * 6 + 1] * 8 + r_b[i * 6 + 2] * 4 + r_b[i * 6 + 3] * 2 + r_b[i * 6 + 4]; //第1、2、3、4位決定列下標 switch (i) //第i個數查詢Si盒 { case 0:jie = s1[hang][lie]; break; case 1:jie = s2[hang][lie]; break; case 2:jie = s3[hang][lie]; break; case 3:jie = s4[hang][lie]; break; case 4:jie = s5[hang][lie]; break; case 5:jie = s6[hang][lie]; break; case 6:jie = s7[hang][lie]; break; case 7:jie = s8[hang][lie]; break; } shizhuaner(r_t, jie, i,32); //S盒為十六進位制,須轉換回二進位制進行存放 } } /*左移函式*/ void key_move(int k2[56], int n) { int i, one, two, twentyeight, twentynight; //計數的i,沒什麼大的含義;one、two、twentyeight、twentynight用於存放第1、2、28、29個數 one = k2[0]; //先記錄第1、2、28、29個數 two = k2[1]; twentyeight = k2[28]; twentynight = k2[29]; for (i = 0; i < (28 - n); i++) //迴圈賦值 { k2[i] = k2[i + n]; k2[i + 28] = k2[i + n + 28]; } if (n == 1) //左移1位時,特殊端點值(第27、55)賦值 { k2[27] = one; k2[55] = twentyeight; } else //左移2位時,特殊端點值(第27、28、54、55)賦值 { k2[26] = one; k2[27] = two; k2[54] = twentyeight; k2[55] = twentynight; } } /* 獲取第r輪左移位數 */ int move_num(int r) { int i; //i儲存移動的位數 switch (r) { case 1:i = 1; break; case 2:i = 1; break; case 3:i = 2; break; case 4:i = 2; break; case 5:i = 2; break; case 6:i = 2; break; case 7:i = 2; break; case 8:i = 2; break; case 9:i = 1; break; case 10:i = 2; break; case 11:i = 2; break; case 12:i = 2; break; case 13:i = 2; break; case 14:i = 2; break; case 15:i = 2; break; case 16:i = 1; break; } return i; //返回移動的位數 } /* 置換函式 */ void exchange(int *k1, int *k2, int *rule,int n) { int i, j; //計數的i,j for (i = 0; i < n; i++) { j = rule[i]; //根據rule陣列進行置換,也就是賦值 k2[i] = k1[j-1]; } return; } /*主函式*/ int main() { /* c是最終密文,c_r是密文的R部分,c_l是密文的L的部分,g是用於緩衝掉回車符,k接收從鍵盤輸入金鑰,m接收從鍵盤輸入的明文*/ char c[16],c_r[8],c_l[8], g, k[8], m[8]; /* 計數的i,j;r是輪數;n接收型別轉換後的結果;k1,k2,k3是金鑰處理中不同大小的陣列;m1,m2是明文處理中的陣列,r_s是R 32大小時的陣列,r_b是R 48大小時的陣列,l是L的陣列*/ int i, j, r, n, k1[64], k2[56], k3[48], m1[64], m2[64],r_s[32],r_b[48],l[32]; /* k_c_one是金鑰處理的置換1,k_c_two是金鑰處理的置換2,m_c_one是明文處理的置換1,e是明文處理的e盒,m_c_two是明文處理的置換2 */ int k_c_one[56] = { 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 }, k_c_two[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, 29, 32 }, m_c_one[64] = { 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 }, e[48] = { 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 }, m_c_two[32] = { 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 }, m_c_three[64] = { 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 }; printf("**DES加密**\n"); printf("\n"); printf("請輸入加密的金鑰:\n"); for (i = 0; i < 8; i++) //從鍵盤裡獲取輸入的金鑰 { scanf_s("%c", &k[i]); n = (int)k[i]; shizhuaner(k1, n, i,64); //進行金鑰進位制轉換 } g = getchar(); //緩衝掉多於8個字元以外字元和回車符 while (g != '\n') { g = getchar(); } printf("請輸入需要加密的明文:\n"); for (i = 0; i < 8; i++) //從鍵盤裡獲取輸入的明文 { scanf_s("%c", &m[i]); n = (int)m[i]; shizhuaner(m1, n, i,64); //進行明文進位制轉換 } g = getchar(); //緩衝掉多於8個字元以外字元和回車符 while (g != '\n') { g = getchar(); } printf("\n"); exchange(k1, k2, k_c_one, 56); //金鑰進行置換1處理 exchange(m1, m2, m_c_one, 64); //明文進行金鑰1處理 for (i = 0; i < 32; i++) l[i] = m2[i]; //明文的L部分 j = 0; for (i = 32; i < 64; i++) { r_s[j] = m2[i]; //明文的R部分 j++; } for (r = 1; r <= 16; r++) //進行16輪的迴圈處理 round(r,k2, k3, r_s, r_b, l, k_c_two, m_c_two, e, c_r, c_l); for (i = 0; i < 64; i++) //將密文R的部分與密文L的部分合一起 { m1[i] = r_s[i]; if (i >= 32) m1[i] = l[i - 32]; } exchange(m1, m2, m_c_three, 64); //進行明文處理的置換3 erzhuanshiliu(m2, c, 64); //二進位制轉換成十六進位制,得到最終密文 printf("這是最終密文:\n"); for (i = 0; i < 16; i++) printf("%c ", c[i]); printf("\n"); return 0; }
以下是測試程式碼和執行情況的截圖:
這是我第一次寫部落格(好緊張好刺激啊~~),請各位大大多多指教撒~~