Archive for September, 2007

Mengcopy baris tertentu dari MS Word memakai Delphi

September 30, 2007

Suatu saat ketika menggunakan Delphi, kita ingin mengcopy baris tertentu dari dokumen MS Word ke dokumen yang lain, berikut adalah kode untuk proses tersebut, dimisalkan area yang akan dicopy adalah baris 5 sampai baris 14 :

uses ClipBrd, ComObj, Word2000;{$R *.dfm}
procedure TForm1.prosesCopy;
var FileName1,FileName2 : OleVariant;
Range : Variant;
WordApp: Variant;
NumPars,i : integer;
begin
Clipboard.Clear;
FileName1:=ExtractFilePath(Application.ExeName)+'First.doc';
FileName2:=ExtractFilePath(Application.ExeName)+'Second.doc';
try
WordApp := CreateOleObject('Word.Application');
WordApp.Visible := False;
WordApp.Documents.Open(
FileName1,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam);
Range := WordApp.Documents.Item(1).Range(
WordApp.Documents.Item(1).Paragraphs.Item(5).Range.Start,
WordApp.Documents.Item(1).Paragraphs.Item(14).Range.End);
Range.Copy; WordApp.Documents.Open(
FileName2,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam);
NumPars := WordApp.Documents.Item(1).Paragraphs.Count;
WordApp.Documents.Add;
Range := WordApp.Documents.Item(1).Range(
WordApp.Documents.Item(1).Paragraphs.Item(1).Range.Start,
WordApp.Documents.Item(1).Paragraphs.Item(1).Range.End);
for i := 1 to 2 do WordApp.Documents.Item(1).Paragraphs.Add;
Range.Paste;
WordApp.Documents.Item(1).SaveAs(
ExtractFilePath(Application.ExeName)+'Third.doc');
except
end;
Clipboard.Clear;
WordApp.Documents.Item(1).Close(wdDoNotSaveChanges);
WordApp.Quit;
ShowMessage('Proses Selesai');
end;

Komponen Kriptografi yang free untuk Delphi

September 30, 2007

Beberapa waktu lalu ketika memerlukan proses enkripsi untuk file MS Access di aplikasi yang sedang dibuat, sebagai alternatif komponen indy yang sudah ada di Delphi, saya coba cari-cari komponen lain yang free dan simple untuk dipakai. Akhirnya ketemu ‘DCPcrypt Cryptographic Component Library v2 Beta 2’ di http://www.cityinthesky.co.uk/ . Algorithma chipers dan hash yang ada di komponen ini yaitu :
Chipers : Blowfish,Cast-128,Cast-256,DES,3DES,Ice,Thin Ice,Ice 2,IDEA,MARS,Misty1,RC2,RC4,RC5,RC6,Rijndael,Serpent,TEA dan Twofish
Hash : Haval,MD4,MD5,RipeMD-128,RipeMD-160,SHA-1,SHA-256,SHA-384,SHA-512 dan Tiger

Berikut contoh untuk mengenkripsi file dengan MD5 :

procedure TForm1.Button3Click(Sender: TObject);
var KeyStr: string;
Source, Dest: TFileStream;
begin
KeyStr:= 'znckue9ew';
try
Source:=TFileStream.Create('D:\LMandDB.mdb',fmOpenRead);
Dest:=TFileStream.Create('D:\dump',fmCreate);
DCP_rc51.InitStr(KeyStr,TDCP_md5);
DCP_rc51.EncryptStream(Source,Dest,Source.Size);
DCP_rc51.Burn;
Dest.Free;
Source.Free;
MessageDlg('File encrypted',mtInformation,[mbOK],0);
except
MessageDlg('File IO error',mtError,[mbOK],0);
end;
end;

Dekripsi file :


procedure TForm1.Button4Click(Sender: TObject);
var KeyStr: string;
Source, Dest: TFileStream;
begin
KeyStr:='znckue9ew';
try
Source:= TFileStream.Create('D:\dump',fmOpenRead);
Dest:= TFileStream.Create('D:\LMandDB2.mdb',fmCreate);
DCP_rc51.InitStr(KeyStr,TDCP_md5);
DCP_rc51.DecryptStream(Source,Dest,Source.Size);
DCP_rc51.Burn;
Dest.Free;
Source.Free;
MessageDlg('File decrypted',mtInformation,[mbOK],0);
except
MessageDlg('File IO error',mtError,[mbOK],0);
end;
end;