getpw.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* $Id$
  2. * MegaZeux
  3. *
  4. * Copyright (C) 1996 Greg Janson
  5. * Copyright (C) 1998 Matthew D. Williams - dbwilli@scsn.net
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <dir.h>
  23. #include <string.h>
  24. #include "meminter.h"
  25. unsigned char far magic_code[16]="æRëòmMJ·‡²’ˆÞ‘$";
  26. int pro_method,pw_size;
  27. unsigned char password[16],xor;
  28. unsigned char get_pw_xor_code(void) {
  29. int work=85;//Start with 85... (01010101)
  30. int t1;
  31. if(pro_method==0) return 0;
  32. //Clear pw after first null
  33. for(t1=strlen(password);t1<16;t1++)
  34. password[t1]=0;
  35. for(t1=0;t1<15;t1++) {
  36. //For each byte, roll once to the left and xor in pw byte if it
  37. //is an odd character, or add in pw byte if it is an even character.
  38. work<<=1;
  39. if(work>255) work^=257;//Wraparound from roll
  40. if(t1&1) {
  41. work+=password[t1];//Add (even byte)
  42. if(work>255) work^=257;//Wraparound from add
  43. }
  44. else work^=password[t1];//XOR (odd byte);
  45. }
  46. //To factor in protection method, add it in and roll one last time
  47. work+=pro_method;
  48. if(work>255) work^=257;
  49. work<<=1;
  50. if(work>255) work^=257;
  51. //Can't be 0-
  52. if(work==0) work=86;//(01010110)
  53. //Done!
  54. return (unsigned char)work;
  55. }
  56. void main(int argc,char *argv[]) {
  57. FILE *source;
  58. FILE *dest;
  59. int t1,t2;
  60. long len;
  61. printf("\n\n");
  62. if(argc<2) {
  63. printf("Usage:\n\nGETPW file.mzx [-d]\n\n-d will de-protect the file.\n\n\a");
  64. return;
  65. }
  66. source=fopen(argv[1],"rb");
  67. fseek(source,25,SEEK_CUR);
  68. pro_method=fgetc(source);//Protection method
  69. if(pro_method) {//Password
  70. fread(password,1,15,source);
  71. //First, normalize password...
  72. for(t1=0;t1<15;t1++) {
  73. password[t1]^=magic_code[t1];
  74. password[t1]-=0x12+pro_method;
  75. password[t1]^=0x8D;
  76. }
  77. //Show!
  78. printf("Password: %s\n",password);
  79. //Deprotect?
  80. if((argc>2)&&((argv[2][1]=='d')||(argv[2][1]=='D'))) {
  81. printf("Deprotecting... (to file NO_PW.MZX)\n");
  82. //Xor code
  83. xor=get_pw_xor_code();
  84. //De-xor entire file
  85. dest=fopen("NO_PW.MZX","wb");
  86. fseek(source,0,SEEK_END);
  87. len=ftell(source);
  88. fseek(source,0,SEEK_SET);
  89. for(t1=0;t1<25;t1++)
  90. fputc(fgetc(source),dest);
  91. fputc(0,dest);
  92. fseek(source,16,SEEK_CUR);
  93. for(t1=0;t1<3;t1++)
  94. fputc(t2=fgetc(source),dest);
  95. if(t2=='X') xor=0;//No xor for later files
  96. len-=44;
  97. for(;len>0;len--)
  98. fputc(fgetc(source)^xor,dest);
  99. fclose(dest);
  100. }
  101. }
  102. else printf("No password!\n\a");
  103. fclose(source);
  104. }