Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- //Prototype functions
- string SDESEncrypt(string plainText, string key, int currentRound);
- string SDESDecrypt(string cipherText, string key, int currentRound);
- string XOR(string inputOne, string inputTwo);
- string expansion(string input);
- //Defines the S-boxes needed for S-DES
- string s1box[2][8] = { "101", "010", "001", "110", "011", "100", "111", "000",
- "001", "100", "110", "010", "000", "111", "101", "011" };
- string s2box[2][8] = { "100", "000", "110", "101", "111", "001", "011", "010",
- "101", "011", "000", "111", "110", "010", "001", "100" };
- int main()
- {
- string plaintext = "100010110101";
- string key = "111000111";
- string encrypted;
- encrypted = SDESEncrypt(plaintext, key, 1);
- cout << "The encrypted text for round 1 is: " << encrypted << endl;
- encrypted = SDESEncrypt(encrypted, key, 2);
- cout << "The encrypted text for round 2 is: " << encrypted << endl;
- encrypted = SDESEncrypt(encrypted, key, 3);
- cout << "The encrypted text for round 3 is: " << encrypted << endl;
- encrypted = SDESEncrypt(encrypted, key, 4);
- cout << "The encrypted text for round 4 is: " << encrypted << endl;
- system("PAUSE");
- return 0;
- }
- string SDESEncrypt(string plainText, string key, int currentRound)
- {
- string leftZero, rightZero, leftOne, rightOne, encrypted, roundKey, sBoxResult;
- int row = 0, column = 0;
- //Calculates the value of the current rounds key
- for (int i = 0; i < 8; i++)
- roundKey += key[(currentRound + i - 1) % 8];
- //Splits the input into a left and right string
- for (int i = 0; i < 6; i++)
- leftZero += plainText[i];
- for (int i = 6; i < 12; i++)
- rightZero += plainText[i];
- //The left output is equal to the right half input
- leftOne = rightZero;
- //Expands the right half of the input
- rightOne = expansion(rightZero);
- //Xors the right half with the key
- rightOne = XOR(rightOne, roundKey);
- //S-Box Function for S-Box 1
- if (rightOne[0] == '1')
- row = 1;
- if (rightOne[1] == '1')
- column += 4;
- if (rightOne[2] == '1')
- column += 2;
- if (rightOne[3] == '1')
- column += 1;
- //S-box function for S-Box 2
- sBoxResult += s1box[row][column];
- row = 0, column = 0;
- if (rightOne[4] == '1')
- row = 1;
- if (rightOne[5] == '1')
- column += 4;
- if (rightOne[6] == '1')
- column += 2;
- if (rightOne[7] == '1')
- column += 1;
- sBoxResult += s2box[row][column];
- //XORs leftzero with the result of the S-Box function
- rightOne = XOR(sBoxResult, leftZero);
- //Puts left half and right half together
- encrypted = leftOne + rightOne;
- return(encrypted);
- }
- string SDESDecrypt(string cipherText, string key, int currentRound)
- {
- return("TEST");
- }
- //This function can XOR to binary numbers
- string XOR(string inputOne, string inputTwo)
- {
- for (int i = 0; i < inputOne.length(); i++)
- {
- if ((inputOne[i] == '1' && inputTwo[i] == '0') || (inputOne[i] == '0' && inputTwo[i] == '1'))
- inputOne[i] = '1';
- else
- inputOne[i] = '0';
- }
- return(inputOne);
- }
- //This is the expansion function from S-DES
- string expansion(string input)
- {
- string expanded;
- expanded += input[0];
- expanded += input[1];
- expanded += input[3];
- expanded += input[2];
- expanded += input[3];
- expanded += input[2];
- expanded += input[4];
- expanded += input[5];
- return(expanded);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement