老 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);

  sso.Seek(0, soFromBeginning);
  SetLength(abt, sso.Size * 2);
  sso.ReadData(abt, sso.Size * 2);

  s1 := TEncoding.Unicode.GetString(abt);
  Edit2.Text := s1;
  ssi.Free;
  sso.Free;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  ssi, sso: TStringStream;
  abt: TBytes;
  s1: string;
begin
  ssi := TStringStream.Create(Edit2.Text);
  sso := TStringStream.Create((‘‘));
  Base64Unit.DecodeStream(ssi, sso);

  sso.Seek(0, soFromBeginning);
  SetLength(abt, sso.Size * 2);
  sso.ReadData(abt, sso.Size * 2);

  Edit4.Text := sso.DataString;
  ssi.Free;
  sso.Free;

end;

unit Base64Unit;

interface

uses
  Classes, SysUtils;

  function Base64Encryption(const Input:String):String;
  function Base64Decryption(const Input:String):String;

  procedure EncodeStream(InStream, OutStream : TStream);
  procedure DecodeStream(InStream, OutStream : TStream);

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;     //Char
  Temp : Byte;
  abt:TBytes;
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*2); // Modified by for xe8 2015-07-30 15:36:51 o > o*2
  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.

时间: 2024-10-26 17:54:14

老 base for xe8的相关文章

使用国内镜像通过pip安装python的一些包 Cannot fetch index base URL http://pypi.python.org/simple/

原文地址:http://www.xuebuyuan.com/1157602.html 学习flask,安装virtualenv环境,这些带都ok,但是一安装包总是出错无法安装, 比如这样超时的问题: (env)[email protected]:~/flask_study/venv-test/test$ easy_install Flask-SQLAlchemy Searching for Flask-SQLAlchemy Reading http://pypi.python.org/simpl

从分布式一致性谈到CAP理论、BASE理论

问题的提出 在计算机科学领域,分布式一致性是一个相当重要且被广泛探索与论证问题,首先来看三种业务场景. 1.火车站售票 假如说我们的终端用户是一位经常坐火车的旅行家,通常他是去车站的售票处购买车票,然后拿着车票去检票口,再坐上火车,开始一段美好的旅行----一切似乎都是那么和谐.想象一下,如果他选择的目的地是杭州,而某一趟开往杭州的火车只剩下最后一张车票,可能在同一时刻,不同售票窗口的另一位乘客也购买了同一张车票.假如说售票系统没有进行一致性的保障,两人都购票成功了.而在检票口检票的时候,其中一

Atitit.code&#160;base&#160;view&#160;视图的实现原理

Atitit.code base view 视图的实现原理 1. 视图的执行算法:1 2. 不可更新的视图:1 3. 关于视图的可插入性:insert2 4. 视图定义3 5. 调用3 1. 视图的执行算法: 存在两种执行算法: 1.  Merge:合并的执行方式,每当执行的时候,先将我们视图的sql语句与外部查询视图的sql语句,混合在一起,最终执行: 2.  Temptable:临时表模式,每当查询的时候,将视图所使用的select语句生成一个结果的临时表,再在当前的临时表内进行查询. 指的

Atitit。Time&#160;base&#160;gc&#160;垃圾&#160;资源&#160;收集的原理与设计

Atitit.Time base gc 垃圾 资源 收集的原理与设计 1. MRC(MannulReference Counting手动 retain/release/autorelease语句1 2. 自动垃圾回收(GC)1 3. Arc ARC介于自动垃圾回收(GC)和手动内存管理之间.1 3.1. 从各种垃圾收集算法最基本的运行方式来说,大概可以分成三个类型: 1. 引用计数(reference counting):1 3.2. 标记-清扫(mark-sweep)2 3.3. 3. 节点复

顺序栈的基本操作中Push压入后的- S.top = S.base + S.stacksize; 作用

#include <stdio.h> #include <malloc.h> #define TRUE 1 #define OK 1 #define ERROR 0 #define FALSE 0 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int Status; typedef int SElemType; struct SqStack { SElemType *base; SElemType *to

【Unity Shaders】游戏性和画面特效——创建一个老电影式的画面特效

本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源(当然你也可以从官网下载). ========================================== 分割线 ========================================== 写在前面 终于到了本书的最后一章了,好激动有木有!作为压轴章,虽然只有两篇,但每篇的内容是比之前的任

The POM for XXX:jar:${com.ld.base.service.version} is missing, no dependency information available

最近有个jar改了名字后,有个依赖它的工程死活引用的是老名字,导致打包的时候出错,如下所示: [INFO] ------------------------------------------------------------------------[INFO] Building com.ld.base.runtime 1.0.0-SNAPSHOT[INFO] -------------------------------------------------------------------

exception &#39;yii\base\ErrorException&#39; with message &#39;Class &#39;MongoClient&#39; not found&#39;

问题描述: 本来项目运行的好好的,搬了一次办公室(电脑主机一起搬的),第二天的时候就登录不了了. php版本和扩展没有改变,且没有修改任何配置,我尝试重启php5-fpm 服务,又重启nginx服务,再登录还是老样子. 报错如下: exception 'yii\base\ErrorException' with message 'Class 'MongoClient' not found' in /home/user/YiiProject/src/vendor/yiisoft/yii2-mong

快速入门系列--WebAPI--04在老版本MVC4下的调整

WebAPI是建立在MVC和WCF的基础上的,原来微软老是喜欢封装的很多,这次终于愿意将http编程模型的相关细节暴露给我们了.在之前的介绍中,基本上都基于.NET 4.5之后版本,其System.Net.Http程序集非常的丰富,而老版本的则相对较弱.在WebAPI v1.0(和ASP.NET MVC4在一起的版本)很多的类和接口并不存在,同时对Task异步编程(ApiController默认提供异步执行方法)的支持还有一些欠缺(缺少不少方便的扩展方法),在使用时会有一些需要注意的地方,由于一