Error encrypting with affinity algorithm

0

I am using afin encryption with its formula

int main(){
 char p[100];
 char alf[]="abcdefghijklmnopqrstuvwxyz";
 int a, b;

 printf("ingrese la palabra ");
 gets(p);
 printf("ingrese la constante de decimacion ");
 scanf("%i",&a);
 printf("ingrese la clave de cifrado ");
 scanf("%i",&b);

 int j;

 for(int i=0;i<strlen(p);i++){
  j=0;
   while(j<26){
      if(p[i]==alf[j]){
         p[i]= (a * alf[j] + b) % 26;
          j=26;
         }
        j++;
      }
   }

   printf("\nencriptacion:  \n");
   printf("\n %s",p);
  return 0;
   }

I get a strange symbol, it does not encrypt properly, I do not know what the problem is if the implementation of the formula is correct

    
asked by Fernanda x 24.10.2017 в 05:20
source

1 answer

1

Without an example to compare, I'm not sure, but I think your fault is, precisely, in your implementation of the formula:

char alf[]="abcdefghijklmnopqrstuvwxyz";
...
p[i]= (a * alf[j] + b) % 26;

We see what happens if the character to be encrypted is the letter 'a' , the constante de decimacion is 1 , and the constante de desplazamiento is 1:

  

(1 * alf [0] + 1)% 16
  (1 * 113 + 1)% 16 // (the ASCII code of 'a' is 113)
  114% 16
  2

And the spelling corresponding to the ASCII code 2 does not exist; It is a control code.

Now, let's change your formula to this one:

p[i]= alf[(a * j + b) % 26];
  

(1 * 0 + 1)% 16
  1% 16
  1

That would give us as a result alf[1] , which is the character 'b' .

    
answered by 24.10.2017 / 06:44
source