home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd1.bin / zkuste / delphi / kompon / d56 / VKDBF.ZIP / VKDBFCrypt.pas < prev    next >
Pascal/Delphi Source File  |  2002-02-21  |  25KB  |  642 lines

  1. {
  2.  Copyright:      Vlad Karpov  mailto:KarpovVV@protek.ru
  3.  Author:         Vlad Karpov
  4.  Remarks:        Based on DEC Hagen Reddmann  mailto:HaReddmann@AOL.COM
  5. }
  6. unit VKDBFCrypt;
  7.  
  8. interface
  9.  
  10. uses
  11.   Windows, Messages, SysUtils, Classes;
  12.  
  13. const
  14.   Gost_Data: array[0..3, 0..255] of LongWord = (
  15.    ($00072000,$00075000,$00074800,$00071000,$00076800,$00074000,$00070000,$00077000,
  16.     $00073000,$00075800,$00070800,$00076000,$00073800,$00077800,$00072800,$00071800,
  17.     $0005A000,$0005D000,$0005C800,$00059000,$0005E800,$0005C000,$00058000,$0005F000,
  18.     $0005B000,$0005D800,$00058800,$0005E000,$0005B800,$0005F800,$0005A800,$00059800,
  19.     $00022000,$00025000,$00024800,$00021000,$00026800,$00024000,$00020000,$00027000,
  20.     $00023000,$00025800,$00020800,$00026000,$00023800,$00027800,$00022800,$00021800,
  21.     $00062000,$00065000,$00064800,$00061000,$00066800,$00064000,$00060000,$00067000,
  22.     $00063000,$00065800,$00060800,$00066000,$00063800,$00067800,$00062800,$00061800,
  23.     $00032000,$00035000,$00034800,$00031000,$00036800,$00034000,$00030000,$00037000,
  24.     $00033000,$00035800,$00030800,$00036000,$00033800,$00037800,$00032800,$00031800,
  25.     $0006A000,$0006D000,$0006C800,$00069000,$0006E800,$0006C000,$00068000,$0006F000,
  26.     $0006B000,$0006D800,$00068800,$0006E000,$0006B800,$0006F800,$0006A800,$00069800,
  27.     $0007A000,$0007D000,$0007C800,$00079000,$0007E800,$0007C000,$00078000,$0007F000,
  28.     $0007B000,$0007D800,$00078800,$0007E000,$0007B800,$0007F800,$0007A800,$00079800,
  29.     $00052000,$00055000,$00054800,$00051000,$00056800,$00054000,$00050000,$00057000,
  30.     $00053000,$00055800,$00050800,$00056000,$00053800,$00057800,$00052800,$00051800,
  31.     $00012000,$00015000,$00014800,$00011000,$00016800,$00014000,$00010000,$00017000,
  32.     $00013000,$00015800,$00010800,$00016000,$00013800,$00017800,$00012800,$00011800,
  33.     $0001A000,$0001D000,$0001C800,$00019000,$0001E800,$0001C000,$00018000,$0001F000,
  34.     $0001B000,$0001D800,$00018800,$0001E000,$0001B800,$0001F800,$0001A800,$00019800,
  35.     $00042000,$00045000,$00044800,$00041000,$00046800,$00044000,$00040000,$00047000,
  36.     $00043000,$00045800,$00040800,$00046000,$00043800,$00047800,$00042800,$00041800,
  37.     $0000A000,$0000D000,$0000C800,$00009000,$0000E800,$0000C000,$00008000,$0000F000,
  38.     $0000B000,$0000D800,$00008800,$0000E000,$0000B800,$0000F800,$0000A800,$00009800,
  39.     $00002000,$00005000,$00004800,$00001000,$00006800,$00004000,$00000000,$00007000,
  40.     $00003000,$00005800,$00000800,$00006000,$00003800,$00007800,$00002800,$00001800,
  41.     $0003A000,$0003D000,$0003C800,$00039000,$0003E800,$0003C000,$00038000,$0003F000,
  42.     $0003B000,$0003D800,$00038800,$0003E000,$0003B800,$0003F800,$0003A800,$00039800,
  43.     $0002A000,$0002D000,$0002C800,$00029000,$0002E800,$0002C000,$00028000,$0002F000,
  44.     $0002B000,$0002D800,$00028800,$0002E000,$0002B800,$0002F800,$0002A800,$00029800,
  45.     $0004A000,$0004D000,$0004C800,$00049000,$0004E800,$0004C000,$00048000,$0004F000,
  46.     $0004B000,$0004D800,$00048800,$0004E000,$0004B800,$0004F800,$0004A800,$00049800),
  47.    ($03A80000,$03C00000,$03880000,$03E80000,$03D00000,$03980000,$03A00000,$03900000,
  48.     $03F00000,$03F80000,$03E00000,$03B80000,$03B00000,$03800000,$03C80000,$03D80000,
  49.     $06A80000,$06C00000,$06880000,$06E80000,$06D00000,$06980000,$06A00000,$06900000,
  50.     $06F00000,$06F80000,$06E00000,$06B80000,$06B00000,$06800000,$06C80000,$06D80000,
  51.     $05280000,$05400000,$05080000,$05680000,$05500000,$05180000,$05200000,$05100000,
  52.     $05700000,$05780000,$05600000,$05380000,$05300000,$05000000,$05480000,$05580000,
  53.     $00A80000,$00C00000,$00880000,$00E80000,$00D00000,$00980000,$00A00000,$00900000,
  54.     $00F00000,$00F80000,$00E00000,$00B80000,$00B00000,$00800000,$00C80000,$00D80000,
  55.     $00280000,$00400000,$00080000,$00680000,$00500000,$00180000,$00200000,$00100000,
  56.     $00700000,$00780000,$00600000,$00380000,$00300000,$00000000,$00480000,$00580000,
  57.     $04280000,$04400000,$04080000,$04680000,$04500000,$04180000,$04200000,$04100000,
  58.     $04700000,$04780000,$04600000,$04380000,$04300000,$04000000,$04480000,$04580000,
  59.     $04A80000,$04C00000,$04880000,$04E80000,$04D00000,$04980000,$04A00000,$04900000,
  60.     $04F00000,$04F80000,$04E00000,$04B80000,$04B00000,$04800000,$04C80000,$04D80000,
  61.     $07A80000,$07C00000,$07880000,$07E80000,$07D00000,$07980000,$07A00000,$07900000,
  62.     $07F00000,$07F80000,$07E00000,$07B80000,$07B00000,$07800000,$07C80000,$07D80000,
  63.     $07280000,$07400000,$07080000,$07680000,$07500000,$07180000,$07200000,$07100000,
  64.     $07700000,$07780000,$07600000,$07380000,$07300000,$07000000,$07480000,$07580000,
  65.     $02280000,$02400000,$02080000,$02680000,$02500000,$02180000,$02200000,$02100000,
  66.     $02700000,$02780000,$02600000,$02380000,$02300000,$02000000,$02480000,$02580000,
  67.     $03280000,$03400000,$03080000,$03680000,$03500000,$03180000,$03200000,$03100000,
  68.     $03700000,$03780000,$03600000,$03380000,$03300000,$03000000,$03480000,$03580000,
  69.     $06280000,$06400000,$06080000,$06680000,$06500000,$06180000,$06200000,$06100000,
  70.     $06700000,$06780000,$06600000,$06380000,$06300000,$06000000,$06480000,$06580000,
  71.     $05A80000,$05C00000,$05880000,$05E80000,$05D00000,$05980000,$05A00000,$05900000,
  72.     $05F00000,$05F80000,$05E00000,$05B80000,$05B00000,$05800000,$05C80000,$05D80000,
  73.     $01280000,$01400000,$01080000,$01680000,$01500000,$01180000,$01200000,$01100000,
  74.     $01700000,$01780000,$01600000,$01380000,$01300000,$01000000,$01480000,$01580000,
  75.     $02A80000,$02C00000,$02880000,$02E80000,$02D00000,$02980000,$02A00000,$02900000,
  76.     $02F00000,$02F80000,$02E00000,$02B80000,$02B00000,$02800000,$02C80000,$02D80000,
  77.     $01A80000,$01C00000,$01880000,$01E80000,$01D00000,$01980000,$01A00000,$01900000,
  78.     $01F00000,$01F80000,$01E00000,$01B80000,$01B00000,$01800000,$01C80000,$01D80000),
  79.    ($30000002,$60000002,$38000002,$08000002,$28000002,$78000002,$68000002,$40000002,
  80.     $20000002,$50000002,$48000002,$70000002,$00000002,$18000002,$58000002,$10000002,
  81.     $B0000005,$E0000005,$B8000005,$88000005,$A8000005,$F8000005,$E8000005,$C0000005,
  82.     $A0000005,$D0000005,$C8000005,$F0000005,$80000005,$98000005,$D8000005,$90000005,
  83.     $30000005,$60000005,$38000005,$08000005,$28000005,$78000005,$68000005,$40000005,
  84.     $20000005,$50000005,$48000005,$70000005,$00000005,$18000005,$58000005,$10000005,
  85.     $30000000,$60000000,$38000000,$08000000,$28000000,$78000000,$68000000,$40000000,
  86.     $20000000,$50000000,$48000000,$70000000,$00000000,$18000000,$58000000,$10000000,
  87.     $B0000003,$E0000003,$B8000003,$88000003,$A8000003,$F8000003,$E8000003,$C0000003,
  88.     $A0000003,$D0000003,$C8000003,$F0000003,$80000003,$98000003,$D8000003,$90000003,
  89.     $30000001,$60000001,$38000001,$08000001,$28000001,$78000001,$68000001,$40000001,
  90.     $20000001,$50000001,$48000001,$70000001,$00000001,$18000001,$58000001,$10000001,
  91.     $B0000000,$E0000000,$B8000000,$88000000,$A8000000,$F8000000,$E8000000,$C0000000,
  92.     $A0000000,$D0000000,$C8000000,$F0000000,$80000000,$98000000,$D8000000,$90000000,
  93.     $B0000006,$E0000006,$B8000006,$88000006,$A8000006,$F8000006,$E8000006,$C0000006,
  94.     $A0000006,$D0000006,$C8000006,$F0000006,$80000006,$98000006,$D8000006,$90000006,
  95.     $B0000001,$E0000001,$B8000001,$88000001,$A8000001,$F8000001,$E8000001,$C0000001,
  96.     $A0000001,$D0000001,$C8000001,$F0000001,$80000001,$98000001,$D8000001,$90000001,
  97.     $30000003,$60000003,$38000003,$08000003,$28000003,$78000003,$68000003,$40000003,
  98.     $20000003,$50000003,$48000003,$70000003,$00000003,$18000003,$58000003,$10000003,
  99.     $30000004,$60000004,$38000004,$08000004,$28000004,$78000004,$68000004,$40000004,
  100.     $20000004,$50000004,$48000004,$70000004,$00000004,$18000004,$58000004,$10000004,
  101.     $B0000002,$E0000002,$B8000002,$88000002,$A8000002,$F8000002,$E8000002,$C0000002,
  102.     $A0000002,$D0000002,$C8000002,$F0000002,$80000002,$98000002,$D8000002,$90000002,
  103.     $B0000004,$E0000004,$B8000004,$88000004,$A8000004,$F8000004,$E8000004,$C0000004,
  104.     $A0000004,$D0000004,$C8000004,$F0000004,$80000004,$98000004,$D8000004,$90000004,
  105.     $30000006,$60000006,$38000006,$08000006,$28000006,$78000006,$68000006,$40000006,
  106.     $20000006,$50000006,$48000006,$70000006,$00000006,$18000006,$58000006,$10000006,
  107.     $B0000007,$E0000007,$B8000007,$88000007,$A8000007,$F8000007,$E8000007,$C0000007,
  108.     $A0000007,$D0000007,$C8000007,$F0000007,$80000007,$98000007,$D8000007,$90000007,
  109.     $30000007,$60000007,$38000007,$08000007,$28000007,$78000007,$68000007,$40000007,
  110.     $20000007,$50000007,$48000007,$70000007,$00000007,$18000007,$58000007,$10000007),
  111.    ($000000E8,$000000D8,$000000A0,$00000088,$00000098,$000000F8,$000000A8,$000000C8,
  112.     $00000080,$000000D0,$000000F0,$000000B8,$000000B0,$000000C0,$00000090,$000000E0,
  113.     $000007E8,$000007D8,$000007A0,$00000788,$00000798,$000007F8,$000007A8,$000007C8,
  114.     $00000780,$000007D0,$000007F0,$000007B8,$000007B0,$000007C0,$00000790,$000007E0,
  115.     $000006E8,$000006D8,$000006A0,$00000688,$00000698,$000006F8,$000006A8,$000006C8,
  116.     $00000680,$000006D0,$000006F0,$000006B8,$000006B0,$000006C0,$00000690,$000006E0,
  117.     $00000068,$00000058,$00000020,$00000008,$00000018,$00000078,$00000028,$00000048,
  118.     $00000000,$00000050,$00000070,$00000038,$00000030,$00000040,$00000010,$00000060,
  119.     $000002E8,$000002D8,$000002A0,$00000288,$00000298,$000002F8,$000002A8,$000002C8,
  120.     $00000280,$000002D0,$000002F0,$000002B8,$000002B0,$000002C0,$00000290,$000002E0,
  121.     $000003E8,$000003D8,$000003A0,$00000388,$00000398,$000003F8,$000003A8,$000003C8,
  122.     $00000380,$000003D0,$000003F0,$000003B8,$000003B0,$000003C0,$00000390,$000003E0,
  123.     $00000568,$00000558,$00000520,$00000508,$00000518,$00000578,$00000528,$00000548,
  124.     $00000500,$00000550,$00000570,$00000538,$00000530,$00000540,$00000510,$00000560,
  125.     $00000268,$00000258,$00000220,$00000208,$00000218,$00000278,$00000228,$00000248,
  126.     $00000200,$00000250,$00000270,$00000238,$00000230,$00000240,$00000210,$00000260,
  127.     $000004E8,$000004D8,$000004A0,$00000488,$00000498,$000004F8,$000004A8,$000004C8,
  128.     $00000480,$000004D0,$000004F0,$000004B8,$000004B0,$000004C0,$00000490,$000004E0,
  129.     $00000168,$00000158,$00000120,$00000108,$00000118,$00000178,$00000128,$00000148,
  130.     $00000100,$00000150,$00000170,$00000138,$00000130,$00000140,$00000110,$00000160,
  131.     $000001E8,$000001D8,$000001A0,$00000188,$00000198,$000001F8,$000001A8,$000001C8,
  132.     $00000180,$000001D0,$000001F0,$000001B8,$000001B0,$000001C0,$00000190,$000001E0,
  133.     $00000768,$00000758,$00000720,$00000708,$00000718,$00000778,$00000728,$00000748,
  134.     $00000700,$00000750,$00000770,$00000738,$00000730,$00000740,$00000710,$00000760,
  135.     $00000368,$00000358,$00000320,$00000308,$00000318,$00000378,$00000328,$00000348,
  136.     $00000300,$00000350,$00000370,$00000338,$00000330,$00000340,$00000310,$00000360,
  137.     $000005E8,$000005D8,$000005A0,$00000588,$00000598,$000005F8,$000005A8,$000005C8,
  138.     $00000580,$000005D0,$000005F0,$000005B8,$000005B0,$000005C0,$00000590,$000005E0,
  139.     $00000468,$00000458,$00000420,$00000408,$00000418,$00000478,$00000428,$00000448,
  140.     $00000400,$00000450,$00000470,$00000438,$00000430,$00000440,$00000410,$00000460,
  141.     $00000668,$00000658,$00000620,$00000608,$00000618,$00000678,$00000628,$00000648,
  142.     $00000600,$00000650,$00000670,$00000638,$00000630,$00000640,$00000610,$00000660));
  143.  
  144.   TableCRC32:  ARRAY[0..255] OF DWORD =
  145.    ($00000000, $77073096, $EE0E612C, $990951BA,
  146.     $076DC419, $706AF48F, $E963A535, $9E6495A3,
  147.     $0EDB8832, $79DCB8A4, $E0D5E91E, $97D2D988,
  148.     $09B64C2B, $7EB17CBD, $E7B82D07, $90BF1D91,
  149.     $1DB71064, $6AB020F2, $F3B97148, $84BE41DE,
  150.     $1ADAD47D, $6DDDE4EB, $F4D4B551, $83D385C7,
  151.     $136C9856, $646BA8C0, $FD62F97A, $8A65C9EC,
  152.     $14015C4F, $63066CD9, $FA0F3D63, $8D080DF5,
  153.     $3B6E20C8, $4C69105E, $D56041E4, $A2677172,
  154.     $3C03E4D1, $4B04D447, $D20D85FD, $A50AB56B,
  155.     $35B5A8FA, $42B2986C, $DBBBC9D6, $ACBCF940,
  156.     $32D86CE3, $45DF5C75, $DCD60DCF, $ABD13D59,
  157.     $26D930AC, $51DE003A, $C8D75180, $BFD06116,
  158.     $21B4F4B5, $56B3C423, $CFBA9599, $B8BDA50F,
  159.     $2802B89E, $5F058808, $C60CD9B2, $B10BE924,
  160.     $2F6F7C87, $58684C11, $C1611DAB, $B6662D3D,
  161.  
  162.     $76DC4190, $01DB7106, $98D220BC, $EFD5102A,
  163.     $71B18589, $06B6B51F, $9FBFE4A5, $E8B8D433,
  164.     $7807C9A2, $0F00F934, $9609A88E, $E10E9818,
  165.     $7F6A0DBB, $086D3D2D, $91646C97, $E6635C01,
  166.     $6B6B51F4, $1C6C6162, $856530D8, $F262004E,
  167.     $6C0695ED, $1B01A57B, $8208F4C1, $F50FC457,
  168.     $65B0D9C6, $12B7E950, $8BBEB8EA, $FCB9887C,
  169.     $62DD1DDF, $15DA2D49, $8CD37CF3, $FBD44C65,
  170.     $4DB26158, $3AB551CE, $A3BC0074, $D4BB30E2,
  171.     $4ADFA541, $3DD895D7, $A4D1C46D, $D3D6F4FB,
  172.     $4369E96A, $346ED9FC, $AD678846, $DA60B8D0,
  173.     $44042D73, $33031DE5, $AA0A4C5F, $DD0D7CC9,
  174.     $5005713C, $270241AA, $BE0B1010, $C90C2086,
  175.     $5768B525, $206F85B3, $B966D409, $CE61E49F,
  176.     $5EDEF90E, $29D9C998, $B0D09822, $C7D7A8B4,
  177.     $59B33D17, $2EB40D81, $B7BD5C3B, $C0BA6CAD,
  178.  
  179.     $EDB88320, $9ABFB3B6, $03B6E20C, $74B1D29A,
  180.     $EAD54739, $9DD277AF, $04DB2615, $73DC1683,
  181.     $E3630B12, $94643B84, $0D6D6A3E, $7A6A5AA8,
  182.     $E40ECF0B, $9309FF9D, $0A00AE27, $7D079EB1,
  183.     $F00F9344, $8708A3D2, $1E01F268, $6906C2FE,
  184.     $F762575D, $806567CB, $196C3671, $6E6B06E7,
  185.     $FED41B76, $89D32BE0, $10DA7A5A, $67DD4ACC,
  186.     $F9B9DF6F, $8EBEEFF9, $17B7BE43, $60B08ED5,
  187.     $D6D6A3E8, $A1D1937E, $38D8C2C4, $4FDFF252,
  188.     $D1BB67F1, $A6BC5767, $3FB506DD, $48B2364B,
  189.     $D80D2BDA, $AF0A1B4C, $36034AF6, $41047A60,
  190.     $DF60EFC3, $A867DF55, $316E8EEF, $4669BE79,
  191.     $CB61B38C, $BC66831A, $256FD2A0, $5268E236,
  192.     $CC0C7795, $BB0B4703, $220216B9, $5505262F,
  193.     $C5BA3BBE, $B2BD0B28, $2BB45A92, $5CB36A04,
  194.     $C2D7FFA7, $B5D0CF31, $2CD99E8B, $5BDEAE1D,
  195.  
  196.     $9B64C2B0, $EC63F226, $756AA39C, $026D930A,
  197.     $9C0906A9, $EB0E363F, $72076785, $05005713,
  198.     $95BF4A82, $E2B87A14, $7BB12BAE, $0CB61B38,
  199.     $92D28E9B, $E5D5BE0D, $7CDCEFB7, $0BDBDF21,
  200.     $86D3D2D4, $F1D4E242, $68DDB3F8, $1FDA836E,
  201.     $81BE16CD, $F6B9265B, $6FB077E1, $18B74777,
  202.     $88085AE6, $FF0F6A70, $66063BCA, $11010B5C,
  203.     $8F659EFF, $F862AE69, $616BFFD3, $166CCF45,
  204.     $A00AE278, $D70DD2EE, $4E048354, $3903B3C2,
  205.     $A7672661, $D06016F7, $4969474D, $3E6E77DB,
  206.     $AED16A4A, $D9D65ADC, $40DF0B66, $37D83BF0,
  207.     $A9BCAE53, $DEBB9EC5, $47B2CF7F, $30B5FFE9,
  208.     $BDBDF21C, $CABAC28A, $53B39330, $24B4A3A6,
  209.     $BAD03605, $CDD70693, $54DE5729, $23D967BF,
  210.     $B3667A2E, $C4614AB8, $5D681B02, $2A6F2B94,
  211.     $B40BBE37, $C30C8EA1, $5A05DF1B, $2D02EF8D);
  212.  
  213. type
  214.  
  215.   TGostN = packed record
  216.     N1: LongWord;
  217.     N2: LongWord;
  218.   end;
  219.   pGostN = ^TGostN;
  220.  
  221.   TGostNB = array [0..7] of Byte;
  222.   pGostNB = ^TGostNB;
  223.  
  224.   TGostKey = array [0..7] of LongWord;
  225.   pGostKey = ^TGostKey;
  226.  
  227.   TGostCrypt = class
  228.   protected
  229.     procedure XorBuffer(p1, p2: Pointer; Size: Integer);
  230.   public
  231.     Gamma0: TGostN;
  232.     Key: TGostKey;
  233.     Password: String;
  234.     procedure Encode(var N: TGostN);
  235.     procedure Decode(var N: TGostN);
  236.     procedure EncodeBuffer(Buff: Pointer; Size: Integer);
  237.     procedure DecodeBuffer(Buff: Pointer; Size: Integer);
  238.     procedure CodeStream(Source, Dest: TStream; DataSize: Integer; Encode: Boolean);
  239.     procedure CodeFile(const Source, Dest: String; Encode: Boolean);
  240.     class procedure Gamma(var N: TGostN);
  241.     procedure InitKey;
  242.   end;
  243.  
  244.   function CRC32(S: String): LongWord;
  245.  
  246.   { XOR by Gamma method }
  247.   function XORActivate(Password: String): LongWord;
  248.   procedure XOREncrypt(Id: LongWord; Context: LongWord; Buff: Pointer; Size: Integer);
  249.   procedure XORDecrypt(Id: LongWord; Context: LongWord; Buff: Pointer; Size: Integer);
  250.   procedure XORDeactivate(Id: LongWord);
  251.  
  252.   { Russian GOST 28147-89 Method }
  253.   function GostActivate(Password: String): LongWord;
  254.   procedure GostEncrypt(Id: LongWord; Context: LongWord; Buff: Pointer; Size: Integer);
  255.   procedure GostDecrypt(Id: LongWord; Context: LongWord; Buff: Pointer; Size: Integer);
  256.   procedure GostDeactivate(Id: LongWord);
  257.  
  258.  
  259. implementation
  260.  
  261. function CRC32(S: String): LongWord;
  262. var
  263.   i, j: Integer;
  264.     CC: Byte;
  265.   FCRC32: LongWord;
  266. begin
  267.   FCRC32 := $FFFFFFFF;
  268.   j := Length(S);
  269.   for i := 1 to j do begin
  270.     CC := Byte(S[i]);
  271.     FCRC32 := (FCRC32 SHR 8) XOR TableCRC32[ CC XOR (FCRC32 AND $000000FF) ];
  272.   end;
  273.   FCRC32 := NOT FCRC32;
  274.   Result := FCRC32;
  275. end;
  276.  
  277. { RU GOST 28147_89 Methods }
  278.  
  279. function GostActivate(Password: String): LongWord;
  280. var
  281.   oCrypt: TGostCrypt;
  282. begin
  283.   oCrypt := TGostCrypt.Create;
  284.   oCrypt.Password := Password;
  285.   oCrypt.InitKey;
  286.   Result := LongInt(Pointer(oCrypt));
  287. end;
  288.  
  289. procedure GostEncrypt(Id: LongWord; Context: LongWord; Buff: Pointer; Size: Integer);
  290. var
  291.   oCrypt: TGostCrypt;
  292. begin
  293.   oCrypt := TGostCrypt(Pointer(Id));
  294.   oCrypt.Gamma0.N2 := TableCRC32[Byte(Context)];
  295.   oCrypt.Gamma0.N1 := TableCRC32[255 - Byte(Context)] xor oCrypt.Gamma0.N2;
  296.   oCrypt.EncodeBuffer(Buff, Size);
  297. end;
  298.  
  299. procedure GostDecrypt(Id: LongWord; Context: LongWord; Buff: Pointer; Size: Integer);
  300. var
  301.   oCrypt: TGostCrypt;
  302. begin
  303.   oCrypt := TGostCrypt(Pointer(Id));
  304.   oCrypt.Gamma0.N2 := TableCRC32[Byte(Context)];
  305.   oCrypt.Gamma0.N1 := TableCRC32[255 - Byte(Context)] xor oCrypt.Gamma0.N2;
  306.   oCrypt.DecodeBuffer(Buff, Size);
  307. end;
  308.  
  309. procedure GostDeactivate(Id: LongWord);
  310. var
  311.   oCrypt: TGostCrypt;
  312. begin
  313.   oCrypt := TGostCrypt(Pointer(Id));
  314.   oCrypt.Free;
  315. end;
  316.  
  317. { XOR by Gamma method }
  318.  
  319. function XORActivate(Password: String): LongWord;
  320. var
  321.   oCrypt: TGostCrypt;
  322. begin
  323.   oCrypt := TGostCrypt.Create;
  324.   oCrypt.Password := Password;
  325.   oCrypt.InitKey;
  326.   Result := LongInt(Pointer(oCrypt));
  327. end;
  328.  
  329. procedure XOREncrypt(Id: LongWord; Context: LongWord; Buff: Pointer; Size: Integer);
  330. var
  331.   oCrypt: TGostCrypt;
  332.   i, j: Integer;
  333.   c: pChar;
  334.   Gamma0: TGostN;
  335. begin
  336.   oCrypt := TGostCrypt(Pointer(Id));
  337.   Gamma0 := oCrypt.Gamma0;
  338.   Gamma0.N2 := TableCRC32[Byte(Context)];
  339.   Gamma0.N1 := Gamma0.N1 xor Gamma0.N2;
  340.   c := pChar(Buff);
  341.   j := 0;
  342.   for i := 0 to Size - 1 do begin
  343.     Byte(c[i]) := Byte(c[i]) xor pGostNB(@Gamma0)[j];
  344.     Inc(j);
  345.     if j = 8 then begin
  346.       oCrypt.Gamma(Gamma0);
  347.       j := 0;
  348.     end;
  349.   end;
  350. end;
  351.  
  352. procedure XORDecrypt(Id: LongWord; Context: LongWord; Buff: Pointer; Size: Integer);
  353. begin
  354.   XOREncrypt(Id, Context, Buff, Size);
  355. end;
  356.  
  357. procedure XORDeactivate(Id: LongWord);
  358. var
  359.   oCrypt: TGostCrypt;
  360. begin
  361.   oCrypt := TGostCrypt(Pointer(Id));
  362.   oCrypt.Free;
  363. end;
  364.  
  365. { TGostCrypt }
  366.  
  367. procedure TGostCrypt.CodeFile(const Source, Dest: String; Encode: Boolean);
  368. var
  369.   S,D: TFileStream;
  370. begin
  371.   S := nil;
  372.   D := nil;
  373.   try
  374.     if (AnsiCompareText(Source, Dest) <> 0) and (Trim(Dest) <> '') then
  375.     begin
  376.       S := TFileStream.Create(Source, fmOpenRead or fmShareDenyNone);
  377.       D := TFileStream.Create(Dest, fmCreate);
  378.     end else
  379.     begin
  380.       S := TFileStream.Create(Source, fmOpenReadWrite);
  381.       D := S;
  382.     end;
  383.     CodeStream(S, D, -1, Encode);
  384.   finally
  385.     S.Free;
  386.     if S <> D then
  387.     begin
  388.       D.Free;
  389.     end;
  390.   end;
  391. end;
  392.  
  393. procedure TGostCrypt.CodeStream(Source, Dest: TStream; DataSize: Integer; Encode: Boolean);
  394. const
  395.   maxBufSize = 1024 * 4;
  396. var
  397.   Buf: PChar;
  398.   SPos: Integer;
  399.   DPos: Integer;
  400.   Len: Integer;
  401.   Proc: procedure(Buff: Pointer; Size: Integer) of object;
  402. begin
  403.   if Source = nil then Exit;
  404.   if Encode then Proc := EncodeBuffer
  405.     else Proc := DecodeBuffer;
  406.   if Dest = nil then Dest := Source;
  407.   if DataSize < 0 then
  408.   begin
  409.     DataSize := Source.Size;
  410.     Source.Position := 0;
  411.   end;
  412.   Buf := nil;
  413.   try
  414.     Buf    := AllocMem(maxBufSize);
  415.     DPos   := Dest.Position;
  416.     SPos   := Source.Position;
  417.     while DataSize > 0 do
  418.     begin
  419.       Source.Position := SPos;
  420.       Len := DataSize;
  421.       if Len > maxBufSize then Len := maxBufSize;
  422.       Len := Source.Read(Buf^, Len);
  423.       SPos := Source.Position;
  424.       if Len <= 0 then Break;
  425.       Proc(Buf, Len);
  426.       Dest.Position := DPos;
  427.       Dest.Write(Buf^, Len);
  428.       DPos := Dest.Position;
  429.       Dec(DataSize, Len);
  430.     end;
  431.   finally
  432.     ReallocMem(Buf, 0);
  433.   end;
  434. end;
  435.  
  436. procedure TGostCrypt.Decode(var N: TGostN);
  437. var
  438.   I,A,B,T: LongWord;
  439.   K: pGostKey;
  440.  
  441.   function AddLong(A, B: LongWord): LongWord;
  442.   asm
  443.     ADD EAX, EDX
  444.   end;
  445.  
  446. begin
  447.   A := N.N1;
  448.   B := N.N2;
  449.   K := @Key[0];
  450.   for I := 0 to 3 do
  451.   begin
  452.     T := AddLong(A, K[0]);
  453.     B := B xor Gost_Data[0, T and $FF] xor
  454.                Gost_Data[1, T shr  8 and $FF] xor
  455.                Gost_Data[2, T shr 16 and $FF] xor
  456.                Gost_Data[3, T shr 24];
  457.     T := AddLong(B, K[1]);
  458.     A := A xor Gost_Data[0, T and $FF] xor
  459.                Gost_Data[1, T shr  8 and $FF] xor
  460.                Gost_Data[2, T shr 16 and $FF] xor
  461.                Gost_Data[3, T shr 24];
  462.     Inc(PInteger(K), 2);
  463.   end;
  464.   for I := 0 to 11 do
  465.   begin
  466.     if I and 3 = 0 then K := @Key[6];
  467.     T := AddLong(A, K[1]);
  468.     B := B xor Gost_Data[0, T and $FF] xor
  469.                Gost_Data[1, T shr  8 and $FF] xor
  470.                Gost_Data[2, T shr 16 and $FF] xor
  471.                Gost_Data[3, T shr 24];
  472.     T := AddLong(B, K[0]);
  473.     A := A xor Gost_Data[0, T and $FF] xor
  474.                Gost_Data[1, T shr  8 and $FF] xor
  475.                Gost_Data[2, T shr 16 and $FF] xor
  476.                Gost_Data[3, T shr 24];
  477.     Dec(PInteger(K), 2);
  478.   end;
  479.   N.N1 := B;
  480.   N.N2 := A;
  481. end;
  482.  
  483. procedure TGostCrypt.DecodeBuffer(Buff: Pointer; Size: Integer);
  484. var
  485.   D: pByte;
  486.   N: TGostN;
  487. begin
  488.   N.N1 := 0;
  489.   N.N2 := 0;
  490.   D := Buff;
  491.   while Size >= 8 do
  492.   begin
  493.     Encode(Gamma0);
  494.     Gamma(Gamma0);
  495.     pGostN(D).N1 := pGostN(D).N1 xor Gamma0.N1;
  496.     pGostN(D).N2 := pGostN(D).N2 xor Gamma0.N2;
  497.     Decode(pGostN(D)^);
  498.     Inc(D, 8);
  499.     Dec(Size, 8);
  500.   end;
  501.   if Size > 0 then
  502.   begin
  503.     Move(D^, N, Size);
  504.     Encode(Gamma0);
  505.     Gamma(Gamma0);
  506.     XorBuffer(@N, @Gamma0, 8);
  507.     Move(N, D^, Size);
  508.   end;
  509. end;
  510.  
  511. procedure TGostCrypt.Encode(var N: TGostN);
  512. var
  513.   I,A,B,T: LongWord;
  514.   K: pGostKey;
  515.  
  516.   function AddLong(A, B: LongWord): LongWord;
  517.   asm
  518.     ADD EAX, EDX
  519.   end;
  520.  
  521. begin
  522.   K := @Key[0];
  523.   A := N.N1;
  524.   B := N.N2;
  525.   for I := 0 to 11 do
  526.   begin
  527.     if I and 3 = 0 then K := @Key[0];
  528.     T := AddLong(A, K[0]);
  529.     B := B xor Gost_Data[0, T        and $FF] xor
  530.                Gost_Data[1, T shr  8 and $FF] xor
  531.                Gost_Data[2, T shr 16 and $FF] xor
  532.                Gost_Data[3, T shr 24        ];
  533.     T := AddLong(B, K[1]);
  534.     A := A xor Gost_Data[0, T        and $FF] xor
  535.                Gost_Data[1, T shr  8 and $FF] xor
  536.                Gost_Data[2, T shr 16 and $FF] xor
  537.                Gost_Data[3, T shr 24        ];
  538.     Inc(PInteger(K), 2);
  539.   end;
  540.   K := @Key[6];
  541.   for I := 0 to 3 do
  542.   begin
  543.     T := AddLong(A, K[1]);
  544.     B := B xor Gost_Data[0, T        and $FF] xor
  545.                Gost_Data[1, T shr  8 and $FF] xor
  546.                Gost_Data[2, T shr 16 and $FF] xor
  547.                Gost_Data[3, T shr 24        ];
  548.     T := AddLong(B, K[0]);
  549.     A := A xor Gost_Data[0, T        and $FF] xor
  550.                Gost_Data[1, T shr  8 and $FF] xor
  551.                Gost_Data[2, T shr 16 and $FF] xor
  552.                Gost_Data[3, T shr 24        ];
  553.     Dec(PInteger(K), 2);
  554.   end;
  555.   N.N1 := B;
  556.   N.N2 := A;
  557. end;
  558.  
  559. procedure TGostCrypt.EncodeBuffer(Buff: Pointer; Size: Integer);
  560. var
  561.   D: pByte;
  562.   N: TGostN;
  563. begin
  564.   N.N1 := 0;
  565.   N.N2 := 0;
  566.   D := Buff;
  567.   while Size >= 8 do
  568.   begin
  569.     Encode(Gamma0);
  570.     Gamma(Gamma0);
  571.     Encode(pGostN(D)^);
  572.     pGostN(D).N1 := pGostN(D).N1 xor Gamma0.N1;
  573.     pGostN(D).N2 := pGostN(D).N2 xor Gamma0.N2;
  574.     Inc(D, 8);
  575.     Dec(Size, 8);
  576.   end;
  577.   if Size > 0 then
  578.   begin
  579.     Move(D^, N, Size);
  580.     Encode(Gamma0);
  581.     Gamma(Gamma0);
  582.     XorBuffer(@N, @Gamma0, 8);
  583.     Move(N, D^, Size);
  584.   end;
  585. end;
  586.  
  587. class procedure TGostCrypt.Gamma(var N: TGostN);
  588. const
  589.   C1 = $01010101;
  590.   C2 = $01010104;
  591.  
  592.   function GN1(N1: LongWord): LongWord;
  593.   asm
  594.     ADD EAX, C1
  595.   end;
  596.  
  597.   function GN2(N2: LongWord): LongWord;
  598.   asm
  599.     ADD EAX, C2
  600.     ADC EAX, 0
  601.   end;
  602.  
  603. begin
  604.   N.N1 := GN1(N.N1);
  605.   N.N2 := GN2(N.N2);
  606. end;
  607.  
  608. procedure TGostCrypt.InitKey;
  609. var
  610.   G0: TGostN;
  611. begin
  612.   Gamma0.N1 := CRC32(Password);
  613.   Gamma0.N2 := Gamma0.N1 xor TableCRC32[10];
  614.   G0 := Gamma0;
  615.   Gamma(G0);
  616.   Encode(G0);
  617.   Key[0] := G0.N1;
  618.   Key[1] := G0.N2;
  619.   Gamma(G0);
  620.   Encode(G0);
  621.   Key[2] := G0.N1;
  622.   Key[3] := G0.N2;
  623.   Gamma(G0);
  624.   Encode(G0);
  625.   Key[4] := G0.N1;
  626.   Key[5] := G0.N2;
  627.   Gamma(G0);
  628.   Encode(G0);
  629.   Key[6] := G0.N1;
  630.   Key[7] := G0.N2;
  631. end;
  632.  
  633. procedure TGostCrypt.XorBuffer(p1, p2: Pointer; Size: Integer);
  634. var
  635.   i: Integer;
  636. begin
  637.   for i := 0 to Size - 1 do
  638.     Byte(pChar(p1)[i]) := Byte(pChar(p1)[i]) xor Byte(pChar(p2)[i]);
  639. end;
  640.  
  641. end.
  642.