Digital signature Add Source

Source: Internet
Author: User
Tags trim

Unit Umain;

Interface

Uses

Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,

Dialogs, Stdctrls;

Type

TForm1 = Class (Tform)

Button1:tbutton;

Selfile:topendialog;

Efile1:tedit;

Label1:tlabel;

Label2:tlabel;

Efile2:tedit;

Button2:tbutton;

Button3:tbutton;

Button4:tbutton;

Cbbakfile:tcheckbox;

Label3:tlabel;

Procedure Button1Click (Sender:tobject);

Procedure Button2click (Sender:tobject);

Procedure Button3click (Sender:tobject);

Procedure Button4click (Sender:tobject);

Procedure Formclose (Sender:tobject; var action:tcloseaction);

Private

{Private declarations}

Public

{Public declarations}

End

Var

Form1:tform1;

Implementation

{$R *.DFM}

Procedure Showmessbox (Pvcaptiong, pvtext:string);

Begin

MessageBox (Form1.handle, Pansichar (Pvtext), Pansichar (Pvcaptiong), 0);

End

function Readhexdz (fvfilename:string; fvhexdz:integer): Integer; Reads the specified offset address hexadecimal data

Var

Vbuffer:array of Byte; Call function back error without length specified

Vbuffer:array [0..3] of byte; Specify length

Vint:integer;

Vfs:tfilestream;

vstr:string;

Begin

Result: =-1;

Vstr:= ';

Try

vfs:= tfilestream.create (Fvfilename, Fmopenread); Open in read mode

Vfs.position:= Fvhexdz; Set Start position

Vfs.readbuffer (Vbuffer, SizeOf (Vbuffer)); Read data to Buffer

For vint:=0 to 3 do

Vstr:=inttohex (Vbuffer[vint], 2) + vstr; Get 16 in.

result:= strtoint (' $ ' +vstr);

Except

Result: =-1

End;

Vfs.free;

End

Procedure Tform1.button1click (Sender:tobject);

Begin

Self.close;

End

Procedure Tform1.button2click (Sender:tobject);

Begin

Selfile.title: = ' Please select a file containing digital signature ';

If not Selfile.execute then Exit;

Efile1.text: = Selfile.filename;

End

Procedure Tform1.button3click (Sender:tobject);

Begin

Selfile.title: = ' Please select a file to add a digital signature ';

If not Selfile.execute then Exit;

Efile2.text: = Selfile.filename;

End

Procedure Tform1.button4click (Sender:tobject);

Var

VFile1, vfile2:string;

Vbuf1,vbuf2:array [0..3] of Byte;

Vfs:tfilestream;

Vbufattr:array [0..100000] of Pansichar;

Vfile2szqmdz,

Vfile1szqmdz,//Specify the address of the digital signature

Vfile1szqmsizedz,//Specify digital signature size

Vszqmdz,//digital signature Address

vbufsize:integer;//Digital Signature Size

Vstr, Vnewstr, vnewstr2:string;

Vint:integer;

Begin

vfile1:= Trim (Efile1.text);

VFile2: = Trim (Efile2.text);

If not fileexists (vFile1) or not FileExists (vFile2) Then

Begin

Showmessbox (' message ', ' Can't find the file! ');

Exit;

End

If Cbbakfile.checked Then

CopyFile (Pansichar (VFile2), Pansichar (Extractfilename (vFile2) + '. Bak '), False);

vfile1szqmdz:= Readhexdz (VFile1, $3c) + $98; Digital Signature Address

Vfile1szqmsizedz: = Vfile1szqmdz +$4; Digital Signature Size Address

vszqmdz:= Readhexdz (VFile1, Vfile1szqmdz); Digital signature Start Position

Vbufsize: = Readhexdz (VFile1, Vfile1szqmsizedz);

Showmessbox (IntToStr (Vfile1szqmsizedz), IntToStr (vbufsize));

Exit

Try

VFS: = Tfilestream.create (VFile1, Fmopenread);

Try

Vfs.position:= Vfile1szqmdz;

Vfs.readbuffer (VBUF1, 4); The buffer where the record digital signature is located

Vfs.position:= Vfile1szqmsizedz;

Vfs.readbuffer (VBUF2, 4); Get a buffer that records the size of a digital signature

Vfs.position:= Vszqmdz;

Vfs.readbuffer (vbufattr, vbufsize); Read digital signature data to Vbufattr

Finally

Vfs.free;

End

Vfile2szqmdz: = Readhexdz (VFile2, $3c) + $98;

VFS: = Tfilestream.create (VFile2, fmopenreadwrite);

Try

Vfs.position:= Vfile2szqmdz;

vstr:= Inttohex (vfs.size, 8);

vnewstr:= Copy (Vstr, 7, 2);

vnewstr:= vnewstr + Copy (VSTR, 5, 2);

vnewstr:= vnewstr + Copy (VSTR, 3, 2);

vnewstr:= vnewstr + Copy (vstr, 1, 2);

Vnewstr2:= ';

For Vint:=1 to (length (vnewstr) div. 2) do

Vnewstr2:=vnewstr2+char (Strtoint (' $ ' +copy (Vnewstr, (vInt-1) *2+1,2)));

Vfs.writebuffer (pointer (vNewStr2) ^, 4); Write data specify the location of the digital signature

Vfs.position: = Vfile2szqmdz + $;

Vfs.writebuffer (Vbuf2, SizeOf (VBUF2)); Write data specify digital signature size

Vfs.position:= vfs.size;

Vfs.writebuffer (vbufattr, vbufsize);

Finally

Vfs.free;

End

Showmessbox (' message ', ' Add digital signature success ');

Except

Showmessbox (' Bad news ', ' Add digital signature error ');

End

End

Procedure Tform1.formclose (Sender:tobject; var action:tcloseaction);

Begin

Action:= Cafree;

End

End.

        Note : For more wonderful tutorials please focus on the Triple Web Design Tutorials section,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.