DES的C++实现
来源:互联网 发布:windows已出现关键问题 编辑:程序博客网 时间:2024/06/05 03:02
/** * author: boyce * date: 2017-10-7 * description: DES */# include <iostream># include <cstring># include <string># include <vector># include <bitset>using namespace std;class DES {public: DES(){ this->setKey(string("12345678")); }; string encrypt(const string & text); string decrypt(const string & text); void setKey(string key) { this->_key = this->str2Bit(key); this->generateKey(); } void setKey(const bitset<64> & key) { this->_key = key; this->generateKey(); }private: //工具函数 bitset<64> str2Bit(string); string bit2Str(const bitset<64> &); bitset<48> XOR(const bitset<48> & a, const bitset<48> & b); bitset<32> XOR(const bitset<32> & a, const bitset<32> & b); void leftShift(bitset<56> & bitvec, int l, int r, int shift_bits); // transform函数 bitset<56> PC_1_trans(const bitset<64> &); // 将64位key的非校验位进行PC-1置换 bitset<48> PC_2_trans(const bitset<56> &); // 压缩置换 bitset<64> IP_trans(const bitset<64> & text); bitset<64> IP_I_trans(const bitset<64> & text); bitset<48> E_trans(const bitset<32> & text); bitset<32> P_trans(const bitset<32> & text); bitset<4> S_trans(const bitset<6> & text, int S_box_index); //核心方法 void generateKey(); bitset<32> feistel(const bitset<32> & R, int ki); bitset<64> _encrypt(const bitset<64> & text); bitset<64> _decrypt(const bitset<64> & text);private: bitset<64> _key; bitset<48> _subKey[16]; // ---------- IP置换 和 IP逆置换 ----------------------- static const int IP[64]; static const int IP_I[64]; // -------------- feistel 函数过程 ------------------- //E扩展 static const int E[48]; // S盒子 static const int S[8][4][16]; // P置换 static const int P[32]; // --------------- 子密钥生成 ---------------------- // 对K的56个非校验位的置换 static const int PC_1[56]; // 将56位的密钥进行压缩,得到48位子密钥 static const int PC_2[48];};//////////////////////////////////////////////////////////////////////////////////// -------------------------------- DES 方法实现 --------------------------// ===================== 核心流程 ============================// 生成子密钥void DES::generateKey() { bitset<56> LR = this->PC_1_trans(this->_key); for(int i = 1; i <= 16; i++) { if(i == 1 || i == 2 || i == 9 || i == 16) { this->leftShift(LR, 0, 27, 1); this->leftShift(LR, 28, 55, 1); } else { this->leftShift(LR, 0, 27, 2); this->leftShift(LR, 28, 55, 2); } this->_subKey[i - 1] = this->PC_2_trans(LR); }}// feistel 轮函数bitset<32> DES::feistel(const bitset<32> & R, int ki) { bitset<48> E_R = this->E_trans(R); E_R = this->XOR(E_R, this->_subKey[ki]); //划分成8小块 bitset<6> E_R_S[8]; bitset<4> ss; bitset<32> ret; for(int i = 0; i < 8; i++) for(int j = 0; j < 6; j++) E_R_S[i][j] = E_R[i*6 + j]; for(int i = 0; i < 8; i++){ ss = this->S_trans(E_R_S[i], i); for(int j = 0; j < 4; j++) ret[i*8 + j] = ss[j]; } return ret;}//对64位的明文进行加密bitset<64> DES::_encrypt(const bitset<64> & text) { // IP置换 bitset<64> ret = this->IP_trans(text); bitset<32> L, R; for(int i = 0; i < 32; i++) { L[i] = ret[i]; R[i] = ret[i + 32]; } // 16次T处理 for(int i = 0; i < 16; i++) { bitset<32> tmp = R; // R = this->XOR(L, this->feistel(R, i)); L = tmp; } // 合成R16L16 for(int i = 0; i < 32; i++){ ret[i] = R[i]; ret[i + 32] = L[i]; } // IP逆置换 ret = this->IP_I_trans(ret); return ret;}//对64位的密文进行加密bitset<64> DES::_decrypt(const bitset<64> & text) { bitset<64> ret = this->IP_trans(text); bitset<32> L, R; for(int i = 0; i < 32; i++) { L[i] = ret[i]; R[i] = ret[i + 32]; } for(int i = 15; i >= 0; i--) { bitset<32> tmp = R; R = this->XOR(L, this->feistel(R, i)); L = tmp; } for(int i = 0; i < 32; i++){ ret[i] = R[i]; ret[i + 32] = L[i]; } ret = this->IP_I_trans(ret); return ret;}// 对明文加密string DES::encrypt(const string & text) { string ret = ""; vector< bitset<64> > cipher_set; int length = text.size(); string str(8, ' '); int index = 0; for(int i = 0; i < length; i++) { if(i != 0 && i % 8 == 0) { cipher_set.push_back(this->str2Bit(str)); str = string(8, ' '); index = 0; } str[index++] = text[i]; } cipher_set.push_back(this->str2Bit(str)); int size = cipher_set.size(); for(int i = 0; i < size; i++) ret += this->bit2Str(this->_encrypt(cipher_set[i])); return ret;}//对密文解密string DES::decrypt(const string & text) { string ret = ""; vector< bitset<64> > cipher_set; int length = text.size(); string str(8, ' '); int index = 0; for(int i = 0; i < length; i++) { if(i != 0 && i % 8 == 0) { cipher_set.push_back(this->str2Bit(str)); str = string(8, ' '); index = 0; } str[index++] = text[i]; } cipher_set.push_back(this->str2Bit(str)); int size = cipher_set.size(); for(int i = 0; i < size; i++) { ret += this->bit2Str(this->_decrypt(cipher_set[i])); } return ret;}// ===================== 工具函数 ============================//64bit string to 64bit bitsetbitset<64> DES::str2Bit(string str) { bitset<64> ret; for(int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) { ret[i*8 + 7 - j] = ((str[i] >> j) & 1); } return ret;}// bitvec to strstring DES::bit2Str(const bitset<64> & bitvec) { string ret(8, '0'); for(int i = 0; i < 8; i++) { int n = 0; for(int j = 0; j < 8; j++) { n = n*2 + bitvec[i*8 + j]; } ret[i] = n; } return ret;}// 对 bitvec[l ... r]区间进行循环左移 shift_bits 次void DES::leftShift(bitset<56> & bitvec, int l, int r, int shift_bits) { for(int i = 0; i < shift_bits; i++) { int tmp = bitvec[l]; for(int j = l; j < r; j++) bitvec[j] = bitvec[j + 1]; bitvec[r] = tmp; }}// 32位异或操作bitset<32> DES::XOR(const bitset<32> & a, const bitset<32> & b) { bitset<32> ret; for(int i = 0; i < 32; i++) ret[i] = a[i] ^ b[i]; return ret;}// 48位异或操作bitset<48> DES::XOR(const bitset<48> & a, const bitset<48> & b) { bitset<48> ret; for(int i = 0; i < 48; i++) ret[i] = a[i] ^ b[i]; return ret;}// ================== transform 函数 ============================// 将64位key的非校验位进行PC-1置换bitset<56> DES::PC_1_trans(const bitset<64> & key) { bitset<56> ret; for(int i = 0; i < 56; i++) ret[i] = key[this->PC_1[i] - 1]; return ret;}//将56位key压缩置换成48keybitset<48> DES::PC_2_trans(const bitset<56> & LR) { bitset<48> ret; for(int i = 0; i < 48; i++) ret[i] = LR[this->PC_2[i] - 1]; return ret;}//IP置换bitset<64> DES::IP_trans(const bitset<64> & text) { bitset<64> ret; for(int i = 0; i < 64; i++) ret[i] = text[this->IP[i] - 1]; return ret;}//IP逆置换bitset<64> DES::IP_I_trans(const bitset<64> & text) { bitset<64> ret; for(int i = 0; i < 64; i++) ret[i] = text[this->IP_I[i] - 1]; return ret;}// E扩展32-48并置换bitset<48> DES::E_trans(const bitset<32> & text) { bitset<48> ret; for(int i = 0; i < 48; i++) ret[i] = text[this->E[i] - 1]; return ret;}//P置换bitset<32> DES::P_trans(const bitset<32> & text) { bitset<32> ret; for(int i = 0; i < 32; i++) ret[i] = text[this->P[i] - 1]; return ret;}/** * S盒子6-4处理 * param text [48位E扩展后的密文段] * param S_box_index [进入的S盒子的index, 0 ... 15] */bitset<4> DES::S_trans(const bitset<6> & text, int S_box_index) { bitset<4> ret; int r, c; r = text[0]*2 + text[5]; c = text[1]*8 + text[2]*4 + text[3]*2 + text[4]; int num = this->S[S_box_index][r][c]; for(int i = 3; i >= 0; i--) { ret[i] = (num & 1); num >>= 1; } return ret;}// ========================= DES 静态成员 =======================const int DES::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 }; const int DES::IP_I[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 };// -------------- feistel 函数过程 -------------------//E扩展const int DES::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 };// S盒子const int DES::S[8][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} }, { {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} }, { {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} }, { {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} }, { {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} }, { {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} }, { {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} }, { {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} } };// P置换const int DES::P[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 };// --------------- 子密钥生成 ---------------------- // 对K的56个非校验位的置换const int DES::PC_1[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 }; // 将56位的密钥进行压缩,得到48位子密钥const int DES::PC_2[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 };//////////////////////////////////////////////////////////////////////////////# include <fstream>int main() { DES des; des.setKey(string("abcdefgh")); fstream infile, outfile; infile.open("source_text.txt", ios::in); string source_text, buffer; while(getline(infile, buffer)) { source_text += buffer; source_text += "\n"; } infile.close(); string cipher = des.encrypt(source_text); outfile.open("cipher.txt", ios::out); outfile << cipher; outfile.close(); string decrypted_text; infile.open("cipher.txt", ios::in); while(getline(infile, buffer)) { source_text += buffer; source_text += "\n"; } infile.close(); decrypted_text = des.decrypt(cipher); outfile.open("decrypted_text.txt", ios::out); outfile << decrypted_text; outfile.close(); return 0;}
阅读全文
0 0
- 用C实现DES的加密
- DES加密算法的C语言实现
- DES加密算法的C语言实现
- 求des算法的c语言实现
- DES算法的C实现代码
- DES加密的C语言实现
- DES算法的c语言实现
- DES-C实现
- 标准DES-C实现
- DES(C语言实现)
- DES加密算法C实现
- DES加解密+C实现
- C语言实现DES算法
- c语言实现des加密
- c语言des加密算法实现
- DES算法-C/C++实现
- DES算法的介绍与C语言实现
- c语言实现记事本文件的 DES加密 解密
- 数据库中随机查询 10条数据
- 远程连接阿里云ECS服务器For Windows
- Java中字符不变性
- (POj 2230)Watchcow [有向欧拉图] 输出欧拉回路
- Java中使用poi导入、导出Excel
- DES的C++实现
- Javascript中bind()方法的使用与实现
- 自己写“扫雷”(一)入门MFC
- 【优化】对偶上升法(Dual Ascent)超简说明
- 图论练习1:银河英雄传说解题报告
- Python学习笔记
- sealed
- 软件开发培训
- 将assets文件中内容复制到SDCard中