zmiana szyfru

0

Witam, mam kod na szyfrowanie liczb względem danego klucza, ale muszę go przerobić na szyfr afiniczny. Wiem, że nie jest to trudne, ale nie mam pomysłu. Jakieś podpowiedzi co zmienić?

int klucz,a;
while((printf("klucz: "))&&(scanf("d",&klucz)==1))
    {
         while(getchar()!='\n')  {}
         klucz=klucz%26;
         printf("podaj tekst: ");
         while((a=toupper(getchar()))!=EOF)
          {
             if(isalpha(a)) a=((a-'A'+26-klucz)%26)+'A';
           }
     }
     return 0;
)
0
a=(((a-'A')*a_klucz%26+26-b_klucz)%26)+'A';
0

najlepiej to wyrobić sobie odruch dzielenia problemu na mniejsze. Czyli na małe funkcje, a dane organizować w struktury.

struct AffineCipherData {
    int a;
    int b;
    int m;
    // int a_inverted;
};

int AffineCipherIsValid(struct AffineCipherData *cipher) {
    ...
}

int AffineCipherEncode(struct AffineCipherData *cipher, int x) {
     assert(x >= 0);
     assert(x < cipher->m);
     return (cipher->a * x + cipher->b ) % cipher->m;
}

int AffineCipherDecode(struct AffineCipherData *cipher, int x) {
     assert(x >= 0);
     assert(x < cipher->m);
     return ....;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1