字符串加密

yuanrong 2003-08-01 04:12:04
比方,我要给一个字符串做加密处理,代码如何写?不用太复杂,只要处理成不是明文即可。
...全文
157 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ailibuli 2004-02-19
  • 打赏
  • 举报
回复
全部験収、
各有千秋、
多謝多謝!
老本 2004-02-17
  • 打赏
  • 举报
回复
输入的密钥,不保存在计算机上,因此,解密的数组为动态产生的。
老本 2004-02-17
  • 打赏
  • 举报
回复
还有一种办法,上面没有提到。
枚举所有可用于字符串中的字符,作为一个数组1;
接受输入并根据输入对数据进行重新排序,得到数组2;
function Encrypt(ch: char):char;
begin
result := 数组2[ch];
end;

解密的原理同上。
老本 2004-02-17
  • 打赏
  • 举报
回复
非得保存成字符串,可以将每个字符分成两个,均 +$30。
用分组异或是一个最简单的办法。
采用一次一密,安全性最好!
ly_liuyang 2003-08-14
  • 打赏
  • 举报
回复
网上有例子的
===
使用Base64编码可以保证可靠
HjmaAsC 2003-08-14
  • 打赏
  • 举报
回复
加密后的串不能用字符类型来保存的,要用varbit类型的数据来保存!!
wanwangzhiwang 2003-08-13
  • 打赏
  • 举报
回复
你们这样写的加密字符串如果存到数据库,可能因为某些字符的原因重新取出来的时候变成别的了,或者少了几个字符。

遇到这种问题你们怎么解决的?
goldwolf 2003-08-13
  • 打赏
  • 举报
回复
overtime 2003-08-13
  • 打赏
  • 举报
回复
function XorEncode(const Key, Source: string): string;
var
I: Integer;
C: Byte;
begin
Result := '';
for I := 1 to Length(Source) do begin
if Length(Key) > 0 then
C := Byte(Key[1 + ((I - 1) mod Length(Key))]) xor Byte(Source[I])
else
C := Byte(Source[I]);
Result := Result + AnsiLowerCase(IntToHex(C, 2));
end;
end;

function XorDecode(const Key, Source: string): string;
var
I: Integer;
C: Char;
begin
Result := '';
for I := 0 to Length(Source) div 2 - 1 do begin
C := Chr(StrToIntDef('$' + Copy(Source, (I * 2) + 1, 2), Ord(' ')));
if Length(Key) > 0 then
C := Chr(Byte(Key[1 + (I mod Length(Key))]) xor Byte(C));
Result := Result + C;
end;
end;
佩佩zpm 2003-08-12
  • 打赏
  • 举报
回复
//参数说明:
//s:待加密的字符串;Key:加密码;bEncrypt:true时加密,false时解密
function Crypt(const s: string; Key: Word; const bEncrypt: boolean = True):
//函数返回加密的字符串(bEncrypt=true)或者解密的字符串(bEncrypt=false)

//下面是一个例子,
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

const
SeedA = 5678; /// 常量 ,你可以修改
SeedB = 5432; /// 常量 ,你可以修改

var
Form1: TForm1;

implementation

{$R *.DFM}

function Crypt(const s: string; Key: Word; const bEncrypt: boolean = True): string;
var
i: integer;
ps, pr: ^byte;
begin
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;

//button1对Edit1中的字符串加密,加密后的内容显示为Lable1的caption
//这里的密匙是111,如果别人不知道这个密匙,也是无法解密的
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.caption := Crypt(Edit1.Text, 111, true);
end;

//button2对加密后的Label1.caption进行解密,解密后还是显示在Label1.caption
//注意用密匙111解密
procedure TForm1.Button2Click(Sender: TObject);
begin
label1.caption := Crypt(label1.caption, 111, false);
end;

end.
coolfilm 2003-08-12
  • 打赏
  • 举报
回复
function encode(s:string):string;
{///////////简单的字符串加密涵数////////////////////////////}
{///////////输入参数:字符串;输出差数:字符串/////////////////}
{///////////2003-7-15 ///////////////}
var
n,i:integer;
str:string;
begin
n:=length(s);
str:='';
for i:=1 to n do
begin
str:=str+char(ord(s[i])-1);
end;
encode:=str;
end;

function decode(s:string):string;
{///////////简单的字符串解密涵数////////////////////////////}
{///////////输入参数:字符串;输出差数:字符串/////////////////}
{///////////2003-7-15 ///////////////}
var
n,i:integer;
str:string;
begin
n:=length(s);
str:='';
for i:=1 to n do
begin
str:=str+char(ord(s[i])+1);
end;
decode:=str;
end;
yuanrong 2003-08-11
  • 打赏
  • 举报
回复
能不能解释一下?谢谢。
佩佩zpm 2003-08-01
  • 打赏
  • 举报
回复
一个字符串加密函数
function Crypt(const s: string; Key: Word; const bEncrypt: boolean = True): string; overload;
var
i: integer;
ps, pr : ^byte;
begin
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;

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧