fork download
  1. #include<iostream> // Allows input output operations
  2. #include <iomanip>
  3. #include<math.h>
  4.  
  5. using namespace std;
  6.  
  7. #define N 200
  8. #define ARRAY_SIZE (N*N)
  9. #define M 100
  10.  
  11. // Mapping is defined for [a-z] , [A-Z] , [0-9]
  12. // {'A','6'} maps A to 6
  13. //
  14. char map[M][2] =
  15. {
  16. {'A','6'}, {'B','b'}, {'C','c'}, {'D','d'}, {'E','2'}, {'F','1'}, {'G','g'}, {'H','h'}, {'I','9'}, {'J','j'}, {'K','k'},
  17. {'L','S'}, {'M','m'}, {'N','n'}, {'O','o'}, {'P','p'}, {'Q','q'}, {'R','r'}, {'S','s'}, {'T','t'}, {'U','u'}, {'V','v'},
  18. {'W','w'}, {'X','x'}, {'Y','y'}, {'Z','z'}, {'a','A'}, {'b','B'}, {'c','C'}, {'d','D'}, {'e','E'}, {'f','F'}, {'g','G'},
  19. {'h','H'}, {'i','I'}, {'j','J'}, {'k','K'}, {'l','L'}, {'m','M'}, {'n','N'}, {'o','O'}, {'p','P'}, {'q','Q'}, {'r','R'},
  20. {'s','l'}, {'t','T'}, {'u','U'}, {'v','V'}, {'w','W'}, {'x','X'}, {'y','Y'}, {'z','Z'}, {'0','i'}, {'1','8'}, {'2','7'},
  21. {'3','a'}, {'4','5'}, {'5','4'}, {'6','3'}, {'7','e'}, {'8','f'}, {'9','0'}
  22. };
  23.  
  24.  
  25. // Define functionality for the following functions
  26. // And call them in main()
  27.  
  28. /*For calculating Determinant of the Matrix */
  29.  
  30. /*******************************************************************************
  31. * Function Name : determinant
  32. * Description : Given a square matrix array[][]. Calculates determinant.
  33. * Input : array,order
  34. * Output : Determinant
  35. * Return : determinant value(type float)
  36. * Example : Order = 2
  37. Array [ 3 2 ] -> Returns 5.3000
  38.   [ 2 3.1 ]
  39.  
  40.  *******************************************************************************/
  41. float determinant(float array[N][N],float order)
  42. {
  43. float sum=0,c;
  44. for(int j=1;j<=order;j++)
  45. {
  46. for(int i=j+1;i<=order;i++)
  47. {
  48. c=(array[i][j])/array[j][j];
  49. for(int k=j;k<=order;k++)
  50. {
  51. array[i][k]=array[i][k]-c*array[j][k];
  52. }
  53. }
  54. }
  55. for(int l=1;l<=order;l++)
  56. {
  57. sum=sum+array[l][l];
  58. }
  59. return sum;
  60. }
  61.  
  62. /*******************************************************************************
  63. * Function Name : inverse
  64. * Description : Given a square matrix array[][]. Calculates inverse_array.
  65. * Input : array,order
  66. * Output : inv
  67. * Return : None
  68. * Example : Order = 2
  69.  
  70. Array [ 3 2 ] -> inv [ (3/5) (-2/5) ]
  71.   [ 2 3 ] [ (-2/5) (3/5) ]
  72.  
  73. *******************************************************************************/
  74.  
  75.  
  76. void inverse(float inv[N][N],float array[N][N],float order)
  77. {
  78. float c[N][N];
  79. int l,p,h,k,i,j;
  80. for(l=0;l<order;l++)
  81. {
  82. for(p=0;p<order;p++)
  83. {
  84. h=0,k=0;
  85. for(i=0;i<order;i++)
  86. {
  87. if(i==l)
  88. continue;
  89. for(j=0;j<order;j++)
  90. {
  91. if(j==p)
  92. continue;
  93. c[h][k]=array[i][j];
  94. k++;
  95. if(k=(order-1))
  96. {
  97. h++;
  98. k=0;
  99. }
  100. }
  101. }
  102.  
  103. inv[p][l]=(pow(-1,l+p)*determinant(c,order-1))/determinant(array,order);
  104.  
  105. }
  106. }
  107. }
  108.  
  109. /*******************************************************************************
  110. * Function Name : array_map
  111. * Description : Maps plain_text_zero_padded to mapped_array using map array
  112. * Input : plain_text_zero_padded
  113. * Output : mapped_array
  114. * Return : None
  115. * Example : plain_text_zero_padded "ABCDE00" -> mapped_array "abcd2ii"
  116. *******************************************************************************/
  117.  
  118.  
  119. void array_map(char mapped_array[ARRAY_SIZE], char plain_text_zero_padded[ARRAY_SIZE])
  120. {
  121. for(int i=0;i<ARRAY_SIZE;i++)
  122. {
  123. for(int j=0;j<M;j++)
  124. {
  125. if(plain_text_zero_padded[i]!=map[j][0])
  126. continue;
  127. else
  128. {
  129. mapped_array[i]=map[j][1];
  130. break;
  131. }
  132. }
  133.  
  134.  
  135. }
  136.  
  137.  
  138.  
  139. /*******************************************************************************
  140. * Function Name : inv_array_map
  141. * Description : Maps inv_mapped_array to decrypt_plain_text_zero_padded using map array
  142. * Input : inv_mapped_array
  143. * Output : decrypt_plain_text_zero_padded
  144. * Return : None
  145. * Example : inv_mapped_array "abcd2ii" -> decrypt_plain_text_zero_padded "ABCDE00"
  146. *******************************************************************************/
  147.  
  148. void inv_array_map(char inv_mapped_array[ARRAY_SIZE], char decrypt_plain_text_zero_padded[ARRAY_SIZE])
  149. {
  150. for(int i=0;i<ARRAY_SIZE;i++)
  151. {
  152. for(int j=0;j<M;j++)
  153. {
  154. if(inv_mapped_array[i]!=map[j][1])
  155. continue;
  156. else
  157. {
  158. decrypt_plain_text_zero_padded[i]=map[j][0];
  159. break;
  160.  
  161. }
  162. }
  163. }
  164. }
  165.  
  166.  
  167. // Donot modify the functions below(upto main)
  168.  
  169. void keygen(float key[N][N], int marker)
  170. {
  171. int i,j;
  172.  
  173. for(i=0;i<marker;i++){
  174. for(j=0;j<marker;j++){
  175. if(i != j)
  176. key[i][j] = marker;
  177. else
  178. key[i][j] = marker + 1;
  179. }
  180. }
  181.  
  182. }
  183.  
  184. void print(char c[N][N], int row, int col)
  185. {
  186.  
  187. int i,j;
  188.  
  189. for(i=0;i<row;i++){
  190. for(j=0;j<col;j++){
  191. cout << c[i][j]<< " ";
  192. }
  193. cout << endl;
  194. }
  195.  
  196. }
  197.  
  198. void print(float f[N][N], int row, int col)
  199. {
  200.  
  201. int i,j;
  202.  
  203. for(i=0;i<row;i++){
  204. for(j=0;j<col;j++){
  205. std::cout << std::fixed << std::setprecision(3) << setw(15) << f[i][j] ;
  206. }
  207. cout << endl;
  208. }
  209.  
  210. }
  211.  
  212. void compare(char plain_text[N],char decrypt_plain_text[N], int length_of_input)
  213. {
  214. int loi;
  215. for (loi = 0;(plain_text[loi] == decrypt_plain_text[loi] && plain_text[loi]);loi++);
  216. if(loi == length_of_input)
  217. cout << "SUCCESS"<<endl;
  218. }
  219.  
  220. int main() {
  221.  
  222. /*********** DO NOT CHANGE VARIABLE NAMES ************/
  223. char plain_text[ARRAY_SIZE];
  224. int marker;
  225.  
  226. float key[N][N];
  227.  
  228. float cipher_text[N][N];
  229. float invkey[N][N];
  230. char decrypt_plain_text[ARRAY_SIZE];
  231. char inv_mapped_array[ARRAY_SIZE];
  232.  
  233. /***************Get User Input***************/
  234.  
  235.  
  236. cout << "Enter the plain text" << endl;
  237. cin >> plain_text;
  238.  
  239. cout << "Enter the marker" << endl;
  240. cin >> marker;
  241.  
  242. // Creating a key matrix of size (marker x marker)
  243. keygen(key,marker);
  244.  
  245.  
  246. /***************End User Input***************/
  247.  
  248.  
  249. /*---------------------------------------------*/
  250.  
  251. // Please add your code here
  252. int length_of_input;
  253. for (length_of_input = 0;plain_text[length_of_input];length_of_input++);
  254. int r;
  255. r=length_of_input%marker;
  256. for(int a=0;a<(length_of_input+marker-r);a++)
  257. {
  258. if (a<length_of_input)
  259. {
  260. plain_text_zero_padded[a]=plain_text[a];
  261. }
  262. else{
  263.  
  264. plain_text_zero_padded[a]=0;
  265. }
  266. }
  267. void array_map(mapped_array,plain_text_zero_padded);
  268. char mapped[ARRAY_SIZE][N];
  269. int z=0;
  270. for( int e=0;e<(length_of_input/marker)+1;e++)
  271. {
  272. for(int f=0;f<marker;f++)
  273. {
  274. mapped[e][f]=mapped_array[z];
  275. z++;
  276. }
  277. }
  278.  
  279.  
  280.  
  281. for (int b=0;b<(length_of_input/marker)+1;b++)
  282. {
  283. for(int c=0;c<marker;c++)
  284. {
  285. cipher_text[b][c]=0;
  286. for(int d=0;d<marker;d++)
  287. cipher_text[b][c]+=mapped[b][d]*key[d][c];
  288. }
  289. }
  290. char rev_mapped[ARRAY_SIZE][N];
  291. void inverse(invkey,key,marker);
  292. for(int o=0;o<(length_of_input/marker)+1;o++)
  293. {
  294. for(int p=0;p<marker;p++)
  295. {
  296. rev_mapped[o][p]=0;
  297. for(int q=0;q<marker;q++)
  298. rev_mapped[o][p]+=cipher_text[o][q]*invkey[q][p];
  299. }
  300. }
  301. int y=0;
  302. for( int e=0;e<(length_of_input/marker)+1;e++)
  303. {
  304. for(int f=0;f<marker;f++)
  305. {
  306. inv_mapped_array[y]=rev_mapped[e][f];
  307. y++;
  308. }
  309. }
  310. void inv_array_map( inv_mapped_array, decrypt_plain_text_zero_padded);
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322. /*---------------------------------------------*/
  323. // Donot modify
  324. //
  325.  
  326. cout << "Plain Text entered" << endl;
  327. cout << plain_text<< "\n";
  328.  
  329. cout << "Key Used" << endl;
  330. print(key,marker,marker);
  331.  
  332. cout << "Cipher Text " << endl;
  333. print(cipher_text,(int)ceil((float)length_of_input/marker),marker);
  334.  
  335. cout << "Key Inverse" << endl;
  336. print(invkey,marker,marker);
  337.  
  338. cout << "Decrypted Text" << endl;
  339. cout << decrypt_plain_text << "\n";
  340.  
  341. compare(plain_text,decrypt_plain_text,length_of_input);
  342.  
  343. return 0;
  344. }
  345.  
  346.  
  347.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'void array_map(char*, char*)':
prog.cpp:149:1: error: a function-definition is not allowed here before '{' token
 {
 ^
prog.cpp:344:1: error: expected '}' at end of input
 }
 ^
stdout
Standard output is empty