hozer

[IS] Autokey Encryption

Oct 9th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. #define MAX 512
  8.  
  9. char offset(int c, int n)
  10. {
  11.     int isUpper = 0, off, sign = 1, rez ;
  12.     if (c >= 65 && c <= 90) { isUpper = 1; c += 32; }
  13.     if (n < 0) sign = -1;
  14.     if (n*sign >= 65 && n*sign <= 90) n += sign * 32;
  15.  
  16.     rez = c;
  17.     off = n % 97;
  18.     rez += off;
  19.     if (rez < 97) rez = 123 + off + (c - 97);
  20.     if (rez > 122) rez = 96 + off - (122 - c);
  21.  
  22.     if (isUpper) rez -= 32;
  23.     return rez;
  24. }
  25.  
  26.  
  27. char * encrypt(char *m, char *key)
  28. {
  29.     int i, j;
  30.     char *cip;
  31.     int mlen = strlen(m), klen = strlen(key);
  32.     cip = (char*) malloc(mlen * sizeof(char));
  33.    
  34.     for (i = 0, j = 0; i < mlen && j < klen; i++)
  35.     {
  36.         if (isalpha(m[i]))
  37.         {
  38.             cip[i] = offset(m[i], (int) key[j]);
  39.             j++;
  40.         }
  41.         else cip[i] = m[i];
  42.     }
  43.  
  44.     for (j = 0; i < mlen; i++)
  45.     {
  46.         if (isalpha(m[i]))
  47.         {
  48.             while (!isalpha(m[j])) j++;
  49.             cip[i] = offset(m[i], (int) m[j]);
  50.             j++;
  51.         }
  52.         else cip[i] = m[i];
  53.         if (j > mlen) j = 0;
  54.     }
  55.  
  56.     cip[i] = '\0';
  57.  
  58.     return cip;
  59. }
  60.  
  61. char *decrypt(char *cip, char *key)
  62. {
  63.     int i, j;
  64.     char *m, *tkey;;
  65.     int clen = strlen(cip), klen = strlen(key);
  66.     m = (char*) malloc(clen * sizeof(char));
  67.     tkey = (char*) malloc(klen * sizeof(char));
  68.  
  69.     for (i = 0, j = 0; i < clen && j < klen; i++)
  70.     {
  71.         if (isalpha(cip[i]))
  72.         {
  73.             m[i] = offset(cip[i], -1 * (int) key[j]);
  74.             j++;
  75.         }
  76.         else m[i] = cip[i];
  77.     }
  78.  
  79.     for (j = 0; i < clen; i++)
  80.     {
  81.         if (isalpha(cip[i]))
  82.         {
  83.             while (!isalpha(cip[j])) j++;
  84.             m[i] = offset(cip[i], -1 * (int) m[j]);
  85.             j++;
  86.         }
  87.         else m[i] = cip[i];
  88.         if (j > clen) j = 0;
  89.     }
  90.  
  91.     m[i] = '\0';
  92.     return m;
  93. }
  94.  
  95. void main()
  96. {
  97.     int i, op;
  98.     char *s[MAX], *c[MAX], *key[MAX];
  99.     FILE *f;
  100.     f = fopen("input.txt", "r");
  101.     fgets(s, MAX, f);
  102.     fclose(f);
  103.  
  104.     printf("input key: "); gets(key);
  105.     printf("select option: encrypt (1), decrypt (2): "); scanf("%d", &op);
  106.  
  107.     if (op == 1) strcpy(c, encrypt(s, key));
  108.     else if (op == 2) strcpy(c, decrypt(s, key));
  109.     f = fopen("output.txt", "w");
  110.     fprintf(f, "%s", c);
  111.     fclose(f);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment