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(, soFromBeginning);
SetLength(abt, sso.Size * );
sso.ReadData(abt, sso.Size * ); 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(, soFromBeginning);
SetLength(abt, sso.Size * );
sso.ReadData(abt, sso.Size * ); 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[..] of Char = ( #, #, #, #, #,
#, #, #, #, #, #, #, #, #, #,
#, #, #, #, #, #, #, #, #, #,
#, #, #, #, #, #, #, #, #, #,
#, #, #, #, #, #, #, #, #, #,
#, #, #, #, #, #, #, #, #, #,
#, #, #, #, #, #, #, #, #); const
BASE64DeTable : array[..] of Byte = ($3E, $7F, $7F, $7F, $3F, $,
$, $, $, $, $, $3A, $3B, $3C, $3D, $7F, $7F, $7F, $7F,
$7F, $7F, $7F, $, $, $, $, $, $, $, $, $, $,
$0A, $0B, $0C, $0D, $0E, $0F, $, $, $, $, $, $, $,
$, $, $, $7F, $7F, $7F, $7F, $7F, $7F, $1A, $1B, $1C, $1D,
$1E, $1F, $, $, $, $, $, $, $, $, $, $, $2A,
$2B, $2C, $2D, $2E, $2F, $, $, $, $); //BASE64算法流加密
procedure EncodeStream(InStream, OutStream : TStream);
var
I, O, Count : Integer;
InBuf : array[..] of Byte;
OutBuf : array[..] of Char; //Char
Temp : Byte;
abt:TBytes;
begin
FillChar(OutBuf, Sizeof(OutBuf), #); repeat
Count := InStream.Read(InBuf, SizeOf(InBuf));
if Count = then Break;
I := ;
O := ;
while I <= (Count-) do begin
{ 编码第一个字节 }
Temp := (InBuf[I] shr );
OutBuf[O] := Char(BASE64Table[Temp and $3F]); { 编码第二个字节 }
Temp := (InBuf[I] shl ) or (InBuf[I+] shr );
OutBuf[O+] := Char(BASE64Table[Temp and $3F]); { 编码第三个字节 }
Temp := (InBuf[I+] shl ) or (InBuf[I+] shr );
OutBuf[O+] := Char(BASE64Table[Temp and $3F]); { 编码第四个字节 }
Temp := (InBuf[I+] and $3F);
OutBuf[O+] := Char(BASE64Table[Temp]); Inc(I, );
Inc(O, );
end; if (I <= Count) then begin
Temp := (InBuf[I] shr );
OutBuf[O] := Char(BASE64Table[Temp and $3F]); { 一个奇数字节 }
if I = Count then begin
Temp := (InBuf[I] shl ) and $;
OutBuf[O+] := Char(BASE64Table[Temp and $3F]);
OutBuf[O+] := '=';
{ 两个基数字节 }
end else begin
Temp := ((InBuf[I] shl ) and $) or ((InBuf[I+] shr ) and $0F);
OutBuf[O+] := Char(BASE64Table[Temp and $3F]);
Temp := (InBuf[I+] shl ) and $3C;
OutBuf[O+] := Char(BASE64Table[Temp and $3F]);
end;
{ 增加= }
OutBuf[O+] := '=';
Inc(O, );
end; { 把编码好的块写到流中 }
OutStream.Write(OutBuf, O*); // 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[..] of Byte;
OutBuf : array[..] of Byte;
begin
repeat
O := ;
I := ; Count := InStream.Read(InBuf, SizeOf(InBuf));
if (Count = ) then
Break; { 解密的数据输入到流中 }
while I < Count do begin
if (InBuf[I] < ) or (InBuf[I] > ) or
(InBuf[I+] < ) or (InBuf[I+] > ) or
(InBuf[I+] < ) or (InBuf[I+] > ) or
(InBuf[I+] < ) or (InBuf[I+] > ) then
raise Exception.Create('Invalid Base64 Character'); c1 := BASE64DeTable[InBuf[I]];
c2 := BASE64DeTable[InBuf[I+]];
c3 := BASE64DeTable[InBuf[I+]];
OutBuf[O] := ((c1 shl ) or (c2 shr ));
Inc(O);
if Char(InBuf[I+]) <> '=' then begin
OutBuf[O] := ((c2 shl ) or (c3 shr ));
Inc(O);
if Char(InBuf[I+]) <> '=' then begin
OutBuf[O] := ((c3 shl ) or BASE64DeTable[InBuf[I+]]);
Inc(O);
end;
end;
Inc(I, );
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[], Length(Input));
InStream.Position := ;
EncodeStream(InStream, OutStream);
OutStream.Position := ;
SetLength(Result, OutStream.Size);
OutStream.Read(Result[], 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[], Length(Input));
InStream.Position := ;
DecodeStream(InStream, OutStream);
OutStream.Position := ;
SetLength(Result, OutStream.Size);
OutStream.Read(Result[], OutStream.Size); InStream.Free;
OutStream.Free;
end; end.

老 base64 for xe8的更多相关文章

  1. [分享黑科技]纯js突破localstorage存储上线,远程抓取图片,并转码base64保存本地,最终实现整个网站所有静态资源离线到用户手机效果却不依赖浏览器的缓存机制,单页应用最新黑科技

    好久没有写博客了,想到2年前答应要放出源代码的也没放出来,最近终于有空先把纯js实现无限空间大小的本地存储的功能开源了,项目地址https://github.com/xueduany/localsto ...

  2. 使用Qpython3制作老版天翼飞TP路由器拨号脚本

    #幻境拨号python版 #by 1414641776 account='xxxxxx@96301' password='xxxxx' # 路由器脚本 def sendToRoute(account, ...

  3. Python之数据加密与解密及相关操作(hashlib模块、hmac模块、random模块、base64模块、pycrypto模块)

    本文内容 数据加密概述 Python中实现数据加密的模块简介 hashlib与hmac模块介绍 random与secrets模块介绍 base64模块介绍 pycrypto模块介绍 总结 参考文档 提 ...

  4. 前端性能优化--图片处理(Css Sprites 与 base64)

    前言: 近期研究着前端性能的优化方面的知识,并以博客记之.之前有相同系列的文章(前端性能优化--图片懒加载(lazyload image)),这次继续是关于图片的处理,css sprites 和 ba ...

  5. 微信小程序语音识别服务搭建全过程解析(https api开放,支持新接口mp3录音、老接口silk录音)

    silk v3(或新录音接口mp3)录音转olami语音识别和语义处理的api服务(ubuntu16.04服务器上实现) 重要的写在前面 重要事项一: 所有相关更新,我优先更新到我个人博客中,其它地方 ...

  6. 将不确定变为确定~老赵写的CodeTimer是代码性能测试的利器

    首先,非常感谢赵老大的CodeTimer,它让我们更好的了解到代码执行的性能,从而可以让我们从性能的角度来考虑问题,有些东西可能我们认为是这样的,但经理测试并非如何,这正应了我之前的那名话:“机器最能 ...

  7. Python之数据加密与解密及相关操作(hashlib、hmac、random、base64、pycrypto)

    本文内容 数据加密概述 Python中实现数据加密的模块简介 hashlib与hmac模块介绍 random与secrets模块介绍 base64模块介绍 pycrypto模块介绍 总结 参考文档 提 ...

  8. Python常用模块--base64

    作用:对一些保密性不强的信息进行加密,变为人类不能直接理解的字符串,但是可以反向解密,是一种‘防君子,不防小人’的措施. 例如:在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动 ...

  9. 使用base64转码的方式上传图片

    1.前端html代码 <input style="width:100%" onchange="loadpicture(1)" type="fil ...

随机推荐

  1. leetcode:Add Two Numbers

    题目描述:You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  2. 51nod1225 余数之和

    打表可以看出规律.分块求就可以了. #include<cstdio> #include<cstring> #include<cctype> #include< ...

  3. 基于Flume的美团日志收集系统(一)架构和设计

    美团的日志收集系统负责美团的所有业务日志的收集,并分别给Hadoop平台提供离线数据和Storm平台提供实时数据流.美团的日志收集系统基于Flume设计和搭建而成. <基于Flume的美团日志收 ...

  4. ISO中运行时简单使用及KVC补充

    一.运行时简单使用 1.包含头文件<objc/message.h> 2.给对象发送消息的方法:objc_msgSend(id, SEL, ....) * 第1个参数是对象 * 第2个参数是 ...

  5. [反汇编练习] 160个CrackMe之022

    [反汇编练习] 160个CrackMe之022. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  6. AutoGenSystem

    #coding=utf-8 # # AutoGenSystem # 声明: # 该软件主要是为了解决Android系统更新时,由于版本很多,管理起来复杂,容易出错,于是采用软件 # 自动对系统软件进行 ...

  7. Spring无配置使用properties文件

    利用@PropertySource注解加载 @Configuration @ComponentScan(basePackages="*") @PropertySource({&qu ...

  8. Java调优之jvm和线程的内存分析

    本文来源于铁木箱子的博客http://www.mzone.cc 这几天因为自己开发的一个网站在768M内存的机器上撑不起100多个用户的运行,因为每个用户启用功能后,系统将为每个用户分配8个左右的独立 ...

  9. MySQL与Oracle 差异比较之七其它

    其它 编号 类别 ORACLE MYSQL 注释 1 内连接的更改 1.select a.*, b.*, c.*, d.*  from a, b, c, d where a.id = b.id   a ...

  10. struts 中 s:iterator 使用注意事项

    后台定义类 public class Course_pj { private String _id; private String _courseid; private String _course_ ...