1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266:
|
unit Main_Unit;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) Memo1: TMemo; Btn_Base64_Encode: TButton; Btn_Base64_Decode: TButton; procedure Btn_Base64_EncodeClick(Sender: TObject); procedure Btn_Base64_DecodeClick(Sender: TObject); private public end;
var Form1: TForm1; function B64Encode(const S: string): string; function B64Decode(const S: string): string;
implementation
{$R *.dfm}
const B64Table= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
function B64Encode; var i: integer; InBuf: array[0..2] of byte; OutBuf: array[0..3] of char; begin SetLength(Result,((Length(S)+2) div 3)*4); for i:= 1 to ((Length(S)+2) div 3) do begin if Length(S)< (i*3) then Move(S[(i-1)*3+1],InBuf,Length(S)-(i-1)*3) else Move(S[(i-1)*3+1],InBuf,3); OutBuf[0]:= B64Table[((InBuf[0] and $FC) shr 2) + 1]; OutBuf[1]:= B64Table[(((InBuf[0] and $03) shl 4) or ((InBuf[1] and $F0) shr 4)) + 1]; OutBuf[2]:= B64Table[(((InBuf[1] and $0F) shl 2) or ((InBuf[2] and $C0) shr 6)) + 1]; OutBuf[3]:= B64Table[(InBuf[2] and $3F) + 1]; Move(OutBuf,Result[(i-1)*4+1],4); end; if (Length(S) mod 3)= 1 then begin Result[Length(Result)-1]:= '='; Result[Length(Result)]:= '='; end else if (Length(S) mod 3)= 2 then Result[Length(Result)]:= '='; end;
function B64Decode; var i: integer; InBuf: array[0..3] of byte; OutBuf: array[0..2] of byte; begin if (Length(S) mod 4)<> 0 then raise Exception.Create('Base64: Incorrect string format'); SetLength(Result,((Length(S) div 4)-1)*3); for i:= 1 to ((Length(S) div 4)-1) do begin Move(S[(i-1)*4+1],InBuf,4); if (InBuf[0]> 64) and (InBuf[0]< 91) then Dec(InBuf[0],65) else if (InBuf[0]> 96) and (InBuf[0]< 123) then Dec(InBuf[0],71) else if (InBuf[0]> 47) and (InBuf[0]< 58) then Inc(InBuf[0],4) else if InBuf[0]= 43 then InBuf[0]:= 62 else InBuf[0]:= 63; if (InBuf[1]> 64) and (InBuf[1]< 91) then Dec(InBuf[1],65) else if (InBuf[1]> 96) and (InBuf[1]< 123) then Dec(InBuf[1],71) else if (InBuf[1]> 47) and (InBuf[1]< 58) then Inc(InBuf[1],4) else if InBuf[1]= 43 then InBuf[1]:= 62 else InBuf[1]:= 63; if (InBuf[2]> 64) and (InBuf[2]< 91) then Dec(InBuf[2],65) else if (InBuf[2]> 96) and (InBuf[2]< 123) then Dec(InBuf[2],71) else if (InBuf[2]> 47) and (InBuf[2]< 58) then Inc(InBuf[2],4) else if InBuf[2]= 43 then InBuf[2]:= 62 else InBuf[2]:= 63; if (InBuf[3]> 64) and (InBuf[3]< 91) then Dec(InBuf[3],65) else if (InBuf[3]> 96) and (InBuf[3]< 123) then Dec(InBuf[3],71) else if (InBuf[3]> 47) and (InBuf[3]< 58) then Inc(InBuf[3],4) else if InBuf[3]= 43 then InBuf[3]:= 62 else InBuf[3]:= 63; OutBuf[0]:= (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $03); OutBuf[1]:= (InBuf[1] shl 4) or ((InBuf[2] shr 2) and $0F); OutBuf[2]:= (InBuf[2] shl 6) or (InBuf[3] and $3F); Move(OutBuf,Result[(i-1)*3+1],3); end; if Length(S)<> 0 then begin Move(S[Length(S)-3],InBuf,4); if InBuf[2]= 61 then begin if (InBuf[0]> 64) and (InBuf[0]< 91) then Dec(InBuf[0],65) else if (InBuf[0]> 96) and (InBuf[0]< 123) then Dec(InBuf[0],71) else if (InBuf[0]> 47) and (InBuf[0]< 58) then Inc(InBuf[0],4) else if InBuf[0]= 43 then InBuf[0]:= 62 else InBuf[0]:= 63; if (InBuf[1]> 64) and (InBuf[1]< 91) then Dec(InBuf[1],65) else if (InBuf[1]> 96) and (InBuf[1]< 123) then Dec(InBuf[1],71) else if (InBuf[1]> 47) and (InBuf[1]< 58) then Inc(InBuf[1],4) else if InBuf[1]= 43 then InBuf[1]:= 62 else InBuf[1]:= 63; OutBuf[0]:= (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $03); Result:= Result + char(OutBuf[0]); end else if InBuf[3]= 61 then begin if (InBuf[0]> 64) and (InBuf[0]< 91) then Dec(InBuf[0],65) else if (InBuf[0]> 96) and (InBuf[0]< 123) then Dec(InBuf[0],71) else if (InBuf[0]> 47) and (InBuf[0]< 58) then Inc(InBuf[0],4) else if InBuf[0]= 43 then InBuf[0]:= 62 else InBuf[0]:= 63; if (InBuf[1]> 64) and (InBuf[1]< 91) then Dec(InBuf[1],65) else if (InBuf[1]> 96) and (InBuf[1]< 123) then Dec(InBuf[1],71) else if (InBuf[1]> 47) and (InBuf[1]< 58) then Inc(InBuf[1],4) else if InBuf[1]= 43 then InBuf[1]:= 62 else InBuf[1]:= 63; if (InBuf[2]> 64) and (InBuf[2]< 91) then Dec(InBuf[2],65) else if (InBuf[2]> 96) and (InBuf[2]< 123) then Dec(InBuf[2],71) else if (InBuf[2]> 47) and (InBuf[2]< 58) then Inc(InBuf[2],4) else if InBuf[2]= 43 then InBuf[2]:= 62 else InBuf[2]:= 63; OutBuf[0]:= (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $03); OutBuf[1]:= (InBuf[1] shl 4) or ((InBuf[2] shr 2) and $0F); Result:= Result + char(OutBuf[0]) + char(OutBuf[1]); end else begin if (InBuf[0]> 64) and (InBuf[0]< 91) then Dec(InBuf[0],65) else if (InBuf[0]> 96) and (InBuf[0]< 123) then Dec(InBuf[0],71) else if (InBuf[0]> 47) and (InBuf[0]< 58) then Inc(InBuf[0],4) else if InBuf[0]= 43 then InBuf[0]:= 62 else InBuf[0]:= 63; if (InBuf[1]> 64) and (InBuf[1]< 91) then Dec(InBuf[1],65) else if (InBuf[1]> 96) and (InBuf[1]< 123) then Dec(InBuf[1],71) else if (InBuf[1]> 47) and (InBuf[1]< 58) then Inc(InBuf[1],4) else if InBuf[1]= 43 then InBuf[1]:= 62 else InBuf[1]:= 63; if (InBuf[2]> 64) and (InBuf[2]< 91) then Dec(InBuf[2],65) else if (InBuf[2]> 96) and (InBuf[2]< 123) then Dec(InBuf[2],71) else if (InBuf[2]> 47) and (InBuf[2]< 58) then Inc(InBuf[2],4) else if InBuf[2]= 43 then InBuf[2]:= 62 else InBuf[2]:= 63; if (InBuf[3]> 64) and (InBuf[3]< 91) then Dec(InBuf[3],65) else if (InBuf[3]> 96) and (InBuf[3]< 123) then Dec(InBuf[3],71) else if (InBuf[3]> 47) and (InBuf[3]< 58) then Inc(InBuf[3],4) else if InBuf[3]= 43 then InBuf[3]:= 62 else InBuf[3]:= 63; OutBuf[0]:= (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $03); OutBuf[1]:= (InBuf[1] shl 4) or ((InBuf[2] shr 2) and $0F); OutBuf[2]:= (InBuf[2] shl 6) or (InBuf[3] and $3F); Result:= Result + Char(OutBuf[0]) + Char(OutBuf[1]) + Char(OutBuf[2]); end; end; end;
procedure TForm1.Btn_Base64_EncodeClick(Sender: TObject); var i: Integer;
begin i := StrToInt(Memo1.Text);
Memo1.Text := IntToStr(i); end;
procedure TForm1.Btn_Base64_DecodeClick(Sender: TObject); begin end;
end. |