Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class procedure TCryptoProCSPVeryfier.DecryptFile(const SourceFile, DestFile, Password: string);
- const
- BUF_LEN = 100;
- KEY_LEN = 256;
- IV_LEN = 8;
- ReadIV = True;
- // PasswordFile = 'C:\Users\Ilya\Temp\Новая папка (2)\KEY_Exported_Test_2016.01.01.dat';
- var
- InStream: TFileStream;
- OutStream: TFileStream;
- Buffer: PByte;
- Key: TJwCryptKeyEx;
- BufferSize: Cardinal;
- CSP: TJwCryptProviderEx;
- Hash: TJwHashEx;
- IV: PByte;
- _PasswordFile:string;
- begin
- InStream := TFileStream.Create(SourceFile, fmOpenRead);
- OutStream := TFileStream.Create(DestFile, fmCreate);
- try
- CSP := CreateCSP('', []);
- try
- Hash := TJwHashEx.Create(ALG_GOST3411, CSP);
- try
- Hash.HashData(PChar(Password), Length(Password) * SizeOf(Char));
- // создание ключа сессии
- Key := TJwCryptKeyEx.Derive(CSP, CALG_G28147, [kfExportable], KEY_LEN, Hash);
- try
- IV := nil;
- GetMem(IV, IV_LEN);
- ZeroMemory(IV, IV_LEN);
- try
- // чтение вектора инициализации
- if ReadIV then
- InStream.Read(IV^, IV_LEN);
- Key.SetIV(IV);
- GetMem(Buffer, InStream.Size - InStream.Position);
- try
- // Установка вектора инициализации
- // без него первые 8 байт расшифруются неправильно
- BufferSize := InStream.Read(Buffer^, InStream.Size - InStream.Position);
- Key.Decrypt(nil, True, [], Buffer, BufferSize);
- OutStream.Write(Buffer^, BufferSize);
- finally
- FreeMem(Buffer);
- end;
- finally
- FreeMem(IV);
- end;
- finally
- Key.Free;
- end;
- finally
- Hash.Free;
- end;
- finally
- CSP.Free;
- end;
- finally
- InStream.Free;
- OutStream.Free;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement