C 實現DES加密 演算法

2021-08-20 21:00:32 字數 3096 閱讀 8760

我的第一篇部落格就是:c語言實現des加密

今天重寫的時候發現好像**傳錯了,有些尷尬,其中有各種奇怪的問題…..

問題1.好多表,包括s盒之類的寫成char型別了,應該寫int型別才對

問題2.好多變數沒宣告就用了

問題3.為啥我當時要寫這麼多static???

問題4.沒標頭檔案,**全擠在cpp裡面了

更新修復下問題:

test.h

#include 

using

namespace

std;

class des_key ;

void chartobit(bool *output, const

char *input); // 位元組組轉換成位組

void rotatel(bool *input,int len);

void transform(bool *output, bool *input, const

int *table, int tablelen);

void xor(bool *inputa, const

bool *inputb, int len);

void des_run(char *mingwen,des_key des_key); //核心加密演算法

void f_func(bool *r, bool *key); //f函式

void s_func(bool *output, bool *input); //s函式

void showinhex(const

bool input[64]); //把密文顯示為16進製制

//置換ip表

const

static

int ip_table[64] = ;

//逆置換ip-1表

const

static

int ipr_table[64] = ;

//e位選擇表(擴充套件置換表)

static

const

int e_table[48] = ;

//p換位表(單純換位表)

const

static

int p_table[32] = ;

//pc1選位表(金鑰生成置換表1)

const

static

int pc1_table[56] = ;

//pc2選位表(金鑰生成置換表2)

const

static

int pc2_table[48] = ;

//左移位數表

const

static

int loop_table[16] = ;

// s盒

const

static

int s_box[8][4][16] = ;

test.cpp

#include "test.h"

int main() ;

cout

<< "plz input char_key[8]"

<< endl;

cin >> char_key;

char char_mingwen[9] = ;

cout

<< "plz input char_mingwen[8]"

<< endl;

cin >> char_mingwen;

//******************input********************

des_key input_key(char_key);

des_run(char_mingwen,input_key);

system("pause");

return0;}

des_key::des_key(char char_key[8])

}void xor(bool *inputa, const

bool *inputb, int len)

}void chartobit(bool *output, const

char *input)

}void transform(bool *output, bool *input, const

int *table, int tablelen)

memcpy(output, temp, tablelen);

}void rotatel(bool *input, int len)

void s_func(bool *output, bool *input)

}}void f_func(bool *r, bool *key)

void des_run(char *mingwen, des_key des_key)

bool temp[32];

memcpy(temp, r0, 32);

memcpy(r0, l0, 32);

memcpy(l0, temp, 32);

transform(bit_mingwen, bit_mingwen, ipr_table, 64);

showinhex(bit_mingwen);

}void showinhex(const

bool input[64]) ;

for (int i = 0; i<16; i++)

for (int i = 0; i<16; i++)

cout

<< endl;

}

windows 10 + vs2017編譯通過,測試完全沒問題

測試:

秘鑰:「security」

明文:「computer」

加密後的密碼:「3b6c72b2710eb513」

DES加密演算法

命名空間 system.security.cryptography 定義訪問資料加密標準 des 演算法的加密服務提供程式 csp 版本的包裝物件。無法繼承此類。測試1加密頁面 protected void page load object sender,eventargs e response.r...

DES加密演算法

include include include include using namespace std const static char ip table 64 初始置換 const static char ipr table 64 初始逆置換表 static const char extensi...

對稱加密演算法 DES加密演算法

一 對稱加密演算法 對稱加密也稱為常規加密 私鑰或單鑰加密。乙個對稱加密由5部分組成 明文 plaintext 這是原始資訊或資料,作為演算法的輸入。加密演算法 encryption algorithm 加密演算法對明文進行各種替換和轉換。金鑰 secret key 金鑰也是演算法的輸入。演算法進行...