Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define max 26
- char cifra[50];
- char alfa[max];
- int aux[max];
- int aux1[max];
- char msg[max];
- char chave[max];
- char quad[max][max];
- static void setAlfa(){
- int i;
- char ax='a';
- for(i=0;i<max;i++){
- alfa[i]=ax;
- ax++;
- // printf("alfa:%c\n",alfa[i]);
- }
- }
- static void setQuad(){
- char a;
- //96=`
- a='`';
- //a=97 z=122
- int i,j;
- for(i=0;i<max;i++){
- if(a<'a' || a<'z') a+=1;
- else if(a>='z') a='a';
- for(j=0;j<max;j++){
- quad[i][j]=a;
- if(a<'z')a++;
- else if(a>='z') a='a';
- }}//fim for
- /*
- printf("\n");
- for(i=0;i<max;i++){
- printf("\n");
- for(j=0;j<max;j++){
- printf("%c",quad[i][j]);
- }}*/
- }
- static void encrypt(int tmCh,int tmMsg){
- int x=0,y=0,p=0;
- int cont=0,i=0,j=0;
- int ct=0;
- // while(cont<tmCh)
- // {
- for(j=0;j<tmCh;j++){
- for(i=0;i<max;i++) {
- if(chave[j]==alfa[i]){
- aux[ct]=i;
- ct++;
- break;
- }else {
- continue;
- }
- }
- }
- // cont++;
- // }//fim while
- /* for(i=0;i<tmCh;i++){
- cont=aux[i];
- printf("chave: %c | posi %d\n",quad[0][cont],cont);
- }
- */
- cont=0;
- ct=0;
- // while(cont<tmMsg){
- for(j=0;j<tmMsg;j++){
- for(i=0;i<max;i++){
- if(msg[j]==quad[i][0]){
- aux1[ct]=i;
- ct++;
- break;
- }else{
- continue;
- }
- }
- }
- // cont++;
- /* //}//fim while
- for(i=0;i<tmMsg;i++){
- cont=aux1[i];
- printf("chave: %c | posi: %d\n",quad[cont][0],cont);
- }
- printf("tmch: %d | tmMsg: %d\n",tmCh,tmMsg);
- */
- //encontrando o ponto p
- cont=0;
- while(cont<tmMsg){
- x=aux[cont];
- y=aux1[cont];
- //printf("mensagem crifrada: %c",quad[x][y]);
- cifra[cont]=quad[y][x];
- cont++;
- }
- printf("cifra: %s\n",cifra);
- }
- int main(int argc,char *argv[]){
- setQuad();
- setAlfa();
- int tmMsg=0,tmCh=0,dif=0;
- if(argc<3 || argc>3){
- printf("numero de argumentos invalido!\n");
- exit(EXIT_FAILURE);
- }
- else if(argc==3)
- tmMsg=strlen(argv[1]);
- tmCh=strlen(argv[2]);
- if(tmCh<tmMsg) dif=tmMsg-tmCh;
- if(tmMsg<=max && tmMsg>=10) strcpy(msg,argv[1]);
- else if(tmMsg<10 || tmMsg>max) {
- perror("Tamanho da msg muito grande ou muito pequeno\n");
- exit(EXIT_FAILURE);
- }
- if(tmCh>=8 && tmCh<=max) strcpy(chave,argv[2]);
- if(tmCh<8 || tmCh>max) {
- perror("Tamanho da chave er muito grande ou muito pequeno\n");
- exit(EXIT_FAILURE);
- }
- if(tmMsg!=tmCh && tmMsg>tmCh){
- int tmp=tmCh-1;//variavel temporaria;
- int tmp1=0;
- while(tmp<tmMsg){
- chave[tmp]=chave[tmp1];
- if(tmp1<tmCh) tmp1++;
- else if(tmp1==tmCh) tmp1=0;
- tmp=strlen(chave);
- //printf("%c",chave[tmp]);
- }
- //printf("%s\n",msg);
- //printf("%s\n",chave);
- encrypt(tmp,tmMsg);
- }//fim ultimo if
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement