从网上整理的一些delphi字符串加密解密方法
来源:互联网 发布:mac 身份不明 编辑:程序博客网 时间:2024/05/01 15:51
function Encode(Str: string): string;var //加密 TmpChr: AnsiChar; i, Len: integer;begin Result := Str; Len := Length(Result); TmpChr := Result[1]; for i := 1 to Len - 1 do Result[i] := Result[i + 1]; Result[Len] := TmpChr;end;
function Decode(Str: string): string;var //解密 TmpChr: AnsiChar; i, Len: integer;begin Result := Str; Len := Length(Result); TmpChr := Result[Len]; for i := Len downto 2 do Result[i] := Result[i - 1]; Result[1] := TmpChr;end;
{异或 加密解密}const // 首先定义一个常量数组 XorKey: array[0..7] of Byte = ($B2, $91, $AA, $55, $93, $6D, $84, $47); //字符串加密用//数组的值可以自己改
function Enc(Str: string): string; //字符加密函數 這是用的一個異或加密var i, j: Integer;begin Result := ''; j := 0; for i := 1 to Length(Str) do begin Result := Result + IntToHex(Byte(Str[i]) xor XorKey[j], 2); j := (j + 1) mod 8; end;end;
function Dec(Str: string): string; //字符解密函數var i, j: Integer;begin Result := ''; j := 0; for i := 1 to Length(Str) div 2 do begin Result := Result + Char(StrToInt('$' + Copy(Str, i * 2 - 1, 2)) xor XorKey[j]); j := (j + 1) mod 8; end;end;
function Crypt(s: string; Key: Word; const bEncrypt: boolean): string;const SeedA = 787; /// 常量,你可以修改 SeedB = 787; /// 常量,你可以修改var i: integer; ps, pr: ^byte;begin if bEncrypt then s := s + #0; SetLength(Result, Length(s)); ps := @s[1]; pr := @Result[1]; for i := 1 to length(s) do begin pr^ := ps^ xor (Key shr 8); if bEncrypt then Key := (pr^ + Key) * SeedA + SeedB else Key := (ps^ + Key) * SeedA + SeedB; pr := pointer(integer(pr) + 1); ps := pointer(integer(ps) + 1); end;end;///////////////////function EncrypStr(Src, Key: string): string; //字符串加密函数//对字符串加密(Src:源 Key:密匙)var KeyLen: Integer; KeyPos: Integer; offset: Integer; dest: string; SrcPos: Integer; SrcAsc: Integer; Range: Integer;begin KeyLen := Length(Key); if KeyLen = 0 then key := 'delphi'; KeyPos := 0; Range := 256; Randomize; offset := Random(Range); dest := format('%1.2x', [offset]); for SrcPos := 1 to Length(Src) do begin SrcAsc := (Ord(Src[SrcPos]) + offset) mod 255; if KeyPos < KeyLen then KeyPos := KeyPos + 1 else KeyPos := 1; SrcAsc := SrcAsc xor Ord(Key[KeyPos]); dest := dest + format('%1.2x', [SrcAsc]); offset := SrcAsc; end; Result := Dest;end;
function UncrypStr(Src, Key: string): string; //字符串解密函数//对字符串解密(Src:源 Key:密匙)var KeyLen: Integer; KeyPos: Integer; offset: Integer; dest: string; SrcPos: Integer; SrcAsc: Integer; TmpSrcAsc: Integer;begin KeyLen := Length(Key); if KeyLen = 0 then key := 'delphi'; KeyPos := 0; offset := StrToInt('$' + copy(src, 1, 2)); SrcPos := 3; repeat SrcAsc := StrToInt('$' + copy(src, SrcPos, 2)); if KeyPos < KeyLen then KeyPos := KeyPos + 1 else KeyPos := 1; TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]); if TmpSrcAsc <= offset then TmpSrcAsc := 255 + TmpSrcAsc - offset else TmpSrcAsc := TmpSrcAsc - offset; dest := dest + chr(TmpSrcAsc); offset := srcAsc; SrcPos := SrcPos + 2; until SrcPos >= Length(Src); Result := Dest;end;
//********************************************************// 加密解密 (利用异或运算) (方式2)//********************************************************
function encryptstr(const s: string; skey: string): string; //加密var i, j: integer; hexS, hexskey, midS, tmpstr: string; a, b, c: byte;begin hexS := myStrtoHex(s); hexskey := myStrtoHex(skey); midS := hexS; for i := 1 to (length(hexskey) div 2) do begin if i <> 1 then midS := tmpstr; tmpstr := ''; for j := 1 to (length(midS) div 2) do begin a := strtoint('$' + midS[2 * j - 1] + midS[2 * j]); b := strtoint('$' + hexskey[2 * i - 1] + hexskey[2 * i]); c := a xor b; tmpstr := tmpstr + myStrtoHex(chr(c)); end; end; result := tmpstr;end;
function decryptstr(const s: string; skey: string): string; //解密var i, j: integer; hexS, hexskey, midS, tmpstr: string; a, b, c: byte;begin hexS := s; //应该是该字符串 if length(hexS) mod 2 = 1 then begin showmessage('密文错误!'); exit; end; hexskey := myStrtoHex(skey); tmpstr := hexS; midS := hexS; for i := (length(hexskey) div 2) downto 1 do begin if i <> (length(hexskey) div 2) then midS := tmpstr; tmpstr := ''; for j := 1 to (length(midS) div 2) do begin a := strtoint('$' + midS[2 * j - 1] + midS[2 * j]); b := strtoint('$' + hexskey[2 * i - 1] + hexskey[2 * i]); c := a xor b; tmpstr := tmpstr + myStrtoHex(chr(c)); end; end; result := myHextoStr(tmpstr);end;
没有一个个去研究,调试。以后在时间在慢慢试。
- 从网上整理的一些delphi字符串加密解密方法
- Delphi字符串加密解密
- 简单的Delphi字符串加密解密函数
- 简单的Delphi字符串加密解密函数
- 加密解密的一些方法
- 从网上整理的一些电信词汇
- Delphi字符串加密解密函数
- Delphi字符串加密解密函数
- Delphi字符串加密解密函数
- .net加密 解密方法(以前网上找的)
- 一些常用的加密解密、编码方法
- 几种字符串加密解密的方法
- 从网上得知的一些delphi经典书籍
- 从网上得知的一些delphi经典书籍
- 整理加密解密部分方法
- Delphi中关于DES加密解密的方法
- 字符串的加密解密
- 【加密解密】对字符串的加密解密
- 如何使用SQLPLUS分析SQL语句(查询执行计划跟踪)
- POJ 2777 Count Color
- svn一些使用问题
- C语言写的trim()函数
- ExtJS中get、getDom、getCmp、getBody、getDoc使用 javascript
- 从网上整理的一些delphi字符串加密解密方法
- android中文字体
- 硅谷13大令人惊叹的女性
- 标记关键数字
- 枚举
- [MS-TDS]: Tabular Data Stream Protocol Specification
- 扎克伯格成功的六大因素
- 几个算法测试题及答案
- 程序猿装B指南