Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. class procedure TCryptoProCSPVeryfier.DecryptFile(const SourceFile, DestFile, Password: string);
  2. const
  3. BUF_LEN = 100;
  4. KEY_LEN = 256;
  5. IV_LEN = 8;
  6. ReadIV = True;
  7. // PasswordFile = 'C:\Users\Ilya\Temp\Новая папка (2)\KEY_Exported_Test_2016.01.01.dat';
  8. var
  9. InStream: TFileStream;
  10. OutStream: TFileStream;
  11.  
  12. Buffer: PByte;
  13.  
  14. Key: TJwCryptKeyEx;
  15. BufferSize: Cardinal;
  16. CSP: TJwCryptProviderEx;
  17. Hash: TJwHashEx;
  18. IV: PByte;
  19. _PasswordFile:string;
  20. begin
  21.  
  22. InStream := TFileStream.Create(SourceFile, fmOpenRead);
  23. OutStream := TFileStream.Create(DestFile, fmCreate);
  24. try
  25.  
  26. CSP := CreateCSP('', []);
  27. try
  28. Hash := TJwHashEx.Create(ALG_GOST3411, CSP);
  29. try
  30. Hash.HashData(PChar(Password), Length(Password) * SizeOf(Char));
  31.  
  32. // создание ключа сессии
  33. Key := TJwCryptKeyEx.Derive(CSP, CALG_G28147, [kfExportable], KEY_LEN, Hash);
  34. try
  35. IV := nil;
  36. GetMem(IV, IV_LEN);
  37. ZeroMemory(IV, IV_LEN);
  38. try
  39. // чтение вектора инициализации
  40. if ReadIV then
  41. InStream.Read(IV^, IV_LEN);
  42. Key.SetIV(IV);
  43. GetMem(Buffer, InStream.Size - InStream.Position);
  44. try
  45.  
  46. // Установка вектора инициализации
  47. // без него первые 8 байт расшифруются неправильно
  48.  
  49. BufferSize := InStream.Read(Buffer^, InStream.Size - InStream.Position);
  50.  
  51. Key.Decrypt(nil, True, [], Buffer, BufferSize);
  52. OutStream.Write(Buffer^, BufferSize);
  53. finally
  54. FreeMem(Buffer);
  55. end;
  56. finally
  57. FreeMem(IV);
  58. end;
  59. finally
  60. Key.Free;
  61. end;
  62. finally
  63. Hash.Free;
  64. end;
  65. finally
  66. CSP.Free;
  67. end;
  68. finally
  69. InStream.Free;
  70. OutStream.Free;
  71. end;
  72. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement