unit Base64Unit;

unit Base64Unit;
//Download by http://www.codefans.net
interface uses
Classes, SysUtils; function Base64Encryption(const Input:String):String;
function Base64Decryption(const Input:String):String; implementation const
BASE64Table : array[0..63] of Char = ( #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, #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, #48, #49, #50,
#51, #52, #53, #54, #55, #56, #57, #43, #47); const
BASE64DeTable : array[43..122] of Byte = ($3E, $7F, $7F, $7F, $3F, $34,
$35, $36, $37, $38, $39, $3A, $3B, $3C, $3D, $7F, $7F, $7F, $7F,
$7F, $7F, $7F, $00, $01, $02, $03, $04, $05, $06, $07, $08, $09,
$0A, $0B, $0C, $0D, $0E, $0F, $10, $11, $12, $13, $14, $15, $16,
$17, $18, $19, $7F, $7F, $7F, $7F, $7F, $7F, $1A, $1B, $1C, $1D,
$1E, $1F, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $2A,
$2B, $2C, $2D, $2E, $2F, $30, $31, $32, $33); //BASE64算法流加密
procedure EncodeStream(InStream, OutStream : TStream);
var
I, O, Count : Integer;
InBuf : array[1..45] of Byte;
OutBuf : array[0..62] of Char;
Temp : Byte;
begin
FillChar(OutBuf, Sizeof(OutBuf), #0); repeat
Count := InStream.Read(InBuf, SizeOf(InBuf));
if Count = 0 then Break;
I := 1;
O := 0;
while I <= (Count-2) do begin
{ 编码第一个字节 }
Temp := (InBuf[I] shr 2);
OutBuf[O] := Char(BASE64Table[Temp and $3F]); { 编码第二个字节 }
Temp := (InBuf[I] shl 4) or (InBuf[I+1] shr 4);
OutBuf[O+1] := Char(BASE64Table[Temp and $3F]); { 编码第三个字节 }
Temp := (InBuf[I+1] shl 2) or (InBuf[I+2] shr 6);
OutBuf[O+2] := Char(BASE64Table[Temp and $3F]); { 编码第四个字节 }
Temp := (InBuf[I+2] and $3F);
OutBuf[O+3] := Char(BASE64Table[Temp]); Inc(I, 3);
Inc(O, 4);
end; if (I <= Count) then begin
Temp := (InBuf[I] shr 2);
OutBuf[O] := Char(BASE64Table[Temp and $3F]); { 一个奇数字节 }
if I = Count then begin
Temp := (InBuf[I] shl 4) and $30;
OutBuf[O+1] := Char(BASE64Table[Temp and $3F]);
OutBuf[O+2] := '=';
{ 两个基数字节 }
end else begin
Temp := ((InBuf[I] shl 4) and $30) or ((InBuf[I+1] shr 4) and $0F);
OutBuf[O+1] := Char(BASE64Table[Temp and $3F]);
Temp := (InBuf[I+1] shl 2) and $3C;
OutBuf[O+2] := Char(BASE64Table[Temp and $3F]);
end;
{ 增加= }
OutBuf[O+3] := '=';
Inc(O, 4);
end; { 把编码好的块写到流中 }
OutStream.Write(OutBuf, O);
until Count < SizeOf(InBuf);
end; //BASE64算法流解密
procedure DecodeStream(InStream, OutStream : TStream);
var
I, O, Count, c1, c2, c3 : Byte;
InBuf : array[0..87] of Byte;
OutBuf : array[0..65] of Byte;
begin
repeat
O := 0;
I := 0; Count := InStream.Read(InBuf, SizeOf(InBuf));
if (Count = 0) then
Break; { 解密的数据输入到流中 }
while I < Count do begin
if (InBuf[I] < 43) or (InBuf[I] > 122) or
(InBuf[I+1] < 43) or (InBuf[I+1] > 122) or
(InBuf[I+2] < 43) or (InBuf[I+2] > 122) or
(InBuf[I+3] < 43) or (InBuf[I+3] > 122) then
raise Exception.Create('Invalid Base64 Character'); c1 := BASE64DeTable[InBuf[I]];
c2 := BASE64DeTable[InBuf[I+1]];
c3 := BASE64DeTable[InBuf[I+2]];
OutBuf[O] := ((c1 shl 2) or (c2 shr 4));
Inc(O);
if Char(InBuf[I+2]) <> '=' then begin
OutBuf[O] := ((c2 shl 4) or (c3 shr 2));
Inc(O);
if Char(InBuf[I+3]) <> '=' then begin
OutBuf[O] := ((c3 shl 6) or BASE64DeTable[InBuf[I+3]]);
Inc(O);
end;
end;
Inc(I, 4);
end;
OutStream.Write(OutBuf, O);
until Count < SizeOf(InBuf);
end; //BASE64算法字符串加密
function Base64Encryption(const Input:String):String;
var
InStream : TMemoryStream;
OutStream : TMemoryStream;
begin
InStream := TMemoryStream.Create;
OutStream := TMemoryStream.Create; InStream.Write(Input[1], Length(Input));
InStream.Position := 0;
EncodeStream(InStream, OutStream);
OutStream.Position := 0;
SetLength(Result, OutStream.Size);
OutStream.Read(Result[1], OutStream.Size); InStream.Free;
OutStream.Free;
end; //BASE64算法字符串解密
function Base64Decryption(const Input:String):String;
var
InStream : TMemoryStream;
OutStream : TMemoryStream;
begin
InStream := TMemoryStream.Create;
OutStream := TMemoryStream.Create; InStream.Write(Input[1], Length(Input));
InStream.Position := 0;
DecodeStream(InStream, OutStream);
OutStream.Position := 0;
SetLength(Result, OutStream.Size);
OutStream.Read(Result[1], OutStream.Size); InStream.Free;
OutStream.Free;
end; end.

  

procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text:=BASE64Encryption(Edit1.Text);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Edit4.Text:=BASE64Decryption(Edit3.Text);
end;

最新文章

  1. HDU 3038 How Many Answers Are Wrong(种类并查集)
  2. ZXingObjC 64位 集成到自己的项目中(xcode 6.4)
  3. ios申请真机调试( xcode 5)详细解析
  4. linux网络编程系列-网络连接的建立
  5. Java HashMap、LinkedHashMap
  6. 文件流操作(FileStream,StreamReader,StreamWriter)
  7. Java执行命令行脚本
  8. ASP.NET 资料下载
  9. C++/C# 最基本的Marshal和Ptr
  10. activity的横屏和竖屏设置
  11. Cleaner, more elegant, and harder to recognize(翻译)
  12. java常用类--系统相关
  13. 关键字:This(上)
  14. .net core Swagger 过滤部分Api
  15. android菜鸟,了解android工程目录结构
  16. IDEA中添加servlet的jar
  17. EOS源码
  18. 在delphi中生成GUID/自动获取临时表名......
  19. H5学习的例子
  20. Mysql读写分离方案-MySQL Proxy环境部署记录

热门文章

  1. 【边做项目边学Android】小白会遇到的问题--Appcompat_V7问题
  2. centos 防火墙开放80端口
  3. strpos 判断字符串是否存在
  4. 嵌入式开发之simulation--- 双目移动dsp机器人
  5. HDOJ 4923 Room and Moor
  6. poj1861 最小生成树 prim &amp;amp; kruskal
  7. (转)linux设备驱动之USB数据传输分析 二
  8. gulp安装教程
  9. VS2017生成类库选择Release失效的问题
  10. Linux安装Nginx使用反向代理