vbakey.cpp
Upload User: hzbojue
Upload Date: 2007-01-05
Package Size: 33k
Code Size: 3k
Category:

Other systems

Development Platform:

DOS

  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <conio.h>
  4. #pragma hdrstop
  5. #include <condefs.h>
  6. #define BLOCK_SIZE 32768
  7. //---------------------------------------------------------------------------
  8. int memstr(const char *buf, const char *s, size_t size)
  9. {
  10.    int        off;
  11.    const char *s_temp;
  12.    //return -1;
  13.    if(!buf || !s || !(*s)) return -1;
  14.    for(off = 0; off < size; off++)
  15.    {
  16.       for(s_temp = s; *s_temp != 0 && *buf == *s_temp; buf++, s_temp++)
  17.       {
  18.          off++;
  19.          if(off >= size) return -1;
  20.       }
  21.       if((*(buf - 1) == *(s_temp - 1)) && *s_temp == 0) return off;
  22.       buf++;
  23.    }
  24.    return -1;
  25. }
  26. LPSTR findEncryptPass(const char *filename)
  27. {
  28.    FILE   *prot_file;
  29.    char   *buff = 0, *s;
  30.    size_t rc;
  31.    int    off;
  32.    prot_file = fopen(filename, "rb");
  33.    if(!prot_file) return 0;
  34.    buff = (char *)malloc(BLOCK_SIZE);
  35.    if(!buff) return 0;
  36.    do
  37.    {
  38.       rc = fread(buff, 1, BLOCK_SIZE, prot_file);
  39.       if(!rc) break;
  40.       off = memstr(buff, "DPB="", BLOCK_SIZE);
  41.       if(off < 0)
  42.       {
  43.          if(rc < BLOCK_SIZE) break;
  44.          fseek(prot_file, -32, SEEK_CUR);
  45.          continue;
  46.       }
  47.       fseek(prot_file, off - rc, SEEK_CUR);
  48.       rc = fread(buff, 1, BLOCK_SIZE, prot_file);
  49.       if(!rc) break;
  50.       s = strchr(buff, '"');
  51.       *s = 0;
  52.       return buff;
  53.    } while(!feof(prot_file));
  54.    free(buff);
  55.    return 0;
  56. }
  57. void decryptPassword(const char *encrypt_pass, char *s)
  58. {
  59.    char str[128], ch;
  60.    char hs[] = { 0, 0, 0 };
  61.    int  v1, v2, i, l;
  62.    int  begin_found = 0;
  63.    *s = 0;
  64.    for(i = 0; encrypt_pass[i*2]; i++)
  65.    {
  66.       hs[0] = encrypt_pass[i*2]; hs[1] = encrypt_pass[i*2+1];
  67.       v1 = strtol(hs, 0, 16);
  68.       hs[0] = encrypt_pass[i*2+4]; hs[1] = encrypt_pass[i*2+5];
  69.       v2 = strtol(hs, 0, 16);
  70.       if(!begin_found)
  71.       {
  72.          if(v1 == v2) begin_found = 1;
  73.       }
  74.       else
  75.       {
  76.          if(v1 != v2)
  77.             begin_found = 0;
  78.          else
  79.          {
  80.             i += 3;
  81.             break;
  82.          }
  83.       }
  84.    }
  85.    if(!begin_found) return;
  86.    for(ch = 0, l = 0; encrypt_pass[i*2+2]; i++, l++)
  87.    {
  88.       hs[0] = encrypt_pass[(i-2)*2]; hs[1] = encrypt_pass[(i-2)*2+1];
  89.       v1 = strtol(hs, 0, 16);
  90.       hs[0] = encrypt_pass[i*2]; hs[1] = encrypt_pass[i*2+1];
  91.       v2 = strtol(hs, 0, 16);
  92.       ch = (ch + (char)v1) ^ (char)v2;
  93.       str[l] = ch;
  94.    }
  95.    str[l] = 0;
  96.    CharToOem(str, s);
  97. }
  98. #pragma argsused
  99. int main(int argc, char **argv)
  100. {
  101.    char s[128];
  102.    char *encrypt_pass_str;
  103.    if(argc < 2)
  104.    {
  105.       printf("Usage: vbakey.exe filenamen");
  106.       return 1;
  107.    }
  108.    if(!(encrypt_pass_str = findEncryptPass(argv[1])))
  109.    {
  110.       //CharToOem("相痤朦 礤 磬殇屙n", s);
  111.       printf("Password not foundn");
  112.       return 1;
  113.    }
  114.    decryptPassword(encrypt_pass_str, s);
  115.    printf("File password: %sn", s);
  116.    free(encrypt_pass_str);
  117.    printf("Press any key for continue ... n");
  118.    getch();
  119.    return 0;
  120. }