DES algorithm in C++.
#include <iostream>
#include <string>
using namespace std;
// Array to hold the 16 keys
string round_keys[16];
string shift_left_once(string key_chunk){
string shifted="";
for(int i = 1; i < 28; i++){
shifted += key_chunk[i];
}
shifted += key_chunk[0];
return shifted;
}
string shift_left_twice(string key_chunk){
string shifted="";
for(int i = 0; i < 2; i++){
for(int j = 1; j < 28; j++){
shifted += key_chunk[j];
}
shifted += key_chunk[0];
key_chunk= shifted;
shifted ="";
}
return key_chunk;
}
void generate_keys(string key){
// The PC1 table
int pc1[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
};
// The PC2 table
int pc2[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
};
string perm_key ="";
for(int i = 0; i < 56; i++){
perm_key+= key[pc1[i]-1];
}
string left= perm_key.substr(0, 28);
string right= perm_key.substr(28, 28);
// Generating 16 keys
for(int i=0; i<16; i++){
if(i == 0 || i == 1 || i==8 || i==15 ){
left= shift_left_once(left);
right= shift_left_once(right);
}
else{
left= shift_left_twice(left);
right= shift_left_twice(right);
}
string combined_key = left + right;
string round_key = "";
for(int i = 0; i < 48; i++){
round_key += combined_key[pc2[i]-1];
}
round_keys[i] = round_key;
cout<<"Key "<<i+1<<": "<<round_keys[i]<<endl;
}
}
int main(){
string key = "10101010101110110000100100011000001001110011";
generate_keys(key);
}
OUTPUT
Key 1: 0000!010011001�100000110001011110010
Key 2: 0100101100000�!00000011011110110010
Key 3: 0000�10101101010010010100111010110101
Key 4: 1110�00010!0000011010110110110001
Key 5: 010�00110!011000101001111010011000101
Key 6: 11000000010!001�101001101001010110
Key 7: !11000100101010100011010111001100000
Key 8: 00010!111�000000101100000110010110
Key 9: !000100101101000100100110110011010
Key 10: 00000101101!00010�1000101010010111
Key 11: 010110100!0011100010101011111000101
Key 12: 1001011000001!�0100101101000001111011
Key 13: �001101!0001100000110101111000010011
Key 14: 0010000110�!100011100011000111000
Key 15: 001001001�00010010!1110101001010110
Key 16: 001!00000011001011100111100010010111
No comments:
Post a Comment
Please do not any spam link in Comment Box