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;

时间: 2024-11-04 19:25:13

unit Base64Unit;的相关文章

老 base for xe8

not recommend ,only for study procedure TForm1.Button3Click(Sender: TObject); var ssi, sso: TStringStream; abt: TBytes; s1: string; begin ssi := TStringStream.Create(Edit1.Text); sso := TStringStream.Create(('')); Base64Unit.EncodeStream(ssi, sso); s

mysqld服务启动失败, Failed to restart mysqld.service: Unit not found.

-bash-4.2# service mysqld restart Redirecting to /bin/systemctl restart mysqld.serviceFailed to restart mysqld.service: Unit not found. 并不存在 mysqld 的服务, -bash-4.2# -bash-4.2# chkconfig -list -list: unknown option -bash-4.2# chkconfig --list Note: Thi

10.23 linux任务计划cron10.24chkconfig工具10.25 systemd管理服务10.26 unit介绍 10.27 target介绍

- 10.23 linux任务计划cron - 10.24 chkconfig工具 - 10.25 systemd管理服务 - 10.26 unit介绍 - 10.27 target介绍 - 扩展 1. anacron http://blog.csdn.net/strikers1982/article/details/4787226  2. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装) http://blog.sina.com.cn/s/blog_46

CentOS 7 防火墙 出现Failed to start iptables.service: Unit iptables.service failed to load

错误信息如下: [root]# service iptables start Redirecting to /bin/systemctl start iptables.service Failed to start iptables.service: Unit iptables.service failed to load: No such file or directory.解决方法如下: 一直用CentOS 6 习惯了,一下没适应过来.防火墙配置后执行service iptables sav

GRU(Gated Recurrent Unit) 更新过程推导及简单代码实现

GRU(Gated Recurrent Unit) 更新过程推导及简单代码实现 RNN GRU matlab codes RNN网络考虑到了具有时间数列的样本数据,但是RNN仍存在着一些问题,比如随着时间的推移,RNN单元就失去了对很久之前信息的保存和处理的能力,而且存在着gradient vanishing问题. 所以有些特殊类型的RNN网络相继被提出,比如LSTM(long short term memory)和GRU(gated recurrent unit)(Chao,et al. 20

2 unit 4

###unit.4管理系统存储### ###1.分区划分 fdisk /dev/vdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Buil

2 unit 5

###unit.5 LVM### ###1.LVM建立 1.划分物理分区并把分区id修改为8e [[email protected] ~]# fdisk /dev/vdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not

(4.5.4)Android测试TestCase单元(Unit test)测试和instrumentationCase单元测试

Android单元和instrumentation单元测试 Developing Android unit and instrumentation tests Android的单元测试是基于JUnit的.可分为: 1.本地单元测试 - 可以在JVM上运行测试(速度快,优先考虑). 2.Instrumented单元测试 - 需要Android系统 Android的Gradle插件支持在JVM上执行Andr??oid单元测试.它使用特殊版本的android.jar(也称为 Android mocka

ABP领域层——工作单元(Unit Of work)

ABP领域层——工作单元(Unit Of work) 点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之12.ABP领域层——工作单元(Unit Of work) ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ABP的官方网站:http://www.aspnetboilerplate.com ABP在Github上的开源项目:https://github.com/aspnetboilerplate 通用