📄 base64.pas
字号:
Unit Base64;
Interface
Uses SysUtils, Classes;
Function Base64EnCodeStr(Const S: String): String; stdcall;
Function Base64DeCodeStr(Const S: String): String; stdcall;
Function IsBase64(Const S: String): boolean;
Implementation
Const
Base64Table: String = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
Function Base64EnStr(Const S, Base64String: String; IsCustom: boolean): String;
Var
i, c1, c2, c3: Integer;
m, n: Integer;
Begin
Result := '';
If S = '' Then Exit;
m := 1;
n := 0;
For i := 1 To (Length(S) Div 3) Do
Begin
c1 := Ord(S[m]);
c2 := Ord(S[m + 1]);
c3 := Ord(S[m + 2]);
m := m + 3;
Result := Result + Base64String[(c1 Shr 2) And $3F + 1];
Result := Result + Base64String[((c1 Shl 4) And $30) Or ((c2 Shr 4) And $0F) + 1];
Result := Result + Base64String[((c2 Shl 2) And $3C) Or ((c3 Shr 6) And $03) + 1];
Result := Result + Base64String[c3 And $3F + 1];
n := n + 4;
If (n = 76) Then n := 0;
{Begin
Result := Result + #13#10;
End;}
End;
If (Length(S) Mod 3) = 1 Then
Begin
c1 := Ord(S[m]);
Result := Result + Base64String[(c1 Shr 2) And $3F + 1];
Result := Result + Base64String[(c1 Shl 4) And $30 + 1];
If Not IsCustom Then
Begin
Result := Result + '=';
Result := Result + '=';
End;
End;
If (Length(S) Mod 3) = 2 Then
Begin
c1 := Ord(S[m]);
c2 := Ord(S[m + 1]);
Result := Result + Base64String[(c1 Shr 2) And $3F + 1];
Result := Result + Base64String[((c1 Shl 4) And $30) Or ((c2 Shr 4) And $0F) + 1];
Result := Result + Base64String[(c2 Shl 2) And $3C + 1];
If Not IsCustom Then Result := Result + '=';
End;
End;
Function Base64DeStr(Const S, Base64String: String; IsCustom: boolean): String;
Var //解密
i, m, n: Integer;
c1, c2, c3, c4: Integer;
Begin
Result := '';
If S = '' Then Exit;
n := 1;
m := Length(S);
If Not IsCustom Then
Begin
If S[m] = '=' Then m := m - 1;
If S[m] = '=' Then m := m - 1;
End;
For i := 1 To m Div 4 Do
Begin
c1 := Pos(S[n], Base64String) - 1;
c2 := Pos(S[n + 1], Base64String) - 1;
c3 := Pos(S[n + 2], Base64String) - 1;
c4 := Pos(S[n + 3], Base64String) - 1;
n := n + 4;
Result := Result + Chr(((c1 Shl 2) And $FC) Or ((c2 Shr 4) And $3));
Result := Result + Chr(((c2 Shl 4) And $F0) Or ((c3 Shr 2) And $0F));
Result := Result + Chr(((c3 Shl 6) And $C0) Or c4);
End;
If m Mod 4 = 2 Then
Begin
c1 := Pos(S[n], Base64String) - 1;
c2 := Pos(S[n + 1], Base64String) - 1;
Result := Result + Chr(((c1 Shl 2) And $FC) Or ((c2 Shr 4) And $3));
End;
If m Mod 4 = 3 Then
Begin
c1 := Pos(S[n], Base64String) - 1;
c2 := Pos(S[n + 1], Base64String) - 1;
c3 := Pos(S[n + 2], Base64String) - 1;
Result := Result + Chr(((c1 Shl 2) And $FC) Or ((c2 Shr 4) And $3));
Result := Result + Chr(((c2 Shl 4) And $F0) Or ((c3 Shr 2) And $0F));
End;
End;
Function Base64EnCodeStr(Const S: String): String; Stdcall;
Begin
Result := Base64EnStr(S, Base64Table, false);
End;
Function Base64DeCodeStr(Const S: String): String; Stdcall;
Begin
Result := Base64DeStr(S, Base64Table, false);
End;
//==============================================================================
Function IsBase64(Const S: String): boolean;
Var
LIntLoop: Integer;
Begin
Result := Trim(S) <> '';
If Result Then
For LIntLoop := 1 To Length(S) Do
If Pos(S[LIntLoop], Base64Table + '=') = 0 Then
Begin
Result := false;
Break;
End;
End;
End.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -