Guest User

pc1.c

a guest
Aug 19th, 2014
1,852
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define ENCRYPT_SIZE 0x100000
  6.  
  7. int keybits;
  8. int y, z;
  9.  
  10. //--------------------------------------------------------------------
  11. int code(int idx, unsigned long *rand_seed)
  12. {
  13.         unsigned long res1, res2;
  14.  
  15.         if(idx + z == 0)
  16.                 res2 = 0;
  17.         else
  18.                 res2 = 0x4e35 * (idx + z);
  19.         res1 = *rand_seed * 0x15a;
  20.         *rand_seed = 0x4e35 * *rand_seed + 1;
  21.        
  22.         z = y + res2 + res1;
  23.         y = res1;
  24.         return *rand_seed ^ z;
  25. }
  26.  
  27. //--------------------------------------------------------------------
  28. int assemble(char *password)
  29. {
  30.         int i;
  31.         unsigned long table[1024];
  32.         unsigned long prev_val, crypt_val;
  33.  
  34.         for(i = 0; i < keybits / 16; i++)
  35.         {
  36.                 if(i == 0)
  37.                         prev_val = 0;
  38.                 else
  39.                         prev_val = table[i - 1];
  40.                 table[i] = prev_val ^ ((password[2 * i] << 8) + password[2 * i + 1]);
  41.                 if(i == 0)
  42.                         crypt_val = 0;
  43.                 crypt_val = crypt_val ^ code(i, &table[i]);
  44.  
  45.         }
  46.         return crypt_val;
  47. }
  48.  
  49. //--------------------------------------------------------------------
  50. unsigned char encrypt(unsigned char v, char *password)
  51. {
  52.         int crypt_val;
  53.         unsigned char xor_value;
  54.         int i, tmp;
  55.  
  56.         crypt_val = assemble(password);
  57.     xor_value = (crypt_val >> 8);
  58.     tmp = crypt_val & 0xff;
  59.        
  60.         for(i = 0; i < keybits / 8; i++)
  61.                 password[i] ^= v;
  62.  
  63.         return v ^ (xor_value ^ tmp);
  64. }
  65.  
  66.  
  67.  //--------------------------------------------------------------------
  68. int file_encrypt(FILE *f_in, FILE *f_out, long size, char *password)
  69. {
  70.     long filesize, decrypted_len, block_size;
  71.     char buffer[1024] = { 0 };
  72.     unsigned char buffer2[1024] = { 0 };
  73.     int i;
  74.  
  75.     fseek(f_in, 0, SEEK_END);
  76.     filesize = ftell(f_in);
  77.        
  78.     if(filesize <= size){
  79.         size = filesize;
  80.         decrypted_len = filesize;
  81.     }else{
  82.         decrypted_len = size;
  83.     }
  84.  
  85.     rewind(f_in);
  86.  
  87.     while(decrypted_len > 0)
  88.     {
  89.         block_size = 1024;
  90.         if(decrypted_len < 1024)
  91.             block_size = decrypted_len;
  92.         fread(buffer, 1, block_size, f_in);
  93.  
  94.         for(i = 0; i < block_size; i++)
  95.         {
  96.             buffer2[i] = encrypt(buffer[i], password);
  97.         }
  98.         fwrite(buffer2, 1, block_size, f_out);
  99.         decrypted_len -= block_size;
  100.         filesize -= block_size;
  101.     }
  102.  
  103.     while(filesize > 0)
  104.     {
  105.         block_size = 1024;
  106.         if(filesize < 1024)
  107.             block_size = filesize;
  108.  
  109.         fread(buffer, 1, block_size, f_in);
  110.         fwrite(buffer, 1, block_size, f_out);
  111.         filesize -= block_size;
  112.     }
  113.  
  114.     return 0;
  115. }
  116.  
  117. //--------------------------------------------------------------------
  118. unsigned char decrypt(unsigned char v, char *password)
  119. {
  120.         int crypt_val;
  121.         unsigned char xor_value;
  122.         int i;
  123.        
  124.         crypt_val = assemble(password);
  125.         xor_value = v ^ (crypt_val >> 8) ^ (crypt_val & 0xff);
  126.         for(i = 0; i < keybits / 8; i++)
  127.             password[i] ^= xor_value;
  128.  
  129.         return xor_value;
  130. }
  131.  
  132. //--------------------------------------------------------------------
  133. int file_decrypt(FILE *f_in, FILE *f_out, char *password)
  134. {
  135.         long filesize, encrypted_len, block_size;
  136.         char buffer[1024] = { 0 };
  137.         unsigned char buffer2[1024] = { 0 };
  138.         int i;
  139.  
  140.         fseek(f_in, 0, SEEK_END);
  141.         filesize = ftell(f_in);
  142.         fseek(f_in, -74, SEEK_END);
  143.         fread(buffer, 1, 6, f_in);
  144.  
  145.         if(strncmp(buffer, "icpnas", 6))
  146.         {
  147.             printf("sig: %s %s\n", buffer, "icpnas");
  148.             return -1;
  149.         }
  150.         else
  151.         {
  152.             fread(&encrypted_len, 4, 1, f_in);
  153.             printf("len=%ld\n", encrypted_len);
  154.             fread(buffer, 1, 16, f_in);
  155.             printf("model name = %s\n", buffer);
  156.             fread(buffer, 1, 16, f_in);
  157.             printf("version = %s\n", buffer);
  158.             rewind(f_in);
  159.  
  160.             filesize -= encrypted_len;
  161.             filesize -= 74;
  162.         }
  163.  
  164.         while(encrypted_len > 0)
  165.         {
  166.             block_size = 1024;
  167.             if(encrypted_len < 1024)
  168.                 block_size = encrypted_len;
  169.             fread(buffer, 1, block_size, f_in);
  170.  
  171.             for(i = 0; i < block_size; i++)
  172.             {
  173.                 buffer2[i] = decrypt(buffer[i], password);
  174.             }
  175.            
  176.             fwrite(buffer2, 1, block_size, f_out);
  177.             encrypted_len -= block_size;
  178.         }
  179.  
  180.         while(filesize > 0)
  181.         {
  182.             block_size = 1024;
  183.             if(filesize < 1024)
  184.                 block_size = filesize;
  185.             fread(buffer, 1, block_size, f_in);
  186.             fwrite(buffer, 1, block_size, f_out);
  187.             filesize -= block_size;
  188.         }
  189.    
  190.         return 0;
  191. }
  192.  
  193. //--------------------------------------------------------------------
  194. int main(int argc, char* argv[])
  195. {
  196.     char *key;
  197.  
  198.     if (argc != 5)
  199.     {
  200.         fprintf(stderr, "Usage: %s e|d \"key\" sourcefile targetfile\n",
  201.             argv[0]);
  202.         fprintf(stderr, "%s d QNAPNASVERSION4 " \
  203.             "TS-219_3.5.0_Build0816.img " \
  204.             "TS-219_3.5.0_Build0816.img.tgz\n", argv[0]);
  205.         fprintf(stderr, "%s e QNAPNASVERSION4 " \
  206.             "TS-219_3.5.0_Build0816.img.tgz " \
  207.             "TS-219_3.5.0_Build0816.img\n", argv[0]);
  208.  
  209.         exit(1);
  210.     }
  211.  
  212.     FILE *f = fopen(argv[3], "rb");
  213.     if(f == NULL){
  214.         perror("file_in");
  215.         exit(2);
  216.     }
  217.  
  218.     FILE *f2 = fopen(argv[4], "wb");
  219.     if(f2 == NULL){
  220.         fclose(f);
  221.         perror("file_out");
  222.         exit(3);
  223.     }
  224.  
  225.     key = argv[2];
  226.     keybits = strlen(key) * 8;
  227.     printf("Using %d-bit encryption - (%s)\n", keybits, key);
  228.  
  229.     if(argv[1][0] == 'e')
  230.         file_encrypt(f, f2, ENCRYPT_SIZE, key);
  231.     else if(argv[1][0] == 'd')
  232.         file_decrypt(f, f2, key);
  233.     else
  234.         fprintf(stderr, "Unknown action\n");
  235.  
  236.     fclose(f);
  237.     fclose(f2);
  238.  
  239.     exit(0);
  240. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×