mrnn_

4307 Vigenére titkosítás és dekódolás

Nov 23rd, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. // 4307 - Vigenére titkosítás és dekódolás - Bíró: 20 pont
  2.  
  3. #include<stdio.h>
  4.  
  5. int main() {
  6.         int kod=0,hossz=0,i=0,j=0;
  7.         char kulcs[27],szo[201];
  8.         FILE *f;
  9.         f=fopen("be.txt","r");
  10.         while(!feof(f)) {
  11.                 fscanf(f,"%d",&kod);
  12.                 fscanf(f,"%d",&hossz);
  13.                 fscanf(f,"%s",kulcs);
  14.                 fscanf(f,"%s",szo);
  15.         }
  16.         if (kod==1) { //kódolás
  17.                 while(szo[i]!='\0') {
  18.                         if (j==hossz) {
  19.                                 j=0;
  20.                         }
  21.                         if ((szo[i]-97)+(kulcs[j]-65)<=25) {
  22.                                 szo[i]=szo[i]+(kulcs[j]-65);
  23.                         } else {
  24.                                 szo[i]=(szo[i]+(kulcs[j]-65))-26;
  25.                         }
  26.                         i++;
  27.                         j++;
  28.                 }
  29.                 f=fopen("ki.txt","w");
  30.                 fprintf(f,"%s\n",szo);
  31.                 fclose(f);
  32.         } else if (kod==2) { //dekódolás
  33.                 while(szo[i]!='\0') {
  34.                         if (j==hossz) {
  35.                                 j=0;
  36.                         }
  37.                         if ((szo[i]-97)-(kulcs[j]-65)>=0) {
  38.                                 szo[i]=szo[i]-(kulcs[j]-65);
  39.                         } else {
  40.                                 szo[i]=(szo[i]-(kulcs[j]-65))+26;
  41.                         }
  42.                         i++;
  43.                         j++;
  44.                 }
  45.                 f=fopen("ki.txt","w");
  46.                 fprintf(f,"%s\n",szo);
  47.                 fclose(f);
  48.         }
  49.         return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment