Advertisement
Guest User

S-DES Encryption

a guest
Nov 1st, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. //Prototype functions
  7. string SDESEncrypt(string plainText, string key, int currentRound);
  8. string SDESDecrypt(string cipherText, string key, int currentRound);
  9. string XOR(string inputOne, string inputTwo);
  10. string expansion(string input);
  11.  
  12.  
  13. //Defines the S-boxes needed for S-DES
  14. string s1box[2][8] = { "101", "010", "001", "110", "011", "100", "111", "000",
  15. "001", "100", "110", "010", "000", "111", "101", "011" };
  16.  
  17. string s2box[2][8] = { "100", "000", "110", "101", "111", "001", "011", "010",
  18. "101", "011", "000", "111", "110", "010", "001", "100" };
  19.  
  20.  
  21. int main()
  22. {
  23.     string plaintext = "100010110101";
  24.     string key = "111000111";
  25.     string encrypted;
  26.  
  27.     encrypted = SDESEncrypt(plaintext, key, 1);
  28.     cout << "The encrypted text for round 1 is: " << encrypted << endl;
  29.  
  30.     encrypted = SDESEncrypt(encrypted, key, 2);
  31.     cout << "The encrypted text for round 2 is: " << encrypted << endl;
  32.  
  33.     encrypted = SDESEncrypt(encrypted, key, 3);
  34.     cout << "The encrypted text for round 3 is: " << encrypted << endl;
  35.  
  36.     encrypted = SDESEncrypt(encrypted, key, 4);
  37.     cout << "The encrypted text for round 4 is: " << encrypted << endl;
  38.  
  39.     system("PAUSE");
  40.     return 0;
  41. }
  42.  
  43. string SDESEncrypt(string plainText, string key, int currentRound)
  44. {
  45.     string leftZero, rightZero, leftOne, rightOne, encrypted, roundKey, sBoxResult;
  46.     int row = 0, column = 0;
  47.     //Calculates the value of the current rounds key
  48.     for (int i = 0; i < 8; i++)
  49.         roundKey += key[(currentRound + i - 1) % 8];
  50.  
  51.     //Splits the input into a left and right string
  52.     for (int i = 0; i < 6; i++)
  53.         leftZero += plainText[i];
  54.  
  55.     for (int i = 6; i < 12; i++)
  56.         rightZero += plainText[i];
  57.  
  58.     //The left output is equal to the right half input
  59.     leftOne = rightZero;
  60.  
  61.     //Expands the right half of the input
  62.     rightOne = expansion(rightZero);
  63.  
  64.     //Xors the right half with the key
  65.     rightOne = XOR(rightOne, roundKey);
  66.    
  67.  
  68.     //S-Box Function for S-Box 1
  69.     if (rightOne[0] == '1')
  70.         row = 1;
  71.  
  72.     if (rightOne[1] == '1')
  73.         column += 4;
  74.     if (rightOne[2] == '1')
  75.         column += 2;
  76.     if (rightOne[3] == '1')
  77.         column += 1;
  78.  
  79.     //S-box function for S-Box 2
  80.     sBoxResult += s1box[row][column];
  81.  
  82.     row = 0, column = 0;
  83.  
  84.     if (rightOne[4] == '1')
  85.         row = 1;
  86.  
  87.     if (rightOne[5] == '1')
  88.         column += 4;
  89.     if (rightOne[6] == '1')
  90.         column += 2;
  91.     if (rightOne[7] == '1')
  92.         column += 1;
  93.  
  94.     sBoxResult += s2box[row][column];
  95.  
  96.     //XORs leftzero with the result of the S-Box function
  97.     rightOne = XOR(sBoxResult, leftZero);
  98.  
  99.     //Puts left half and right half together
  100.     encrypted = leftOne + rightOne;
  101.  
  102.     return(encrypted);
  103. }
  104.  
  105. string SDESDecrypt(string cipherText, string key, int currentRound)
  106. {
  107.     return("TEST");
  108. }
  109.  
  110. //This function can XOR to binary numbers
  111. string XOR(string inputOne, string inputTwo)
  112. {
  113.     for (int i = 0; i < inputOne.length(); i++)
  114.     {
  115.         if ((inputOne[i] == '1' && inputTwo[i] == '0') || (inputOne[i] == '0' && inputTwo[i] == '1'))
  116.             inputOne[i] = '1';
  117.  
  118.         else
  119.             inputOne[i] = '0';
  120.     }
  121.  
  122.     return(inputOne);
  123. }
  124.  
  125. //This is the expansion function from S-DES
  126. string expansion(string input)
  127. {
  128.     string expanded;
  129.     expanded += input[0];
  130.     expanded += input[1];
  131.     expanded += input[3];
  132.     expanded += input[2];
  133.     expanded += input[3];
  134.     expanded += input[2];
  135.     expanded += input[4];
  136.     expanded += input[5];
  137.  
  138.     return(expanded);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement