对邮件进行编码最初的原因是因为 Internet 上的很多网关不能正确传输8 bit 内码的字符,比如汉字等。编码的原理就是把 8 bit 的内容转换成 7 bit 的形式以能正确传输,在接收方收到之后,再将其还原成 8 bit 的内容。Subject: =?gb2312?B?xOO6w6Oh?= 这里是邮件的主题,可是因为编码了,我们看不出是什么内容,其原来的文本是:“你好!”请问如何用程序实行解码的功能呢??????????
标题:邮件解码之一 说明:Quoted Printable 设计:Zswang 日期:2002-02-19 支持:[email protected] //*)///////Begin Source function QuotedPrintableEncode(mSource: string): string; var I, J: Integer; begin Result := ''; J := 0; for I := 1 to Length(mSource) do begin if mSource[I] in [#32..#127, #13, #10] - ['='] then begin Result := Result + mSource[I]; Inc(J); end else begin Result := Result + '=' + IntToHex(Ord(mSource[I]), 2); Inc(J, 3); end; if mSource[I] in [#13, #10] then J := 0; if J >= 70 then begin Result := Result + #13#10; J := 0; end; end; end; { QuotedPrintableEncode }function QuotedPrintableDecode(mCode: string): string; var I, J, L: Integer; begin Result := ''; J := 0; mCode := AdjustLineBreaks(mCode); L := Length(mCode); I := 1; while I <= L do begin if mCode[I] = '=' then begin Result := Result + Chr(StrToIntDef('$' + Copy(mCode, I + 1, 2), 0)); Inc(J, 3); Inc(I, 3); end else if mCode[I] in [#13, #10] then begin if J < 70 then Result := Result + mCode[I]; if mCode[I] = #10 then J := 0; Inc(I); end else begin Result := Result + mCode[I]; Inc(J); Inc(I); end; end; end; { QuotedPrintableDecode } ///////End Source///////Begin Demo procedure TForm1.Button1Click(Sender: TObject); begin Memo2.Text := QuotedPrintableEncode(Memo1.Text); end; procedure TForm1.Button2Click(Sender: TObject); begin Memo1.Text := QuotedPrintableDecode(Memo2.Text); end; ///////End Demo
标题:邮件解码之一 说明:Quoted Printable 设计:Zswang 日期:2002-02-19 支持:[email protected] //*)///////Begin Source function QuotedPrintableEncode(mSource: string): string; var I, J: Integer; begin Result := ''; J := 0; for I := 1 to Length(mSource) do begin if mSource[I] in [#32..#127, #13, #10] - ['='] then begin Result := Result + mSource[I]; Inc(J); end else begin Result := Result + '=' + IntToHex(Ord(mSource[I]), 2); Inc(J, 3); end; if mSource[I] in [#13, #10] then J := 0; if J >= 70 then begin Result := Result + #13#10; J := 0; end; end; end; { QuotedPrintableEncode }function QuotedPrintableDecode(mCode: string): string; var I, J, L: Integer; begin Result := ''; J := 0; mCode := AdjustLineBreaks(mCode); L := Length(mCode); I := 1; while I <= L do begin if mCode[I] = '=' then begin Result := Result + Chr(StrToIntDef('$' + Copy(mCode, I + 1, 2), 0)); Inc(J, 3); Inc(I, 3); end else if mCode[I] in [#13, #10] then begin if J < 70 then Result := Result + mCode[I]; if mCode[I] = #10 then J := 0; Inc(I); end else begin Result := Result + mCode[I]; Inc(J); Inc(I); end; end; end; { QuotedPrintableDecode } ///////End Source///////Begin Demo procedure TForm1.Button1Click(Sender: TObject); begin Memo2.Text := QuotedPrintableEncode(Memo1.Text); end; procedure TForm1.Button2Click(Sender: TObject); begin Memo1.Text := QuotedPrintableDecode(Memo2.Text); end; ///////End Demo
标题:邮件解码之二 说明:Base64 设计:Zswang 日期:2002-02-21 支持:[email protected]///////Begin Source const cBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';function Base64Encode(mSource: string; mAddLine: Boolean = True): string; var I, J: Integer; S: string; begin Result := ''; J := 0; for I := 0 to Length(mSource) div 3 - 1 do begin S := Copy(mSource, I * 3 + 1, 3); Result := Result + cBase64[Ord(S[1]) shr 2 + 1]; Result := Result + cBase64[((Ord(S[1]) and $03) shl 4) + (Ord(S[2]) shr 4) + 1]; Result := Result + cBase64[((Ord(S[2]) and $0F) shl 2) + (Ord(S[3]) shr 6) + 1]; Result := Result + cBase64[Ord(S[3]) and $3F + 1]; if mAddLine then begin Inc(J, 4); if J >= 76 then begin Result := Result + #13#10; J := 0; end; end; end; I := Length(mSource) div 3; S := Copy(mSource, I * 3 + 1, 3); case Length(S) of 1: begin Result := Result + cBase64[Ord(S[1]) shr 2 + 1]; Result := Result + cBase64[(Ord(S[1]) and $03) shl 4 + 1]; Result := Result + cBase64[65]; Result := Result + cBase64[65]; end; 2: begin Result := Result + cBase64[Ord(S[1]) shr 2 + 1]; Result := Result + cBase64[((Ord(S[1]) and $03) shl 4) + (Ord(S[2]) shr 4) + 1]; Result := Result + cBase64[(Ord(S[2]) and $0F) shl 2 + 1]; Result := Result + cBase64[65]; end; end; end; { Base64Encode }function Base64Decode(mCode: string): string; var I, L: Integer; S: string; begin Result := ''; L := Length(mCode); I := 1; while I <= L do begin if Pos(mCode[I], cBase64) > 0 then begin S := Copy(mCode, I, 4); if (Length(S) = 4) then begin Result := Result + Chr((Pos(S[1], cBase64) - 1) shl 2 + (Pos(S[2], cBase64) - 1) shr 4); if S[3] <> cBase64[65] then begin Result := Result + Chr(((Pos(S[2], cBase64) - 1) and $0F) shl 4 + (Pos(S[3], cBase64) - 1) shr 2); if S[4] <> cBase64[65] then Result := Result + Chr(((Pos(S[3], cBase64) - 1) and $03) shl 6 + (Pos(S[4], cBase64) - 1)); end; end; Inc(I, 4); end else Inc(I); end; end; { Base64Decode } ///////End Source///////Begin Demo procedure TForm1.Button1Click(Sender: TObject); begin Memo2.Text := Base64Encode(Memo1.Text); end;procedure TForm1.Button2Click(Sender: TObject); begin Memo1.Text := Base64Decode(Memo2.Text); end;
说明:Quoted Printable
设计:Zswang
日期:2002-02-19
支持:[email protected]
//*)///////Begin Source
function QuotedPrintableEncode(mSource: string): string;
var
I, J: Integer;
begin
Result := '';
J := 0;
for I := 1 to Length(mSource) do begin
if mSource[I] in [#32..#127, #13, #10] - ['='] then begin
Result := Result + mSource[I];
Inc(J);
end else begin
Result := Result + '=' + IntToHex(Ord(mSource[I]), 2);
Inc(J, 3);
end;
if mSource[I] in [#13, #10] then J := 0;
if J >= 70 then begin
Result := Result + #13#10;
J := 0;
end;
end;
end; { QuotedPrintableEncode }function QuotedPrintableDecode(mCode: string): string;
var
I, J, L: Integer;
begin
Result := '';
J := 0;
mCode := AdjustLineBreaks(mCode);
L := Length(mCode);
I := 1;
while I <= L do begin
if mCode[I] = '=' then begin
Result := Result + Chr(StrToIntDef('$' + Copy(mCode, I + 1, 2), 0));
Inc(J, 3);
Inc(I, 3);
end else if mCode[I] in [#13, #10] then begin
if J < 70 then Result := Result + mCode[I];
if mCode[I] = #10 then J := 0;
Inc(I);
end else begin
Result := Result + mCode[I];
Inc(J);
Inc(I);
end;
end;
end; { QuotedPrintableDecode }
///////End Source///////Begin Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo2.Text := QuotedPrintableEncode(Memo1.Text);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := QuotedPrintableDecode(Memo2.Text);
end;
///////End Demo
说明:Quoted Printable
设计:Zswang
日期:2002-02-19
支持:[email protected]
//*)///////Begin Source
function QuotedPrintableEncode(mSource: string): string;
var
I, J: Integer;
begin
Result := '';
J := 0;
for I := 1 to Length(mSource) do begin
if mSource[I] in [#32..#127, #13, #10] - ['='] then begin
Result := Result + mSource[I];
Inc(J);
end else begin
Result := Result + '=' + IntToHex(Ord(mSource[I]), 2);
Inc(J, 3);
end;
if mSource[I] in [#13, #10] then J := 0;
if J >= 70 then begin
Result := Result + #13#10;
J := 0;
end;
end;
end; { QuotedPrintableEncode }function QuotedPrintableDecode(mCode: string): string;
var
I, J, L: Integer;
begin
Result := '';
J := 0;
mCode := AdjustLineBreaks(mCode);
L := Length(mCode);
I := 1;
while I <= L do begin
if mCode[I] = '=' then begin
Result := Result + Chr(StrToIntDef('$' + Copy(mCode, I + 1, 2), 0));
Inc(J, 3);
Inc(I, 3);
end else if mCode[I] in [#13, #10] then begin
if J < 70 then Result := Result + mCode[I];
if mCode[I] = #10 then J := 0;
Inc(I);
end else begin
Result := Result + mCode[I];
Inc(J);
Inc(I);
end;
end;
end; { QuotedPrintableDecode }
///////End Source///////Begin Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo2.Text := QuotedPrintableEncode(Memo1.Text);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := QuotedPrintableDecode(Memo2.Text);
end;
///////End Demo
我想知道的是Base 64 的解法.
?gb2312?B?xOO6w6Oh?
说明:Base64
设计:Zswang
日期:2002-02-21
支持:[email protected]///////Begin Source
const
cBase64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';function Base64Encode(mSource: string; mAddLine: Boolean = True): string;
var
I, J: Integer;
S: string;
begin
Result := '';
J := 0;
for I := 0 to Length(mSource) div 3 - 1 do begin
S := Copy(mSource, I * 3 + 1, 3);
Result := Result + cBase64[Ord(S[1]) shr 2 + 1];
Result := Result + cBase64[((Ord(S[1]) and $03) shl 4) + (Ord(S[2]) shr 4) + 1];
Result := Result + cBase64[((Ord(S[2]) and $0F) shl 2) + (Ord(S[3]) shr 6) + 1];
Result := Result + cBase64[Ord(S[3]) and $3F + 1];
if mAddLine then begin
Inc(J, 4);
if J >= 76 then begin
Result := Result + #13#10;
J := 0;
end;
end;
end;
I := Length(mSource) div 3;
S := Copy(mSource, I * 3 + 1, 3);
case Length(S) of
1: begin
Result := Result + cBase64[Ord(S[1]) shr 2 + 1];
Result := Result + cBase64[(Ord(S[1]) and $03) shl 4 + 1];
Result := Result + cBase64[65];
Result := Result + cBase64[65];
end;
2: begin
Result := Result + cBase64[Ord(S[1]) shr 2 + 1];
Result := Result + cBase64[((Ord(S[1]) and $03) shl 4) + (Ord(S[2]) shr 4) + 1];
Result := Result + cBase64[(Ord(S[2]) and $0F) shl 2 + 1];
Result := Result + cBase64[65];
end;
end;
end; { Base64Encode }function Base64Decode(mCode: string): string;
var
I, L: Integer;
S: string;
begin
Result := '';
L := Length(mCode);
I := 1;
while I <= L do begin
if Pos(mCode[I], cBase64) > 0 then begin
S := Copy(mCode, I, 4);
if (Length(S) = 4) then begin
Result := Result + Chr((Pos(S[1], cBase64) - 1) shl 2 +
(Pos(S[2], cBase64) - 1) shr 4);
if S[3] <> cBase64[65] then begin
Result := Result + Chr(((Pos(S[2], cBase64) - 1) and $0F) shl 4 +
(Pos(S[3], cBase64) - 1) shr 2);
if S[4] <> cBase64[65] then
Result := Result + Chr(((Pos(S[3], cBase64) - 1) and $03) shl 6 +
(Pos(S[4], cBase64) - 1));
end;
end;
Inc(I, 4);
end else Inc(I);
end;
end; { Base64Decode }
///////End Source///////Begin Demo
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo2.Text := Base64Encode(Memo1.Text);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := Base64Decode(Memo2.Text);
end;