DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: fhc2004
今日帖子: 17
在线用户: 14
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 hongama (hongama) ★☆☆☆☆ -
盒子活跃会员
2004/3/26 11:13:31
标题:
高手请进!!!!!!!!!急。。。 浏览:1229
加入我的收藏
楼主: 现在在给数据库的连接字符串加密,这样记录数据库名,用户名和用户密码就不会泄露给普通用户了(因为我加密之后就会是直接打开文件出现乱码),可是我不知道TWordTriple是什么数据类型,怎么定义,才能调用function TextEncrypt(const s: string; Key: TWordTriple): string; 等函数,请高手指教,做过这样工程的也请发表一下这样的做法,或是其他的方法思路。谢谢!




加密/解密代码如下:
unit EZCrypt; 

{modeled by Ben Hochstrasser(bhoc@surfeu.ch) after some code snippet from borland} 

interface 

uses Windows, Classes; 

type 
  TWordTriple = Array[0..2] of Word; 

function FileEncrypt(InFile, OutFile: String; Key: TWordTriple): boolean; 
function FileDecrypt(InFile, OutFile: String; Key: TWordTriple): boolean; 
function TextEncrypt(const s: string; Key: TWordTriple): string; 
function TextDecrypt(const s: string; Key: TWordTriple): string; 
function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean; 
function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean; 

implementation 

function MemoryEncrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean; 
var 
  pIn, pOut: ^byte; 
  i : Cardinal; 
begin 
  if SrcSize = TargetSize then 
  begin 
    pIn := Src; 
    pOut := Target; 
    for i := 1 to SrcSize do 
    begin 
      pOut^ := pIn^ xor (Key[2] shr 8); 
      Key[2] := Byte(pIn^ + Key[2]) * Key[0] + Key[1]; 
      inc(pIn); 
      inc(pOut); 
    end; 
    Result := True; 
  end else 
    Result := False; 
end; 

function MemoryDecrypt(Src: Pointer; SrcSize: Cardinal; Target: Pointer; TargetSize: Cardinal; Key: TWordTriple): boolean; 
var 
  pIn, pOut: ^byte; 
  i : Cardinal; 
begin 
  if SrcSize = TargetSize then 
  begin 
    pIn := Src; 
    pOut := Target; 
    for i := 1 to SrcSize do 
    begin 
      pOut^ := pIn^ xor (Key[2] shr 8); 
      Key[2] := byte(pOut^ + Key[2]) * Key[0] + Key[1]; 
      inc(pIn); 
      inc(pOut); 
    end; 
    Result := True; 
  end else 
    Result := False; 
end; 

function TextCrypt(const s: string; Key: TWordTriple; Encrypt: Boolean): string; 
var 
  bOK: Boolean; 
begin 
  SetLength(Result, Length(s)); 
  if Encrypt then 
    bOK := MemoryEncrypt(PChar(s), Length(s), PChar(Result), Length(Result), Key) 
  else 
    bOK := MemoryDecrypt(PChar(s), Length(s), PChar(Result), Length(Result), Key); 
  if not bOK then Result := ''; 
end; 

function FileCrypt(InFile, OutFile: String; Key: TWordTriple; Encrypt: Boolean): boolean; 
var 
  MIn, MOut: TMemoryStream; 
begin 
  MIn := TMemoryStream.Create; 
  MOut := TMemoryStream.Create; 
  Try 
    MIn.LoadFromFile(InFile); 
    MOut.SetSize(MIn.Size); 
    if Encrypt then 
      Result := MemoryEncrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key) 
    else 
      Result := MemoryDecrypt(MIn.Memory, MIn.Size, MOut.Memory, MOut.Size, Key); 
    MOut.SaveToFile(OutFile); 
  finally 
    MOut.Free; 
    MIn.Free; 
  end; 
end; 

function TextEncrypt(const s: string; Key: TWordTriple): string; 
begin 
  Result := TextCrypt(s, Key, True); 
end; 

function TextDecrypt(const s: string; Key: TWordTriple): string; 
begin 
  Result := TextCrypt(s, Key, False); 
end; 

function FileEncrypt(InFile, OutFile: String; Key: TWordTriple): boolean; begin 
  Result := FileCrypt(InFile, OutFile, Key, True); 
end; 

function FileDecrypt(InFile, OutFile: String; Key: TWordTriple): boolean; begin 
  Result := FileCrypt(InFile, OutFile, Key, False); 
end; 

字符串加密, 解密

----------------------------------------------
-
作者:
男 hongama (hongama) ★☆☆☆☆ -
盒子活跃会员
2004/3/26 11:26:31
1楼: 高手快进啊,进来的同志帮我 顶一下,谢谢!
我顶...........
----------------------------------------------
-
作者:
男 merry_bip (merry_bip) ★☆☆☆☆ -
盒子活跃会员
2004/3/26 11:30:02
2楼: 这么简单的都不知道啊!!!
TWordTriple = Array[0..2] of Word; 他不是定义了吗!!

----------------------------------------------
merry_bip
作者:
男 hongama (hongama) ★☆☆☆☆ -
盒子活跃会员
2004/3/26 11:32:39
3楼: 哈哈,我怎么这都没看,我去试试,我靠,一语惊醒梦中人啊
----------------------------------------------
-
作者:
男 hongama (hongama) ★☆☆☆☆ -
盒子活跃会员
2004/3/26 11:33:56
4楼: 那又谁做过这样的程序的吗?给个经验之谈
----------------------------------------------
-
作者:
男 bios (阿贡) ★☆☆☆☆ -
盒子中级会员
2004/3/26 11:40:14
5楼: 用tmemorystream 只能提示到这里 自己动脑筋!
此帖子包含附件:
JPEG 图像
大小:101.4K
----------------------------------------------
按此在新窗口浏览图片
按此在新窗口浏览图片
作者:
男 hongama (hongama) ★☆☆☆☆ -
盒子活跃会员
2004/3/26 11:46:19
6楼: bios:不会吧,我考虑都很久了,要不,也不能连array都没看到啊,给个比较好的建议,行吗?
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行101.5625毫秒 RSS