返回列表 发帖

[转帖]利用URL编码进行加密 [源码]

刚刚拜读了Stamina的“URL编码及C语言实现”,觉得有必要写一个GUI的转换器,于是我就操起VB来 完成了这个作品.
其实原理很简单,但是我做GUI界面用了些时间,下面是主要的转换部分:
';';encode - 转成16进制符
For i = 1 To Len(strIN)
s = CStr(Hex(Asc(Mid(strIN, i, 1)))) ';';是不是太~~~快了~~
If Len(s) = 1 Then s = "0" & s ';';解出小于F的值就在前面加 0
strOUT = strOUT & "%" & s
Next i
txtOUT.Text = strOUT ';';这里就是输出了
___________________________________________________________
';';decode - 解码, 比较麻烦点
For i = 1 To Len(strIN) Step 3 ';'; Step 3 因为格式是 %XX 三位一个字符
s1 = UCase(Mid(strIN, i + 1, 1)) ';';得到第2位的16进制数字
s2 = UCase(Mid(strIN, i + 2, 1)) ';';得到第1位的16进制数字
Select Case s1 ';'; 进制转换
Case "A" To "F"
n = (10 + 5 - (Asc("F") - Asc(s1))) * 16
Case Else
n = Val(s1) * 16
End Select
Select Case s2
Case "A" To "F"
n = n + (10 + 5 - (Asc("F") - Asc(s2)))
Case Else
n = n + Val(s2)
End Select
s = ChrW $(n) ';'; 转为字符
strOUT = strOUT & s
Next i
txtOUT.Text = strOUT

程序下载: http://apower.uhome.net/download/encoder.zip
http://members.rogers.com/tdl/encoder.zip

[转帖]利用URL编码进行加密 [源码]

用delphi也可以实现
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, HTTPApp;
type
TForm1 = class(TForm)
  Button1: TButton;
  Memo1: TMemo;
  Memo2: TMemo;
  Button2: TButton;
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
private
  { Private declarations }
  function URLEncode(const msg : String) : String;
  function GetMemoText(memo:TMemo):string;
public
  { Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
ss: string;
begin
ss := GetMemoText(Memo1);
Memo1.Text :=ss;
Memo2.Clear;
for i := 1 to Length(ss) do
begin
  Memo2.Text := Memo2.Text + ';%'; + IntToHex(Ord(ss), 2);
end;
end;
function TForm1.URLEncode(const msg: String): String;
var
  I : Integer;
begin
  Result := ';';;
  for I := 1 to Length(msg) do begin
    if msg[I] = '; '; then
        Result := Result + ';+';
    else if msg[I] in [';a';..';z';, ';A';..';Z';, ';0';..';9';] then
        Result := Result + msg[I]
    else
        Result := Result + ';%'; + IntToHex(ord(msg[I]), 2);
  end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var ss:string;
begin
Memo2.Clear;
ss :=GetMemoText(Memo1);
Memo1.Text :=ss;
Memo2.Text :=HTTPDecode(trim(ss));
end;
function TForm1.GetMemoText(memo: TMemo): string;
var i:integer;
begin
for i:=0 to memo.Lines.Count-1 do
begin
  Result :=Result + trim(memo.Lines);
end;
end;
end.

TOP

返回列表 回复 发帖