unit Base64Unit;
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;
unit Base64Unit;的更多相关文章
- 老 base64 for xe8
not recommend ,only for study procedure TForm1.Button3Click(Sender: TObject); var ssi, sso: TStringS ...
- ABP(现代ASP.NET样板开发框架)系列之12、ABP领域层——工作单元(Unit Of work)
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之12.ABP领域层——工作单元(Unit Of work) ABP是“ASP.NET Boilerplate Pr ...
- ABP源码分析十:Unit Of Work
ABP以AOP的方式实现UnitOfWork功能.通过UnitOfWorkRegistrar将UnitOfWorkInterceptor在某个类被注册到IOCContainner的时候,一并添加到该类 ...
- Failed to stop iptables.service: Unit iptables.service not loaded.
redhat 7 [root@lk0 ~]# service iptables stop Redirecting to /bin/systemctl stop iptables.service Fai ...
- VS2012 Unit Test 个人学习汇总(含目录)
首先,给出MSDN相关地址:http://msdn.microsoft.com/en-us/library/Microsoft.VisualStudio.TestTools.UnitTesting.a ...
- VS2012 Unit Test —— 我对IdleTest库动的大手术以及对Xml相关操作进行测试的方式
[1]我的IdleTest源码地址:http://idletest.codeplex.com/ [2]IdleTest改动说明:2013年10月份在保持原有功能的情况下对其动了较大的手术,首先将基本的 ...
- VS2012 Unit Test——Microsoft Fakes入门
如题,本文主要作为在VS2012使用Fakes的入门示例,开发工具必须是VS2012或更高版本. 关于Fakes的MSDN地址:http://msdn.microsoft.com/en-us/libr ...
- MTU(Maximum transmission unit) 最大传输单元
最大传输单元(Maximum transmission unit),以太网MTU为1500. 不同网络MTU如下: 如果最大报文数据大小(MSS)超过MTU,则会引起分片操作. 路径MTU: 网路 ...
- Simulink Memory vs Unit Delay
Memoryブロック.Unit Delayブロック共に前回の入力値を出力しますが.動作するタイミングが異なります. ●Memoryブロック シミュレーションの各時刻(ステップ)で動作し.「1ステップ」 ...
随机推荐
- Swing与javafx直接调用
Swing调用javafx 调用方法: Platform.runLater(new Runnable(){ @Override public void run() { WebView webView ...
- 【数据挖掘】分类之decision tree(转载)
[数据挖掘]分类之decision tree. 1. ID3 算法 ID3 算法是一种典型的决策树(decision tree)算法,C4.5, CART都是在其基础上发展而来.决策树的叶子节点表示类 ...
- vmware workstation导出ovf
ovf tool路径 /Applications/VMware Fusion.app/Contents/Library/VMware OVF Tool 上面红色的部分,需要右键点击应用程序中的VMwa ...
- wamp 两个不同的php.ini
最近在本地开发的windows wamp环境安装一个vld 扩展,碰见一个奇怪的问题,phpinfo() 有 而cli 命令模式里面却没有 最后发现wamp phpinfo()和cli命令模式指向的p ...
- laravel学习之路1:认证相关
Laravel中Auth::guard()表示什么意思? Auth::check() 是判断用户是否登录的方法,如果使用的默认用户系统,那这样使用没问题. 但是使用两组用户的话,如何使用各组用户的功能 ...
- Linux中crontab下scp文件传输的两种方式
Linux下文件传输一般有两个命令scp.ftp(工具需要下载安装) 本文主要讲讲scp的文件传输脚本 1.scp ssh-keygen -t rsa免输入密码,传输 这里假设主机A 用来获到主机B的 ...
- 3354 [IOI2005]河流
题目描述 几乎整个Byteland王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河,最后这条大河流进了大海.这条大河的入海口处有一个村庄——名叫 ...
- Nearest Common Ancestors(LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- Arrays类--Arrays.asList()方法使用
java.util类 Arrays java.lang.Object——java.util.Arrays public class Arrays extends Object 此类包含用来操作数组(比 ...
- JS分段传输数据
<SCRIPT language=javascript> file://数据拆分,并放到相应的hidden域中,在Form的onSubmit事件中激发 function fnPreHand ...