1 extern uint CRCTab[256];
2 
3 #define  rol(x,n,xsize)  (((x)<<(n)) | ((x)>>(xsize-(n))))
4 #define  ror(x,n,xsize)  (((x)>>(n)) | ((x)<<(xsize-(n))))
5 
SetKey13(const char * Password)6 void CryptData::SetKey13(const char *Password)
7 {
8   Key13[0]=Key13[1]=Key13[2]=0;
9   for (size_t I=0;Password[I]!=0;I++)
10   {
11     byte P=Password[I];
12     Key13[0]+=P;
13     Key13[1]^=P;
14     Key13[2]+=P;
15     Key13[2]=(byte)rol(Key13[2],1,8);
16   }
17 }
18 
19 
SetKey15(const char * Password)20 void CryptData::SetKey15(const char *Password)
21 {
22   InitCRC32(CRCTab);
23   uint PswCRC=CRC32(0xffffffff,Password,strlen(Password));
24   Key15[0]=PswCRC&0xffff;
25   Key15[1]=(PswCRC>>16)&0xffff;
26   Key15[2]=Key15[3]=0;
27   for (size_t I=0;Password[I]!=0;I++)
28   {
29     byte P=Password[I];
30     Key15[2]^=P^CRCTab[P];
31     Key15[3]+=P+(CRCTab[P]>>16);
32   }
33 }
34 
35 
SetAV15Encryption()36 void CryptData::SetAV15Encryption()
37 {
38   InitCRC32(CRCTab);
39   Method=CRYPT_RAR15;
40   Key15[0]=0x4765;
41   Key15[1]=0x9021;
42   Key15[2]=0x7382;
43   Key15[3]=0x5215;
44 }
45 
46 
SetCmt13Encryption()47 void CryptData::SetCmt13Encryption()
48 {
49   Method=CRYPT_RAR13;
50   Key13[0]=0;
51   Key13[1]=7;
52   Key13[2]=77;
53 }
54 
55 
Decrypt13(byte * Data,size_t Count)56 void CryptData::Decrypt13(byte *Data,size_t Count)
57 {
58   while (Count--)
59   {
60     Key13[1]+=Key13[2];
61     Key13[0]+=Key13[1];
62     *Data-=Key13[0];
63     Data++;
64   }
65 }
66 
67 
Crypt15(byte * Data,size_t Count)68 void CryptData::Crypt15(byte *Data,size_t Count)
69 {
70   while (Count--)
71   {
72     Key15[0]+=0x1234;
73     Key15[1]^=CRCTab[(Key15[0] & 0x1fe)>>1];
74     Key15[2]-=CRCTab[(Key15[0] & 0x1fe)>>1]>>16;
75     Key15[0]^=Key15[2];
76     Key15[3]=ror(Key15[3]&0xffff,1,16)^Key15[1];
77     Key15[3]=ror(Key15[3]&0xffff,1,16);
78     Key15[0]^=Key15[3];
79     *Data^=(byte)(Key15[0]>>8);
80     Data++;
81   }
82 }
83