const
BASE_OFFSET = $3C;
XOR_KEY = $AC;
function GetEncodeSize(InSize: Integer): Integer;
begin
Result := (InSize * 4 + 2) div 3;
end;
function GetDecodeSize(InSize: Integer): Integer;
begin
Result := Trunc(InSize * 3 / 4);
end;
function Encode6BitBuf2(Src, Dest: PChar; SrcLen, DestLen: Integer): Integer;
var
I, RoundCount, Remainder: Integer;
pbSrc, pbDest: PByte;
Byte0, Byte1, Byte2: Byte;
begin
Result := GetEncodeSize(SrcLen); 获取加密后数据长度
if Result > DestLen then
begin
SrcLen := GetDecodeSize(DestLen);
Result := GetEncodeSize(SrcLen);
end;
pbSrc := PByte(Src);
pbDest := PByte(Dest);
RoundCount := SrcLen div 3; // 获取轮述
for I := 0 to RoundCount - 1 do
begin
Byte0 := pbSrc^ xor XOR_KEY; Inc(pbSrc); // 把此轮的三个字节进行异或加密
Byte1 := pbSrc^ xor XOR_KEY; Inc(pbSrc);
Byte2 := pbSrc^ xor XOR_KEY; Inc(pbSrc);
pbDest^ := ((Byte0 and $F0) shr 2) or (Byte0 and 3) + BASE_OFFSET; // 取第一位的 0x11110011
Inc(pbDest);
pbDest^ := ((Byte1 and $F0) shr 2) or (Byte1 and 3) + BASE_OFFSET; // 取第二字节的 0x11110011
Inc(pbDest);
pbDest^ := (Byte2 and $3F) + BASE_OFFSET; // 取三字节的0x00111111
Inc(pbDest);
pbDest^ := (((Byte2 and $C0) shr 2) or (Byte0 and $0C) or ((Byte1 and $0C) shr 2)) + BASE_OFFSET;
//去第一字节0x00001100 第二字节的0x00001100 第三字节0x11000000
Inc(pbDest);
end;
Remainder := SrcLen mod 3; // 获取剩余字节的个数
if Remainder = 1 then
begin
Byte0 := pbSrc^ xor XOR_KEY; Inc(pbSrc); // 剩余一个字节
pbDest^ := ((Byte0 and $F0) shr 2) or (Byte0 and 3) + BASE_OFFSET; //取第一字节的0x11110011字节
Inc(pbDest);
pbDest^ := ((Byte0 and $0C) shr 2) + BASE_OFFSET; // 取第一字节的0x00001100
Inc(pbDest);
end
else if Remainder = 2 then // 剩余两个字节
begin
Byte0 := pbSrc^ xor XOR_KEY; Inc(pbSrc);
Byte1 := pbSrc^ xor XOR_KEY; Inc(pbSrc);
pbDest^ := ((Byte0 and $F0) shr 2) or (Byte0 and 3) + BASE_OFFSET; // 第一字节的0x11110011字节
Inc(pbDest);
pbDest^ := ((Byte1 and $F0) shr 2) or (Byte1 and 3) + BASE_OFFSET; // 第二字节的0x11110011字节
Inc(pbDest);
pbDest^ := (Byte0 and $0C) or ((Byte1 and $0C) shr 2) + BASE_OFFSET; // 取第一字节的0x00001100 第二字节0x00001100
Inc(pbDest);
end;
end;
function Decode6BitBuf2(Src, Dest: PChar; SrcLen, DestLen: Integer): Integer;
const
BASE_OFFSET = $3C;
var
I, RoundCount, Remainder: Integer;
pbSrc, pbDest: PByte;
Byte0, Byte1, Byte2, Byte3: Byte;
begin
Result := GetDecodeSize(SrcLen);
if Result > DestLen then
begin
SrcLen := GetEncodeSize(DestLen);
Result := GetDecodeSize(SrcLen);
end;
pbSrc := PByte(Src);
pbDest := PByte(Dest);
RoundCount := SrcLen div 4; // RoundCount 执行轮数
for I := 0 to RoundCount - 1 do
begin
Byte0 := (pbSrc^ - BASE_OFFSET); Inc(pbSrc); // 对解密字符减去基础字符
Byte1 := (pbSrc^ - BASE_OFFSET); Inc(pbSrc);
Byte2 := (pbSrc^ - BASE_OFFSET); Inc(pbSrc);
Byte3 := (pbSrc^ - BASE_OFFSET); Inc(pbSrc);
pbDest^ := ((Byte0 shl 2 ) and $F3) or (Byte3 and $0C) or (Byte0 and $03); // 取第一字节0x11110011和第四字节0x00001100
pbDest^ := pbDest^ xor XOR_KEY; // 异或解密
Inc(pbDest);
pbDest^ := ((Byte1 shl 2) and $F3) or ((Byte3 and $03) shl 2) or (Byte1 and $03);
pbDest^ := pbDest^ xor XOR_KEY;
Inc(pbDest);
pbDest^ := ((Byte3 and $30) shl 2) or Byte2;
pbDest^ := pbDest^ xor XOR_KEY;
Inc(pbDest);
end;
Remainder := SrcLen mod 4;
if Remainder = 2 then
begin
Byte0 := (pbSrc^ - BASE_OFFSET); Inc(pbSrc);
Byte1 := (pbSrc^ - BASE_OFFSET) ; Inc(pbSrc);
pbDest^ := ((Byte0 shl 2) and $F3) or ((Byte1 and $03) shl 2) or (Byte0 and $03);
pbDest^ := pbDest^ xor XOR_KEY;
Inc(pbDest);
end
else if Remainder = 3 then
begin
Byte0 :=(pbSrc^ - BASE_OFFSET); Inc(pbSrc);
Byte1 :=(pbSrc^ - BASE_OFFSET); Inc(pbSrc);
Byte2 :=(pbSrc^ - BASE_OFFSET); Inc(pbSrc);
pbDest^ := ((Byte0 shl 2) and $F3) or (Byte2 and $0C) or (Byte0 and $03);
pbDest^ := pbDest^ xor XOR_KEY;
Inc(pbDest);
pbDest^ := ((Byte1 shl 2) and $F3) or ((Byte2 and 3) shl 2) or (Byte1 and $03);
pbDest^ := pbDest^ xor XOR_KEY;
Inc(pbDest);
end;
end;