lazarus:用DCPCrypt对文本文件加密

需要uses两个单元:DCPrc4, DCPsha1。

加密过程:

procedure TForm1.ButtonEnCryptClick(Sender: TObject);
var
  Cipher: TDCP_rc4;
  Source, Target: TFileStream;
begin

  if not FileExists(EditSource.Text) then begin
  ShowMessage('出错了: 源文件不存在!');
  Exit;
  end;

  if FileExists(EditTarget.Text) then
    RenameFile(EditTarget.Text, EditTarget.Text+'.bak');

  try
    Source:= TFileStream.Create(EditSource.Text,fmOpenRead);
    Target:= TFileStream.Create(EditTarget.Text,fmCreate);
    Cipher:= TDCP_rc4.Create(Self);
    Cipher.InitStr(KeyStr,TDCP_sha1);
    Cipher.EncryptStream(Source,Target,Source.Size);
    Cipher.Burn;
    Cipher.Free;
    Target.Free;
    Source.Free;
    ShowMessage('搞定!');
  except
    ShowMessage('错误: 读写文件错误!');
  end;
end;   

解密过程:

procedure TForm1.ButtonDeCryptClick(Sender: TObject);
var
  Cipher: TDCP_rc4;
  Source, Target: TFileStream;
  begin

  if not FileExists(EditTarget.Text) then begin
    ShowMessage('出错了: 源文件不存在!');
    Exit;
  end;

   if FileExists(EditSource.Text) then
    RenameFile(EditSource.Text, EditSource.Text+'.bak');

   try
     Source:= TFileStream.Create(EditTarget.Text,fmOpenRead);
     Target:= TFileStream.Create(EditSource.Text,fmCreate);
     Cipher:= TDCP_rc4.Create(Self);
     Cipher.InitStr(KeyStr,TDCP_sha1);
     Cipher.DecryptStream(Source,Target,Source.Size);
     Cipher.Burn;
     Cipher.Free;
     Target.Free;
     Source.Free;
     ShowMessage('加密完成!');
   except
     ShowMessage('错误: 读写文件错误!');
   end;
  end; 

源文本文件:

加密后:

 

********** 强力推荐 加密、解密控件 源码 Demo**********http://www.cityinthesky.co.uk/英国小伙David Barton的Open Source,小伙将在 7th January 2006 结婚,祝福他们!喜欢这个控件的兄弟,也发个邮件祝福他们吧,以下是邮箱:davebarton[at]cityinthesky.co.ukcrypto[at]cityinthesky.co.uk同时,也感谢万重兄弟的盒子,提供一个大家交流的平台!----------Delphi Cryptography PackageDCPcrypt is an open source library of cryptographic components (encryption algorithms and hash algorithms) for Borland Delphi and Kylix (it also works under some versions of C++ Builder). I haven't been able to update it for quite a long time now but it still works quite happily with Delphi 4, 5, 6, 7 and 2005 and Kylix 1 and 2 (it might work under Kylix 3, but I don't have a copy to try it out on). The current version (v2 beta 3) is considered to be stable and for all intents and purposes is the final release before I revamp everything.Thanks to Manuel C. for making changes to the code to make this work under Delphi 2005!***注:经不完全测试,支持Delphi 2006 ***For those of you using PHP, here is a little demo showing how to get PHP and Delphi encryption to work together.The idea behind DCPcrypt is that it should be possible to "drop in" any algorithm implementation to replace another with minimum or no code changes. To aid in this goal all cryptographic components are descended from one of several base classes, TDCP_cipher for encryption algorithms and TDCP_hash for message digest algorithms.DCPcrypt v2 contains full implementations of the following algorithms:Encryption Algorithms Hash AlgorithmsBlowfishCast 128Cast 256DES, 3DESIce, Thin Ice, Ice2IDEAMarsMisty1RC2, RC4, RC5, RC6Rijndael (the new AES)SerpentTeaTwofish HavalMD4MD5RipeMD-128RipeMD-160SHA-1SHA-256, SHA-384, SHA-512TigerDCPcrypt is open source software (released under the MIT license) and as such there is no charge for inclusion in other software. However, I am currently a student and if you are making money from my software I would really appreciate a donation of some sort, whether financial or a license for the softwar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值