Des cipher algorithm

Source: Internet
Author: User
Tags decrypt

The code puts this aside for backup.

#include <iostream> #include <cstring>using namespace std;const static char s_box[8][4][16] = {//s box replacement//S1 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, one, ten, 9, 7, 3, ten, 5, 0,15,, 8, 2, 4, 9, 1, 7, 5, 3, 0, 13,//S2 6, 15, 1, 8, 14, 6, 11, 3 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, ten, 1, 3,, 4, 2, one, 6, 7,, 0, 5,, 9,//S3 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, 3, 5, 12,//S4 2, 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6 and 15, 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, 7, 2, 14,//S5 2, 124, 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, 9, 5, 6, 3, 0, 14,11, 8,, 7, 1,, 2, 6, 0, 9, 4, 3,//S6 5, 12, 1, 10, 15, 9, 2, 6, 8 3, 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, One, 6,4, 3, 2, ten, 9, 5, 1, 7, 6, 0, 8, 13,//S7 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,  Ten, 7, 9, 5, 0, 12,//S8 2, 3, 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8,  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};const static char ip[64] = {//p Displace 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, 3  7, 5, 48, 40, 16, 56, 24, fp[64, 7};const, +, (+), +--------- 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, 1 2,----------------------------------------- Onst static char ep[48] = {//expand displace 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};const static char sp[32] = {//p Displacement 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2,    8, 3, 9, 6, A, one, 4, 25};const static char pc1[56] = {//Displacement selection 1, 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, 2  3, 7, A, A, Wu, Wu, Si, 6, A,,,,,,,,,,, 5,,, and the static char pc2[48] = {//permutation selection 2 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20 , 2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, and 32};const s Tatic Char Loop[16]={1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};bool key[16][48];void bytetobit (char *in, BOOL *out, int bits)//byte in place {for (int i=0;i<bits;++i) out[i] = (in[i/8]>> (i%8)) &1;} void Bittobyte (bool *in, char *out, int bits)//bit to byte {memset (OUT,0,BITS/8); for (int i=0;i<bits;++i) OUT[I/8] |= in[i]&lt ;< (i%8);} void Transform (bool *in, bool *out, const char *table, int len)//permutation function {static bool temp[128];for (int i=0; i<len; ++i) t Emp[i] = in[table[i]-1];memcpy (Out,temp,len);} VOID Circulatemove (bool *in, int len,const char s)//loop left {static bool temp[128];memcpy (temp, in, Len); for (int i=0; i<l En ++i) in[i]=temp[(i+s)%len];} void Roundkey (char initkey[8])//calculate wheel key {static bool keybits[64], crypkeybits[56]; Bytetobit (Initkey, Keybits, 64); Transform (Keybits, Crypkeybits, PC1, and-Z); for (int i=0; i<16; ++i) {circulatemove (crypkeybits,, Loop[i]); Circulatemove (&crypkeybits[28], loop[i]); Transform (Crypkeybits, Key[i], PC2, 48);}} void Xor (bool *out, bool *in, int len)///XOR operation {for (int i=0; i<len; ++i) out[i] ^= in[i];} void S (bool in[48], BOOL out[32])//s box substitution {int x, Y;char c;for (int i=0; i<8; i++, in+=6) {x = in[0] * 2 + in[5];y = in[1] * 8 + in[2] * 4 + in[3] * 2 + in[4];c = s_box[i][x][y];out[4*i] = (c%2) &1;out[4*i+1] = ((C/2)%2) &1;out[4*i +2] = ((C/4)%2) &1;out[4*i+3] = ((C/8)%2) &1;}} void FE (bool in[64], BOOL thiskey[48])//wheel function {static bool temp[128],afterep[48];memcpy (TEMP,&AMP;IN[32],32); Transform (&in[32], Afterep, EP, 48); Xor (afterep,thiskey,48); S (afterep,&in[32]); Transform (&in[32], &in[32], SP, 32); Xor (&in[32], in, +); memcpy (in, temp, 32);} void FD (bool in[64], BOOL thiskey[48])//wheel function {static bool temp[128],afterep[48];memcpy (TEMP,IN,32); Transform (in, Afterep, EP, 48); Xor (afterep,thiskey,48); S (Afterep,in); Transform (in, in, SP, 32); Xor (in, &in[32], +); memcpy (&in[32], temp, 32);} void Encrypt (char in[8], char out[8])//encryption {static bool bits[64]; Bytetobit (in,bits,64); Transform (bits,bits,ip,64); for (int i=0; i<16; ++i) FE (Bits,key[i]); Transform (Bits, bits, FP, 64); Bittobyte (Bits, out, 64);} void Decrypt (char in[8], char out[8])//encryption {static bool bits[64]; Bytetobit (in,bits,64); Transform (bits,bits,ip,64); for (int i=15; i>=0; i--) FD (Bits,key[i]); Transform (Bits, bits, FP, 64); Bittobyte (Bits, out, 64);} int main () {char C;char initkey[9];cout<< "============= des cipher algorithm ==============" <<endl<<endl;cout << "Please set the 8-character key:" <<endl;cin>>initKey; RoundkeY (Initkey); Cin.get (c); char plaintext[256],cryptext[256],newplaintext[256];cout<<endl<< "Please enter the plaintext to be encrypted ( Support spaces and Chinese): "<<endl;cin.getline (plaintext,256); int L=strlen (plaintext), if (l%8!=0)//If the length is not a multiple of 8, fill with a space {int NEWL = 8* (l/8+1); for (int i=l;i < newl;i++) plaintext[i]= ";} for (int i=0;i<l;i+=8) Encrypt (plaintext+i,cryptext+i);cout<<endl<< "Your plaintext encryption results as follows:" <<endl;for ( int i=0;i<l;i++) cout<<cryptext[i];cout<<endl<< "-------------------------------------" < <endl;cout<< "Please enter the 8-bit key you previously set to decrypt:" <<endl;cin>>initKey; Roundkey (Initkey); for (int i=0;i<l;i+=8) Decrypt (cryptext+i,newplaintext+i);cout<<endl<< " After decrypting your ciphertext, the result is as follows: "<<endl;for (int i=0;i<l;i++) cout<<newplaintext[i];cout<<endl<<endl< < "des cipher algorithm complete! "<<endl;system" ("pause"); return 0;}


Des cipher algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.