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ステップ」 ...
随机推荐
- idea设置自定义快捷键
在说明之前,先引入一个非常好的 Intellij Idea中文教程:intelliJ Idea中文教程 一创建模板类 二使用方法备注 在IntellijIdea中我并没有找到直接对某个方法进行注释的模 ...
- linux修改root管理员密码
以root 身份登录(SSH操作) 输入 passwd 命令 就可以看到提示输入新密码了 输入密码的时候是看不到字符的.
- find - exec 命令
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是command ...
- datatable 常用参数
DataTables(http://www.datatables.net/)应该是我到目前为止见过的,功能最强大的表格解决方案(当然,不计算其它整套框架中的table控件在内). 先把它主页上写的特性 ...
- SQL索引及表的页的逻辑顺序与物理顺序
1.经过测试发现当聚集索引新建或者重建时,会按照逻辑顺序重新排列数据页和数据页内的数据行的物理顺序. 2.但修改表时,无论是聚集索引还是堆的数据页都是按自然顺序向后插入数据,页面上的偏移量可以证明.因 ...
- 通俗的解释下音视频同步里pcr作用
PCR同步在非硬件精确时钟源的情况还是谨慎使用,gstreamer里面采用PCR同步,但是发现好多ffmpeg转的片儿,或者是CP方的片源,pcr打得很粗糙的,老是有跳帧等现象.音视频同步,有三种方法 ...
- mybatis介绍安装
MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简单 ...
- saltstack内置执行模块groupadd
groupadd模块用于命令行管理用户组 salt.modules.groupadd.add(name, gid=None, system=False) 添加一个用户到指定GID 例:salt '*' ...
- history命令使用方法详解
history是一条非常实用的shell命令,可以显示出之前在shell中运行的命令,配合last显示之前登录的用户,就可以追溯是哪个用户执行了某些命令.以下详细说明history使用中常见的命令或技 ...
- vue2 本地安装