日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DES密码实现( C语言 )

發布時間:2024/4/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DES密码实现( C语言 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 說明
  • 任務拆解
    • 子密鑰生成
    • 加/解密
  • 效果
  • 代碼
    • main.cpp 文件
    • Basic_Func_Lib.h頭文件

說明

1、先給家人們說一下,我這程序明文加密之后再解密不等于原明文,發出來是因為以后有時間會改一改,好找

2、在加解密過程中,所使用的置換表,S盒,P盒也是我在網上找的,除了IP表和IP逆表我驗證過沒問題,其他的表我就不能保證了

3、我這寫下來很流暢,除非是我對加解密的某一步理解出了問題,不然的話就有可能是我在網上找的這些表不合規范

4、對源碼有興趣的話,可以瞅瞅
鏈接:https://pan.baidu.com/s/1Eoeam_fZp3DcVAs4ZI56Ew
提取碼:nlvl

任務拆解

子密鑰生成

加/解密

效果

代碼

main.cpp 文件

#include "Basic_Func_Lib.h"void Generating_Subkeys();//子密鑰:Subkeys生成 void Encryption();//加密 void Decode();//解密int main() {//密鑰生成Generating_Subkeys();//加密Encryption();//解密Decode();return 0; }//子密鑰:Subkeys生成 void Generating_Subkeys() {Ke_transform();for (int i = 0; i < 16; i++) {Cyclic_Shift_Left(Cyclic_Shift_Table[i]);Compression_replacement(Subkeys[i]);} }//加密 void Encryption() {printf("明文為:\n");print_1(M, 64);//S盒初始化S_Box_Initial();//分割Split(M);//IP置換Equivalent_replacement(M);//加密for (int i = 0; i < 16; i++) {//函數f計算結果保存在Func_Ret中Func(i);//賦值Copy(Right, Left, 32);Copy(Func_Ret, Right, 32);}//合并Combination(C);//IP逆置換Inv_Equivalent_replacement(C);printf("加密結果為:\n");print_1(C, 64); }//解密 void Decode() {printf("密文為:\n");print_1(C, 64);//S盒初始化S_Box_Initial();//分割Split(C);//IP置換Equivalent_replacement(C);//解密for (int i = 15; i >= 0; i--) {//函數f計算結果保存在Func_Ret中Func(i);//賦值Copy(Left, Right, 32);Copy(Func_Ret, Left, 32);}//合并Combination(M);//IP逆置換Inv_Equivalent_replacement(M);printf("解密結果為:\n");print_1(M, 64); }

Basic_Func_Lib.h頭文件

#include <stdio.h> #include <stdlib.h> #define Row 16void Copy(int* src, int* dst, int n);//###############################數組打印 //一維數組打印函數 void print_1(int* array, int n) {for (int i = 0; i < n; i++) {printf("%d ", array[i]);}printf("\n"); }//二維數組打印函數 void print_2(int array[][Row], int n, int m) {for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {printf("%d ", array[i][j]);}printf("\n");} }//###############################輪密鑰生成基本函數 //初始密鑰 int Ke_Initial[64] = { 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0 }; //左 int Ke_Left_Index[28] = { 47, 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 }; int Ke_Left[28]; //右 int Ke_Right_Index[28] = { 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 }; int Ke_Right[28];//移位步長表 int Cyclic_Shift_Table[16] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };//壓縮置換表 int Compression_replacement_table[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 };//子密鑰存儲 int Subkeys[16][48];//除去密鑰中的奇偶校驗位,將密鑰分為兩28位密鑰 void Ke_transform() {for (int i = 0; i < 28; i++) {Ke_Left[i] = Ke_Initial[Ke_Left_Index[i] - 1];Ke_Right[i] = Ke_Initial[Ke_Right_Index[i] - 1];} }//密鑰左循環移位 void Cyclic_Shift_Left(int n) {int temp_L[2];int temp_R[2];for (int i = 0; i < n; i++) {temp_L[i] = Ke_Left[i];temp_R[i] = Ke_Right[i];}for (int i = n; i < 28; i++) {Ke_Left[i - n] = Ke_Left[i];Ke_Right[i - n] = Ke_Right[i];}for (int i = 0; i < n; i++) {Ke_Left[28 - n + i] = temp_L[i];Ke_Right[28 - n + i] = temp_L[i];} }//置換選擇 void Compression_replacement(int Subkey[48]) {int temp[56];for (int i = 0; i < 28; i++) {temp[i] = Ke_Left[i];temp[i + 28] = Ke_Right[i];}for (int i = 0; i < 48; i++) {Subkey[i] = temp[Compression_replacement_table[i] - 1];} }//###############################加/解密基本函數 //明文 int M[64] = { 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0 };//明密文置換中轉站 int Temp[64];//初始置換表 int Ip[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 }; int Left[32];//左 int Right[32];//右//擴展置換表 int Permutation_extension_table[48] = { 32, 8, 16, 24, 1, 9, 17, 25, 2, 10, 18, 26, 3, 11, 19, 27, 4, 12, 20, 28, 5, 13, 21, 29, 4, 12, 20, 28, 5, 13, 21, 29, 6, 14, 22, 30, 7, 15, 23, 31, 8, 16, 24, 32, 9, 17, 25, 1 }; int Permutation_extension_M[48];//擴展置換明文//S盒 int S_Box[4][16]; int S_Box_L0[16] = { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 }; int S_Box_L1[16] = { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 }; int S_Box_L2[16] = { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 }; int S_Box_L3[16] = { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 };int Func_Ret[32];//函數f處理結果暫存//P盒 int P_Box[32] = { 16,1,2,19,7,15,8,13,20,23,24,30,21,26,14,6,29,5,32,22,12,18,27,11,28,31,3,4,17,10,9,25 };//密文 int C[64];//Ip逆置換表 int Ip_Inv[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 };void S_Box_Initial() {Copy(S_Box_L0, S_Box[0], 16);Copy(S_Box_L1, S_Box[1], 16);Copy(S_Box_L2, S_Box[2], 16);Copy(S_Box_L3, S_Box[3], 16); }//IP置換函數 void Equivalent_replacement(int* M_C) {for (int i = 0; i < 64; i++) {Temp[i] = M_C[Ip[i] - 1];}Copy(Temp, M_C, 64);for (int i = 0; i < 32; i++) {Left[i] = M_C[Ip[i] - 1];Right[i] = M_C[Ip[i + 32] - 1];} }//分割函數 將輸入的數組分配給Left,Right void Split(int* M_C) {for (int i = 0; i < 32; i++) {Left[i] = M_C[i];Right[i] = M_C[i + 32];} }//置換擴展函數 void Extended_replacement() {for (int i = 0; i < 48; i++) {Permutation_extension_M[i] = Right[Permutation_extension_table[i] - 1];} }//異或 void Xor(int n) {for (int i = 0; i < 48; i++) {Permutation_extension_M[i] ^= Subkeys[n][i];} }//一個16進制數轉4位二進制數,存入Func_Ret void x_2_b(int x, int n) {int r;for (int i = 3; i >= 0; i--) {r = x % 2;x = (x - r) / 2;Func_Ret[i + n] = r;} }//S盒壓縮 一共需要七個S盒,為了簡單都用S_Box void S_box() {int L, R;for (int i = 0; i < 48; i += 6) {L = 2 * Permutation_extension_M[i] + Permutation_extension_M[i + 5];R = 8 * Permutation_extension_M[i + 1] + 4 * Permutation_extension_M[i + 2] + 2 * Permutation_extension_M[i + 3] + Permutation_extension_M[i + 4];x_2_b(S_Box[L][R], i);} }//P盒置換 void P_box() {Func_Ret;P_Box;for (int i = 0; i < 32; i++) {Func_Ret[i] = Func_Ret[P_Box[i] - 1];} }//函數f整合 void Func(int n) {Extended_replacement();Xor(n);S_box();P_box(); }//src賦值給dst void Copy(int* src, int* dst, int n) {for (int i = 0; i < n; i++) {dst[i] = src[i];} }//合并L、R void Combination(int* M_C) {for (int i = 0; i < 32; i++) {M_C[i] = Left[i];M_C[i + 32] = Right[i];} }//逆置換函數 void Inv_Equivalent_replacement(int* M_C) {for (int i = 0; i < 64; i++) {Temp[i] = M_C[Ip_Inv[i] - 1];}Copy(Temp, M_C, 64); }//交換M_L與M_R void Exchange() {int temp;for (int i = 0; i < 32; i++) {temp = Left[i] ^ Right[i];Left[i] ^= temp;Right[i] ^= temp;} }

總結

以上是生活随笔為你收集整理的DES密码实现( C语言 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。