fork download
  1. /* Trabalho de Matemática Discreta
  2.  
  3.   Criptografia de HILL: Elaborar uma criptografia e descodificar o código gerado.
  4.  
  5.   Felipe Barbosa Pereira
  6.   Kimberly Lima
  7.   Rodrigo Pontes
  8.  
  9.   Ciência da Computação
  10.  
  11.   10/11/2015
  12.  
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <locale.h>
  19.  
  20.  
  21. void tira_espaco(char *);
  22. void menu(void);
  23. void menu_2(char *);
  24. void titulo (void);
  25. void adiciona_letra(char *);
  26. void mudar_para_criptografia(char *, int *);
  27. void criptografiaHill(int *, int [2][2], int, char *);
  28. void pega_codigo(int [2][2]);
  29. int verifica_codigo(int[2][2]);
  30. void descodificar(int, int[2][2], char *, int, int *);
  31. void sugestao(int [2][2]);
  32.  
  33. int main ()
  34. {
  35. setlocale(LC_ALL, "");
  36.  
  37. char str[501];
  38. int str_2[500];
  39. int determinante_inversa;
  40. int cod[2][2];
  41. char c;
  42. int k;
  43. char opcao;
  44.  
  45. menu();
  46.  
  47.  
  48.  
  49. do
  50. {
  51. switch (getch(c))
  52. {
  53. case '1':
  54. fflush(stdin);
  55. system("cls");
  56.  
  57. titulo();
  58.  
  59. printf("Entre com a palavra ou frase a ser criptografada por cifras de HILL:\n");
  60. scanf("%499[^\n]s", str);
  61. fflush(stdin);
  62.  
  63. tira_espaco(str);
  64.  
  65. do
  66. //Loop para receber os codigos até que um deles seja validado
  67. {
  68. pega_codigo(cod);
  69. determinante_inversa = verifica_codigo(cod);
  70.  
  71. if (cod[0][0]*cod[1][1] - cod[0][1]*cod [1][0] == 0)
  72. //Se determinante for 0, essa matriz é invalida
  73. {
  74. printf ("\n\nO codigo informado não está correto, pois não é inversível!\nEntre com outro código!\n");
  75. }
  76.  
  77. if (determinante_inversa == 69)
  78. // essa variavel recebeu um numero pela função verifica_codigo, esse numero é devido que a determinante inversa não está em modulo 26
  79. {
  80. printf("\n\nO codigo informado não está correto!\nA determinante inversa não está em modulo 26!\nEntre com outro código!\n\n\n");
  81. sugestao(cod);
  82. _sleep(3000);
  83. }
  84. }
  85. while (determinante_inversa == 69);
  86.  
  87.  
  88. if ((strlen(str)%2) != 0)
  89. //Verifico se for impar, chama função de repetir a ultima letra
  90. {
  91. adiciona_letra(str);
  92. }
  93.  
  94. mudar_para_criptografia(str, str_2);
  95. //Entra com a string e muda a string informada para os numeros da cifra em str_2
  96.  
  97. k = strlen(str);
  98.  
  99. criptografiaHill(str_2, cod, k, str);
  100.  
  101. printf("\n\nPalavra criptografada por HILL: %s\n\n\n", str);
  102. printf("\n\n\nDigite qualquer tecla para continuar....");
  103.  
  104. scanf("%c", &opcao);
  105. fflush(stdin);
  106. system("cls");
  107.  
  108. menu_2(str);
  109.  
  110. menu();
  111. break;
  112.  
  113. case '2':
  114. fflush(stdin);
  115. system("cls");
  116.  
  117. titulo();
  118.  
  119. printf("Entre com a palavra ou frase a ser decodificada por cifras de HILL:\n");
  120. scanf("%499[^\n]s", str);
  121. fflush(stdin);
  122.  
  123. tira_espaco(str);
  124.  
  125. int cod[2][2];
  126.  
  127. do
  128. {
  129. pega_codigo(cod);
  130. determinante_inversa = verifica_codigo(cod);
  131.  
  132. if (cod[0][0]*cod[1][1] - cod[0][1]*cod [1][0] == 0)
  133. {
  134. printf ("\n\nO codigo informado não está correto, pois não é inversível!\nEntre com outro código!\n\n\n");
  135. }
  136.  
  137. if (determinante_inversa == 69)
  138. {
  139. printf("\n\nO codigo informado não está correto!\nA determinante inversa não está em modulo 26!\nEntre com outro código!\n\n\n");
  140. _sleep(3000);
  141. }
  142.  
  143. }
  144. while (determinante_inversa == 69);
  145.  
  146. //printf("Determinante inversa: %d", determinante_inversa);
  147.  
  148. if ((strlen(str)%2) != 0)
  149. {
  150. adiciona_letra(str);
  151. }
  152.  
  153. mudar_para_criptografia(str, str_2);
  154.  
  155. k = strlen(str);
  156.  
  157. descodificar(determinante_inversa, cod, str, k, str_2);
  158.  
  159. printf("\n\n\t\tDecodificação efetuada com sucesso!\n\n'%s'\n\n\n\n", str);
  160.  
  161. printf("\n\n\nDigite qualquer tecla para continuar....");
  162. scanf("%c", &opcao);
  163. fflush(stdin);
  164. system("cls");
  165.  
  166.  
  167.  
  168. menu();
  169. }
  170.  
  171.  
  172. }
  173. while (c != 27);
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. return 0;
  182. }
  183.  
  184.  
  185. void menu ()
  186. {
  187. printf("\n--------------------------------------------------------------------------------\n\tBem vindo ao programa de CRIPTOGRAFIA CIFRAS DE HILL\n\n--------------------------------------------------------------------------------\n\n");
  188. printf("\t\t\t*--------------------------*\n"); //26 espaços
  189. printf("\t\t\t|ESCOLHA A OPÇÃO PRETENDIDA|\n\t\t\t| |\n");
  190. printf("\t\t\t|1 - Codificar |\n\t\t\t| |\n\t\t\t|2 - Decodificar |\n");
  191. printf("\t\t\t*--------------------------*\n\n");
  192. }
  193.  
  194. void menu_2 (char *str)
  195. {
  196. printf("\n--------------------------------------------------------------------------------\n\tBem vindo ao programa de CRIPTOGRAFIA CIFRAS DE HILL\n\n--------------------------------------------------------------------------------\n\n");
  197. printf("\n\nDESEJA SALVAR A PALAVRA CRIPTOGRAFADA EM UM ARQUIVO?\n\n'S' para SIM ou 'N' para NÃO\n\n");
  198.  
  199. char opcao;
  200. scanf("%c", &opcao);
  201. fflush(stdin);
  202. if (opcao == 's' || opcao == 'S')
  203. {
  204. FILE *pa;
  205. pa = fopen("Criptografia_de_HILL.txt", "w");
  206. fprintf(pa, "--------------------------------------------------------------------------------\n\tBem vindo ao programa de CRIPTOGRAFIA CIFRAS DE HILL\n\n--------------------------------------------------------------------------------\n\n");
  207. fprintf(pa, "\n\nTexto Criptografado ''%s'' \n\n\n", str);
  208. fprintf(pa, "Caso queira colar este texto no programa siga os passos:\n\n1º - Copie o texto\nObs: NÃO COPIE AS ASPAS!\n\n");
  209. fprintf(pa, "2º - Abra o menu do programa 'Decodificar'\n\n3º - Na Barra de Título, clique com o botão direito do mouse -> Editar -> Colar\n\n\n\nLembre-se de fechar este documento caso queira salvar outra criptografia!");
  210. fprintf(pa, "\n\n\n\n\n********************************************************************************\n\n\t\t\tPrograma desenvolvido por:\n\n\t\t\tFelipe Barbosa\n\t\t\tKimberly Lima\n\t\t\tRodrigo Pontes\n\n\n********************************************************************************\n\nCiência da Computação\n10/11/2015\nUNISANTOS\n\n");
  211. fclose(pa);
  212.  
  213. printf("\n\n\nARQUIVO 'Criptografia_de_HILL.txt' GERADO COM SUCESSO!\n\n\n");
  214. printf("Verifique este arquivo na pasta onde o programa está gravado!\n\n\n");
  215.  
  216. printf("\n\n\nDigite qualquer tecla para continuar....");
  217.  
  218. scanf("%c", &opcao);
  219. fflush(stdin);
  220. system("cls");
  221. fflush(stdin);
  222. }
  223. else
  224. {
  225. system("cls");
  226.  
  227. }
  228.  
  229. }
  230.  
  231. void titulo (void)
  232. {
  233. printf("\n--------------------------------------------------------------------------------\n\tBem vindo ao programa de CRIPTOGRAFIA CIFRAS DE HILL\n\n--------------------------------------------------------------------------------\n\n");
  234. }
  235.  
  236. void tira_espaco(char *str)
  237. {
  238. char aux;
  239. int i, j, k = strlen(str), l;
  240.  
  241.  
  242. for (l = 0 ; l < k ; l++)
  243. {
  244. for (i = 0 ; i < k ; i++)
  245. {
  246. if (str[i] == ' ')
  247. {
  248. for (j = i; j < k ; j++)
  249. {
  250. aux = str[j+1];
  251. str[j] = aux;
  252. }
  253. }
  254. }
  255. }
  256. }
  257.  
  258.  
  259. void adiciona_letra(char *str)
  260. {
  261. char letra = str[strlen(str)-1];
  262. int aux = strlen(str);
  263. str[strlen(str)] = letra;
  264. str[aux+1] = '\0';
  265. }
  266.  
  267. void mudar_para_criptografia(char *str, int *str_2)
  268. //Recebo a string informada pelo usuário e modifico essa string para os numeros da cifra de HILL na variável inteira str_2
  269. {
  270. int i, j = 0;
  271.  
  272. for (i = 0 ; i < strlen(str) ; i++)
  273. {
  274. if (str[i] > 64 && str[i] < 91)
  275. {
  276. str_2[j] = str[i] - 64;
  277. }
  278. else if (str[i] > 96 && str[i] < 123)
  279. {
  280. str_2[j] = str[i] - 96;
  281. }
  282.  
  283. if (str_2[j] == 26)
  284. {
  285. str_2[j] = 0;
  286. }
  287.  
  288. j++;
  289. }
  290. }
  291.  
  292. void criptografiaHill(int *str_2, int cod[2][2], int k, char *str)
  293. {
  294. int j = 0, i;
  295. int str_HILL[500];
  296.  
  297. for (i = 0 ; i < k/2 ; i ++)
  298. {
  299. str_HILL[j] = (cod[0][0] * str_2[j]) + (cod[0][1] * str_2[j+1]);
  300. if (str_HILL[j] > 26 )
  301. {
  302. do
  303. {
  304. str_HILL[j] %= 26;
  305. }
  306. while (str_HILL[j] > 26);
  307.  
  308. if (str_HILL[j] == 0 )
  309. {
  310. str_HILL[j] = 26;
  311. }
  312.  
  313. /*if (str_HILL[j] < 0 )
  314.   {
  315.   str_HILL[j] = (str_HILL[j] % 26) + 26;
  316.   }*/
  317. }
  318. else if (str_HILL[j] == 0 )
  319. {
  320. str_HILL[j] = 26;
  321. }
  322.  
  323. str[j] = str_HILL[j] + 64;
  324.  
  325. str_HILL[j+1] = (cod[1][0] * str_2[j]) + (cod[1][1] * str_2[j+1]);
  326. if (str_HILL[j+1] > 26)
  327. {
  328. do
  329. {
  330. str_HILL[j+1] %= 26;
  331. }
  332. while (str_HILL[j+1] > 26);
  333.  
  334. if (str_HILL[j+1] == 0 )
  335. {
  336. str_HILL[j+1] = 26;
  337. }
  338.  
  339. /* if (str_HILL[j] < 0 )
  340.   {
  341.   str_HILL[j+1] = (str_HILL[j+1] % 26) + 26;
  342.   }*/
  343. }
  344. if (str_HILL[j+1] == 0 )
  345. {
  346. str_HILL[j+1] = 26;
  347. }
  348. str[j+1] = str_HILL[j+1] + 64;
  349.  
  350. j+=2;
  351.  
  352. }
  353.  
  354. str[k] = '\0';
  355.  
  356. }
  357.  
  358. void pega_codigo(int cod[2][2])
  359. {
  360. printf("\n\n\tEntre com o código (em linhas) :\n\n1º codigo: ");
  361.  
  362. scanf("%d", &cod[0][0]);
  363.  
  364. printf("\n2º codigo: ");
  365.  
  366. scanf("%d", &cod[0][1]);
  367.  
  368. printf("\n3º codigo: ");
  369.  
  370. scanf("%d", &cod[1][0]);
  371.  
  372. printf("\n4º codigo: ");
  373.  
  374. scanf("%d", &cod[1][1]);
  375. fflush(stdin);
  376.  
  377. int i, j;
  378. /*printf("\n\n\n\t Código informado:\n\n");
  379.   for (i = 0; i < 2; i++)
  380.   {
  381.   for (j = 0 ; j < 2 ; j++)
  382.   {
  383.   printf("\t %d", cod[i][j]);
  384.   }
  385.   printf("\n");
  386.   }*/
  387. }
  388.  
  389. int verifica_codigo(int cod[2][2])
  390. {
  391. int determinante = cod[0][0]*cod[1][1] - cod[0][1]*cod[1][0];
  392. int determinante_inversa;
  393.  
  394. //printf("\n\nDeterminante: %d\n\n", determinante);
  395.  
  396. /* switch (determinante)
  397.   {
  398.   case 1:
  399.   determinante_inversa = 1;
  400.   break;
  401.   case 3:
  402.   determinante_inversa = 9;
  403.   break;
  404.   case 5:
  405.   determinante_inversa = 21;
  406.   break;
  407.   case 7:
  408.   determinante_inversa = 15;
  409.   break;
  410.   case 9:
  411.   determinante_inversa = 3;
  412.   break;
  413.   case 11:
  414.   determinante_inversa = 19;
  415.   break;
  416.   case 15:
  417.   determinante_inversa = 7;
  418.   break;
  419.   case 17:
  420.   determinante_inversa = 23;
  421.   break;
  422.   case 19:
  423.   determinante_inversa = 11;
  424.   break;
  425.   case 21:
  426.   determinante_inversa = 5;
  427.   break;
  428.   case 23:
  429.   determinante_inversa = 17;
  430.   break;
  431.   case 25:
  432.   determinante_inversa = 25;
  433.   break;
  434.   default:
  435.   printf("Código inválido! Tente novamente!");
  436.   determinante_inversa = 69;
  437.   break;
  438.   }*/
  439.  
  440. int i;
  441. for (i = 0 ; i < 26 ; i++)
  442. {
  443. if ((determinante*i)%26 == 1)
  444. {
  445. return i;
  446. }
  447. }
  448.  
  449. return determinante_inversa = 69;
  450.  
  451.  
  452. }
  453.  
  454. void descodificar(int determinante_inversa, int cod[2][2], char *str, int k, int *str_2)
  455. {
  456. int j = 0, i;
  457. int str_HILL[500];
  458. int aux;
  459.  
  460. aux = cod[0][0] * determinante_inversa;
  461. cod[0][0] = cod[1][1] * determinante_inversa;
  462. cod[1][1] = aux;
  463.  
  464. cod[0][1] *= determinante_inversa * (-1);
  465. cod[1][0] *= determinante_inversa * (-1);
  466.  
  467.  
  468. for (i = 0 ; i < k/2 ; i ++)
  469. {
  470. str_HILL[j] = (cod[0][0] * str_2[j]) + (cod[0][1] * str_2[j+1]);
  471.  
  472.  
  473.  
  474. if (!(str_HILL[j] > 0 && str_HILL[j] < 26))
  475. {
  476. do
  477. {
  478. str_HILL[j] %= 26;
  479. }
  480. while (str_HILL[j] > 26 && str_HILL[j] > -26 );
  481.  
  482. if (str_HILL[j] == 0 )
  483. {
  484. str_HILL[j] = 26;
  485. }
  486.  
  487. if (str_HILL[j] < 0 )
  488. {
  489. str_HILL[j] = (str_HILL[j] % 26) + 26;
  490. }
  491. }
  492.  
  493.  
  494. str[j] = str_HILL[j] + 64;
  495.  
  496.  
  497. str_HILL[j+1] = (cod[1][0] * str_2[j]) + (cod[1][1] * str_2[j+1]);
  498.  
  499.  
  500.  
  501. if (!(str_HILL[j+1] > 0 && str_HILL[j+1] < 26))
  502. {
  503. do
  504. {
  505. str_HILL[j+1] %= 26;
  506. }
  507. while (str_HILL[j+1] > 26 && str_HILL[j+1] > -26 );
  508.  
  509. if (str_HILL[j+1] == 0 )
  510. {
  511. str_HILL[j+1] = 26;
  512. }
  513.  
  514. if (str_HILL[j+1] < 0)
  515. {
  516. str_HILL[j+1] = (str_HILL[j+1] % 26) + 26;
  517.  
  518. }
  519. }
  520.  
  521. str[j+1] = str_HILL[j+1] + 64;
  522.  
  523. j+=2;
  524.  
  525. }
  526.  
  527. str[k] = '\0';
  528. }
  529.  
  530. void sugestao(int cod[2][2])
  531. {
  532. int i, j, contador = 0, aux;
  533. int determinante;
  534. int determinante_inversa;
  535.  
  536. //Verificar para posição 1 do codigo
  537.  
  538. aux = cod[0][0];
  539. for (j = 1; j <= 5 ; j++)
  540. {
  541. cod[0][0] += j;
  542. determinante = cod[0][0]*cod[1][1] - cod[0][1]*cod[1][0];
  543.  
  544. for (i = 0 ; i < 26 ; i++)
  545. {
  546. if ((determinante*i)%26 == 1 && contador < 2)
  547. {
  548. if (contador == 0)
  549. {
  550. printf("\n\n\tEscolha uma das alternativas para validação do seu código:\n");
  551. }
  552. printf("\n\nSugerimos que troque o 1º código por: %d\n\n", cod[0][0]);
  553. contador++;
  554. }
  555. }
  556. }
  557. cod[0][0] = aux;
  558.  
  559.  
  560. //Verificar para codigo 2:
  561. aux = cod[0][1];
  562. for (j = 1; j <= 5 ; j++)
  563. {
  564. cod[0][1] += j;
  565. determinante = cod[0][0]*cod[1][1] - cod[0][1]*cod[1][0];
  566.  
  567. for (i = 0 ; i < 5 ; i++)
  568. {
  569. if ((determinante*i)%26 == 1 && contador < 2)
  570. {
  571. if (contador == 0)
  572. {
  573. printf("\n\n\tEscolha uma das alternativas para validação do seu código:\n");
  574. }
  575. printf("\n\nSugerimos que troque o 2º código por: %d\n\n", cod[0][1]);
  576. contador++;
  577. break;
  578. }
  579. }
  580. }
  581. cod[0][1] = aux;
  582.  
  583. //Verificar para codigo 3:
  584.  
  585. aux = cod[1][0];
  586.  
  587. for (j = 1; j <= 5 ; j++)
  588. {
  589. cod[1][0] += j;
  590. determinante = cod[0][0]*cod[1][1] - cod[0][1]*cod[1][0];
  591.  
  592. for (i = 0 ; i < 26 ; i++)
  593. {
  594. if ((determinante*i)%26 == 1 && contador < 2)
  595. {
  596. if (contador == 0)
  597. {
  598. printf("\n\n\tEscolha uma das alternativas para validação do seu código:\n");
  599. }
  600. printf("\n\nSugerimos que troque o 3º código por: %d\n\n", cod[1][0]);
  601. contador++;
  602. break;
  603. }
  604. }
  605. }
  606. cod[1][0] = aux;
  607.  
  608. //Verificar para codigo 4:
  609.  
  610. aux = cod[1][1];
  611. for (j = 1; j <= 5 ; j++)
  612. {
  613. cod[1][1] += j;
  614. determinante = cod[0][0]*cod[1][1] - cod[0][1]*cod[1][0];
  615.  
  616. for (i = 0 ; i < 26 ; i++)
  617. {
  618. if ((determinante*i)%26 == 1 && contador < 2)
  619. {
  620. if (contador == 0)
  621. {
  622. printf("\n\n\tEscolha uma das alternativas para validação do seu código:\n");
  623. }
  624.  
  625. printf("\n\nSugerimos que troque o 4º código por: %d\n\n", cod[1][1]);
  626. contador++;
  627. }
  628. }
  629. }
  630. cod[1][1] = aux;
  631.  
  632. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
ola
2
3
4
9
compilation info
prog.c: In function 'main':
prog.c:51:17: warning: implicit declaration of function 'getch' [-Wimplicit-function-declaration]
         switch (getch(c))
                 ^
prog.c:82:21: warning: implicit declaration of function '_sleep' [-Wimplicit-function-declaration]
                     _sleep(3000);
                     ^
prog.c: In function 'pega_codigo':
prog.c:377:12: warning: unused variable 'j' [-Wunused-variable]
     int i, j;
            ^
prog.c:377:9: warning: unused variable 'i' [-Wunused-variable]
     int i, j;
         ^
prog.c: In function 'sugestao':
prog.c:534:9: warning: unused variable 'determinante_inversa' [-Wunused-variable]
     int determinante_inversa;
         ^
prog.c: In function 'main':
prog.c:51:17: warning: 'c' is used uninitialized in this function [-Wuninitialized]
         switch (getch(c))
                 ^
/home/PpIXOf/cckKZSf0.o: In function `main':
prog.c:(.text.startup+0x36): undefined reference to `getch'
prog.c:(.text.startup+0x213): undefined reference to `_sleep'
prog.c:(.text.startup+0x401): undefined reference to `_sleep'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty