Advertisement
Guest User

xor.c

a guest
Nov 6th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. unsigned int Hash(const char * str)
  4. {
  5.  
  6.     unsigned int hash = 0;
  7.  
  8.     for(; *str; str++)
  9.     {
  10.         hash += (unsigned char)(str);
  11.         hash -= (hash << 13) | (hash >> 19);
  12.     }
  13.  
  14.     return hash;
  15.  
  16. }
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. int main(void)
  24. {
  25.     char KeyWord[255], Plain[255], Enc[255], DecTemp[255];
  26.     int i=0, ii=0, KeyLength=0, PlainLength=0, EncLength=0, DecLength=0;
  27.  
  28.     printf("Enter key: ");
  29.     gets(KeyWord);
  30.  
  31.     while(KeyWord[KeyLength])
  32.         KeyLength++;
  33.  
  34.     unsigned int HashedKey[KeyLength];
  35.     for (i = 0; i<KeyLength; i++)
  36.         HashedKey[i] = Hash(&KeyWord[i]);
  37.  
  38.     printf("Enter message: ");
  39.     gets(Plain);
  40.  
  41.     while(Plain[PlainLength])
  42.         PlainLength++;
  43.  
  44.     for (i=0; i<PlainLength; i++)
  45.     {
  46.         Enc[i] = Plain[i]^HashedKey[ii];
  47.         EncLength++;
  48.         if (ii<KeyLength)
  49.             ii++;
  50.         else
  51.             ii=0;
  52.     }
  53.     printf("!!! ii = %d\n", ii);
  54.     ii=0;
  55.     for (i=0; i<EncLength; i++)
  56.     {
  57.         DecTemp[i] = Enc[i]^HashedKey[ii];
  58.         DecLength++;
  59.         if (ii<KeyLength)
  60.             ii++;
  61.         else
  62.             ii=0;
  63.     }
  64.     printf("!!! ii = %d\n", ii);
  65.  
  66.     //char Decrypted[DecLength];
  67.     //for(i=0; i<DecLength; i++)
  68.     //    Decrypted[i] = DecTemp[i];
  69.  
  70.     printf("KEY: %s\n\nPLAINTEXT: %s\n\nKEYHASH: ", KeyWord, Plain);
  71.     for (i=0; i<KeyLength; i++)
  72.         printf("%X ", HashedKey[i]);
  73.  
  74.     printf("\n\nENCRYPTEDTEXT: %s\n\nDECRYPTEDTEXT: %s", Enc, DecTemp);
  75.  
  76.     printf("\n\nKEYLENGTH: %d\n\nPLAINLENGTH: %d\n\nENCLENGTH: %d\n\nDECLENGTH: %d\n\n", KeyLength, PlainLength, EncLength, DecLength);
  77.     system("pause");
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement