Delphi进行数据加密,在数据库方面经常要使用到。从网上转载过来的,以后会经常会用到。
一、MD5加密算法
在C#/.Net里提供了MD5加密的类库。在Delphi中没有。只能自己建一个新的单位,将网上别人写的MD5加密函数拿来用。
View Code
调用方法:
var md5: TMD5Digest; //MD5Unit.pas passwordSource:string; passwordDestinate:string; begin passwordSource:='testStringForMD5'; MD5String(passwordSource, @md5); passwordDestinate:= LowerCase(MD5DigestToStr(md5)); ShowMessage(passwordDestinate); end;
调用方法二:
uses MD5Unit,IdHashMessageDigest,IdHash; procedure TForm1.btn1Click(Sender: TObject); var MyMD5: TIdHashMessageDigest5;//IdHashMessageDigest.pas Digest: T4x4LongWordRecord; //IdHash.pas passwordSource:string; passwordDestinate32:string; passwordDestinate16:string; begin passwordSource:='testStringForMD5'; MyMD5 := TIdHashMessageDigest5.Create; Digest := MyMD5.HashValue(passwordSource); passwordDestinate32:=LowerCase(MyMD5.AsHex(Digest)); //32个字符长度的MD5签名结果 passwordDestinate16:=Copy(passwordDestinate32, 9, 16);//16个字符长度的MD5签名结果 ShowMessage('32: ' +passwordDestinate32+#13#10+'16: ' + passwordDestinate16); MyMD5.Free; end; end.
二、DES加密算法
DES的加密、解密封库单元
View Code
调用方法,加密:
var PlaintextStr:string; begin PlaintextStr:='加密测试!'; PublicCiphertextStr:=EncryStrHex(PlaintextStr, '11111111');//StandardDES.pas showmessage(PublicCiphertextStr); end;
调用方法,解密:
var PlaintextStr:string; begin PlaintextStr:=DecryStrHex(PublicCiphertextStr, '11111111');//StandardDES.pas showmessage(PlaintextStr); end;
转截自:http://www.cnblogs.com/edisonfeng/archive/2011/07/22/2054520.html