Advertisement
corot7b2

Untitled

Oct 28th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define max 26
  6.  
  7. char cifra[50];
  8. char alfa[max];
  9. int aux[max];
  10. int aux1[max];
  11. char msg[max];
  12. char chave[max];
  13. char quad[max][max];
  14. static void setAlfa(){
  15. int i;
  16. char ax='a';
  17. for(i=0;i<max;i++){
  18. alfa[i]=ax;
  19. ax++;
  20. // printf("alfa:%c\n",alfa[i]);
  21. }
  22. }
  23. static void setQuad(){
  24. char a;
  25. //96=`
  26. a='`';
  27. //a=97 z=122
  28. int i,j;
  29. for(i=0;i<max;i++){
  30. if(a<'a' || a<'z') a+=1;
  31. else if(a>='z') a='a';
  32. for(j=0;j<max;j++){
  33. quad[i][j]=a;
  34. if(a<'z')a++;
  35. else if(a>='z') a='a';
  36. }}//fim for
  37. /*
  38. printf("\n");
  39. for(i=0;i<max;i++){
  40. printf("\n");
  41. for(j=0;j<max;j++){
  42. printf("%c",quad[i][j]);
  43. }}*/
  44. }
  45.  
  46. static void encrypt(int tmCh,int tmMsg){
  47. int x=0,y=0,p=0;
  48. int cont=0,i=0,j=0;
  49. int ct=0;
  50.  
  51. // while(cont<tmCh)
  52. // {
  53.  
  54. for(j=0;j<tmCh;j++){
  55.  
  56. for(i=0;i<max;i++) {
  57. if(chave[j]==alfa[i]){
  58. aux[ct]=i;
  59. ct++;
  60. break;
  61. }else {
  62.  
  63. continue;
  64.  
  65. }
  66. }
  67. }
  68.  
  69.  
  70. // cont++;
  71.  
  72. // }//fim while
  73.  
  74. /* for(i=0;i<tmCh;i++){
  75. cont=aux[i];
  76. printf("chave: %c | posi %d\n",quad[0][cont],cont);
  77. }
  78. */
  79. cont=0;
  80. ct=0;
  81. // while(cont<tmMsg){
  82. for(j=0;j<tmMsg;j++){
  83. for(i=0;i<max;i++){
  84. if(msg[j]==quad[i][0]){
  85. aux1[ct]=i;
  86. ct++;
  87. break;
  88. }else{
  89. continue;
  90. }
  91. }
  92. }
  93. // cont++;
  94. /* //}//fim while
  95. for(i=0;i<tmMsg;i++){
  96. cont=aux1[i];
  97. printf("chave: %c | posi: %d\n",quad[cont][0],cont);
  98. }
  99. printf("tmch: %d | tmMsg: %d\n",tmCh,tmMsg);
  100.  
  101. */
  102.  
  103. //encontrando o ponto p
  104. cont=0;
  105. while(cont<tmMsg){
  106. x=aux[cont];
  107. y=aux1[cont];
  108. //printf("mensagem crifrada: %c",quad[x][y]);
  109. cifra[cont]=quad[y][x];
  110. cont++;
  111. }
  112. printf("cifra: %s\n",cifra);
  113. }
  114.  
  115.  
  116. int main(int argc,char *argv[]){
  117. setQuad();
  118. setAlfa();
  119. int tmMsg=0,tmCh=0,dif=0;
  120. if(argc<3 || argc>3){
  121. printf("numero de argumentos invalido!\n");
  122. exit(EXIT_FAILURE);
  123. }
  124. else if(argc==3)
  125. tmMsg=strlen(argv[1]);
  126. tmCh=strlen(argv[2]);
  127. if(tmCh<tmMsg) dif=tmMsg-tmCh;
  128.  
  129. if(tmMsg<=max && tmMsg>=10) strcpy(msg,argv[1]);
  130. else if(tmMsg<10 || tmMsg>max) {
  131. perror("Tamanho da msg muito grande ou muito pequeno\n");
  132. exit(EXIT_FAILURE);
  133. }
  134. if(tmCh>=8 && tmCh<=max) strcpy(chave,argv[2]);
  135. if(tmCh<8 || tmCh>max) {
  136. perror("Tamanho da chave er muito grande ou muito pequeno\n");
  137. exit(EXIT_FAILURE);
  138. }
  139. if(tmMsg!=tmCh && tmMsg>tmCh){
  140. int tmp=tmCh-1;//variavel temporaria;
  141. int tmp1=0;
  142. while(tmp<tmMsg){
  143. chave[tmp]=chave[tmp1];
  144. if(tmp1<tmCh) tmp1++;
  145. else if(tmp1==tmCh) tmp1=0;
  146. tmp=strlen(chave);
  147. //printf("%c",chave[tmp]);
  148. }
  149. //printf("%s\n",msg);
  150. //printf("%s\n",chave);
  151. encrypt(tmp,tmMsg);
  152.  
  153. }//fim ultimo if
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement