Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1.  
  2. // minides.cpp
  3.  
  4. // Fall 2015
  5.  
  6. #include<iostream>
  7. #include<string.h>
  8. #include<stdlib.h>
  9.  
  10. using namespace std;
  11.  
  12. // Public Variables
  13. int plain[12];
  14. int key[9];
  15. int xorx[8];
  16. int f[6]={0};
  17. int round = 0;
  18. int currKey[8];
  19.  
  20. void encipher();
  21. void decipher();
  22. void sBoxes();
  23. void getKey();
  24.  
  25. int main(int argc, char* argv[])
  26. {
  27. // error
  28. if( argc != 2)
  29. {
  30. cout << "wrong number of arguments. Input should look like this: " << endl;
  31. cout << "cat key.txt message.txt | ./minides -[e/d] > output.txt" << endl;
  32. return 0;
  33. }
  34.  
  35. // get input
  36. char message[12];
  37. char charKey[9];
  38.  
  39. cin >> charKey;
  40. cin >> message;
  41.  
  42. // take message from input
  43. for (int j = 0; j < 12; j++)
  44. plain[j] = message[j]-'0';
  45. // take key from input
  46. for (int i = 0; i < 12; i++)
  47. key[i] = charKey[i]-'0';
  48.  
  49. // encipher/decipher flag
  50. switch (argv[1][1]) {
  51. case 'e':
  52. encipher();
  53. encipher();
  54. encipher();
  55. encipher();
  56. for ( int p = 0; p < 12; p++)
  57. cout << plain[p];
  58. break;
  59.  
  60. case 'd':
  61. round = 3;
  62. decipher();
  63. decipher();
  64. decipher();
  65. decipher();
  66. for ( int p = 0; p < 12; p++)
  67. cout << plain[p];
  68. break;
  69. }
  70. return 0;
  71. }
  72.  
  73.  
  74. void encipher()
  75. {
  76. // Variables
  77. int R[6];
  78. int L[6];
  79. int expR[8];
  80. int lorf[6];
  81.  
  82. // put inputs in R, L, key
  83. for (int i = 0; i < 6; i++)
  84. {
  85. L[i] = plain[i];
  86. R[i] = plain[i+6];
  87. }
  88.  
  89. // Expand R
  90. expR[0] = R[0];
  91. expR[1] = R[1];
  92. expR[2] = R[3];
  93. expR[3] = R[2];
  94. expR[4] = R[3];
  95. expR[5] = R[2];
  96. expR[6] = R[4];
  97. expR[7] = R[5];
  98.  
  99. getKey();
  100.  
  101. // E(R) xor K1
  102. for(int j=0; j<8; j++)
  103. {
  104. xorx[j] = expR[j] + currKey[j];
  105. if(xorx[j] == 2)
  106. xorx[j] = 0;
  107. }
  108.  
  109. // Send xorx through sBoxes
  110. sBoxes();
  111.  
  112. // L0 xor f
  113. for (int m = 0; m < 6; m++)
  114. {
  115. lorf[m] = L[m] + f[m];
  116. if (lorf[m] == 2)
  117. lorf[m] = 0;
  118. }
  119.  
  120. // put L0 xor f with R0 to get the cipher
  121. for (int n = 0; n < 6; n++)
  122. {
  123. plain[n] = R[n];
  124. plain[n + 6] = lorf[n];
  125. }
  126.  
  127. round++;
  128. }
  129.  
  130.  
  131.  
  132. void decipher() {
  133. // Variables
  134. int R[6];
  135. int L[6];
  136. int expR[8];
  137. int lorf[6];
  138.  
  139. // put inputs in R, L, key
  140. for (int i = 0; i < 6; i++)
  141. {
  142. R[i] = plain[i];
  143. L[i] = plain[i+6];
  144. }
  145.  
  146. // Expand R
  147. expR[0] = R[0];
  148. expR[1] = R[1];
  149. expR[2] = R[3];
  150. expR[3] = R[2];
  151. expR[4] = R[3];
  152. expR[5] = R[2];
  153. expR[6] = R[4];
  154. expR[7] = R[5];
  155.  
  156. getKey();
  157.  
  158. // E(R) xor K1
  159. for(int j=0; j<8; j++)
  160. {
  161. xorx[j] = expR[j] + currKey[j];
  162. if(xorx[j] == 2)
  163. xorx[j] = 0;
  164. }
  165.  
  166. // Send xorx through sBoxes
  167. sBoxes();
  168.  
  169. // L0 xor f
  170. for (int m = 0; m < 6; m++)
  171. {
  172. lorf[m] = L[m] + f[m];
  173. if (lorf[m] == 2)
  174. lorf[m] = 0;
  175. }
  176.  
  177. // put L0 xor f with R0 to get the cipher
  178. for (int n = 0; n < 6; n++)
  179. {
  180. plain[n] = lorf[n];
  181. plain[n + 6] = R[n];
  182. }
  183.  
  184. round--;
  185. }
  186.  
  187. void sBoxes() // puts the xorx function through the S boxes to get the function f(L2,K)
  188. {
  189. int s1[16][3]={0};
  190. for ( int i1 = 0; i1 < 16; i1++) {
  191. if ( i1==0 || i1==3 || i1==5 || i1==6 || i1==9 || i1==10 || i1==13 || i1==14)
  192. s1[i1][0]=1;
  193. else
  194. s1[i1][0]=0;
  195. }
  196. for ( int i2 = 0; i2 < 16; i2++) {
  197. if ( i2==1 || i2==3 || i2==4 || i2==6 || i2==10 || i2==11 || i2==13 || i2==15)
  198. s1[i2][1]=1;
  199. else
  200. s1[i2][1]=0;
  201. }
  202. for ( int i3 = 0; i3 < 16; i3++) {
  203. if ( i3==0 || i3==2 || i3==4 || i3==6 || i3==8 || i3==13 || i3==14 || i3==15)
  204. s1[i3][2]=1;
  205. else
  206. s1[i3][2]=0;
  207. }
  208.  
  209. int s2[16][3];
  210. for ( int i4 = 0; i4 < 16; i4++) {
  211. if ( i4==0 || i4==2 || i4==3 || i4==4 || i4==8 || i4==11 || i4==12 || i4==15)
  212. s2[i4][0]=1;
  213. else
  214. s2[i4][0]=0;
  215. }
  216. for ( int i5 = 0; i5 < 16; i5++) {
  217. if ( i5==2 || i5==4 || i5==6 || i5==7 || i5==9 || i5==11 || i5==12 || i5==13)
  218. s2[i5][1]=1;
  219. else
  220. s2[i5][1]=0;
  221. }
  222. for ( int i6 = 0; i6 < 16; i6++) {
  223. if ( i6==3 || i6==4 || i6==5 || i6==6 || i6==8 || i6==9 || i6==11 || i6==14)
  224. s2[i6][2]=1;
  225. else
  226. s2[i6][2]=0;
  227. }
  228.  
  229. int index1=0;
  230. int index2=0;
  231. index1 = 8*xorx[0]+4*xorx[1]+2*xorx[2]+xorx[3];
  232. index2 = 8*xorx[4]+4*xorx[5]+2*xorx[6]+xorx[7];
  233.  
  234. f[0] = s1[index1][0];
  235. f[1] = s1[index1][1];
  236. f[2] = s1[index1][2];
  237. f[3] = s2[index2][0];
  238. f[4] = s2[index2][1];
  239. f[5] = s2[index2][2];
  240. }
  241.  
  242. void getKey()
  243. {
  244. for(int i =0; i < 8; i++)
  245. {
  246. currKey[i]= key[(i + round) % 9];
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement