📄 edcodeunit.pas
字号:
LeaveCriticalSection(CSEncode);
end;
end;
procedure DecodeBuffer(Src: string; Buf: PChar; bufsize: Integer);
var
EncBuf: array[0..BUFFERSIZE - 1] of Char;
begin
try
EnterCriticalSection(CSEncode);
Decode6BitBuf(PChar(Src), @EncBuf, Length(Src), SizeOf(EncBuf));
Move(EncBuf, Buf^, bufsize);
finally
LeaveCriticalSection(CSEncode);
end;
end;
function EncodeString(Str: string): string;
var
EncBuf: array[0..BUFFERSIZE - 1] of Char;
begin
try
EnterCriticalSection(CSEncode);
Encode6BitBuf(PChar(Str), @EncBuf, Length(Str), SizeOf(EncBuf));
Result := StrPas(EncBuf);
finally
LeaveCriticalSection(CSEncode);
end;
end;
function EncodeBuffer(Buf: PChar; bufsize: Integer): string;
var
EncBuf, TempBuf: array[0..BUFFERSIZE - 1] of Char;
begin
try
EnterCriticalSection(CSEncode);
if bufsize < BUFFERSIZE then begin
Move(Buf^, TempBuf, bufsize);
Encode6BitBuf(@TempBuf, @EncBuf, bufsize, SizeOf(EncBuf));
Result := StrPas(EncBuf);
end else Result := '';
finally
LeaveCriticalSection(CSEncode);
end;
end;
function ReverseStr(SourceStr: string): string;
var
Counter: Integer;
begin
Result := '';
for Counter := 1 to Length(SourceStr) do
Result := SourceStr[Counter] + Result;
end;
{function Encry(Src, Key: string): string;
var
sSrc, sKey: string;
begin
EnterCriticalSection(CSEncode);
try
if Key = '' then sKey := IntToStr(240621028)
else sKey := Key;
sSrc := EncryStrHex(Src, sKey);
Result := ReverseStr(sSrc);
finally
LeaveCriticalSection(CSEncode);
end;
end;
function Decry(Src, Key: string): string;
var
sSrc, sKey: string;
begin
EnterCriticalSection(CSEncode);
try
try
if Key = '' then sKey := IntToStr(240621028)
else sKey := Key;
sSrc := ReverseStr(Src);
Result := DecryStrHex(sSrc, sKey);
except
Result := '';
end;
finally
LeaveCriticalSection(CSEncode);
end;
end;}
function Chinese2UniCode(AiChinese: string): Integer;
var
Ch, cl: string[2];
A: array[1..2] of Char;
begin
StringToWideChar(Copy(AiChinese, 1, 2), @(A[1]), 2);
Ch := IntToHex(Integer(A[2]), 2);
cl := IntToHex(Integer(A[1]), 2);
Result := StrToInt('$' + Ch + cl);
end;
function GetUniCode(msg: string): Integer;
var
I: Integer;
begin
Result := -1;
for I := 1 to Length(msg) do begin
Result := Result + Chinese2UniCode(msg[I]) * I;
end;
end;
function PowMod(base: Int64; pow: Int64; n: Int64): Int64;
var
A, b, c: Int64;
begin
A := base;
b := pow;
c := 1;
while (b > 0) do begin
while (not ((b and 1) > 0)) do begin
b := b shr 1;
A := A * A mod n;
end;
Dec(b);
c := A * c mod n;
end;
Result := c;
end;
//RSA的加密和解密函数,等价于(m^e) mod n(即m的e次幂对n求余)
function Encrypt_Decrypt(m: Int64; E: Int64 = $2C86F9; n: Int64 = $69AAA0E3): Integer;
var
A, b, c: Int64;
nN: Integer;
const
nNumber = 100000;
MaxValue = 1400000000;
MinValue = 1299999999;
function GetInteger(n: Int64): Int64;
var
D: Int64;
begin
D := n;
while D > MaxValue do D := D - nNumber;
while D < MinValue do D := D + nNumber;
if D = MinValue then D := D + m;
if D = MaxValue then D := D - m;
Result := D;
end;
begin
EnterCriticalSection(CSEncode);
try
A := m;
b := E;
c := 1;
while b <> 0 do
if (b mod 2) = 0
then begin
b := b div 2;
A := (A * A) mod n;
end
else begin
b := b - 1;
c := (A * c) mod n;
end;
while (c < MinValue) or (c > MaxValue) do c := GetInteger(c);
Result := c;
finally
LeaveCriticalSection(CSEncode);
end;
end;
function DecodeString_3des(Source, Key: string): string;
var
DesDecode: TDCP_3des;
Str: string;
begin
try
Result := '';
DesDecode := TDCP_3des.Create(nil);
DesDecode.InitStr(Key);
DesDecode.Reset;
Str := DesDecode.DecryptString(Source);
DesDecode.Reset;
Result := Str;
DesDecode.Free;
except
Result := '';
end;
end;
function EncodeString_3des(Source, Key: string): string;
var
DesEncode: TDCP_3des;
Str: string;
begin
try
Result := '';
DesEncode := TDCP_3des.Create(nil);
DesEncode.InitStr(Key);
DesEncode.Reset;
Str := DesEncode.EncryptString(Source);
DesEncode.Reset;
Result := Str;
DesEncode.Free;
except
Result := '';
end;
end;
function Encode(Src: string; var Dest: string): Boolean;
var
StringInfo: TStringInfo;
sDest: string;
begin
Result := False;
Dest := '';
FillChar(StringInfo, SizeOf(TStringInfo), 0);
StringInfo.btLength := Length(Src);
StringInfo.nUniCode := GetUniCode(Src);
FillChar(StringInfo.sString, SizeOf(StringInfo.sString), 0);
Move(Src[1], StringInfo.sString, StringInfo.btLength);
setlength(sDest, SizeOf(Byte) + SizeOf(Integer) + StringInfo.btLength);
Move(StringInfo, sDest[1], SizeOf(Byte) + SizeOf(Integer) + StringInfo.btLength);
Dest := ReverseStr(EncryStrHex(sDest, IntToStr(240621028)));
Result := True;
end;
function Decode(Src: string; var Dest: string): Boolean;
var
StringInfo: TStringInfo;
sDest: string;
sSrc: string;
begin
Result := False;
Dest := '';
sDest := ReverseStr(Trim(Src));
try
sDest := DecryStrHex(sDest, IntToStr(240621028));
except
Exit;
end;
FillChar(StringInfo, SizeOf(TStringInfo), 0);
Move(sDest[1], StringInfo, Length(sDest));
sSrc := StrPas(@StringInfo.sString);
if (GetUniCode(sSrc) = StringInfo.nUniCode) and (Length(sSrc) = StringInfo.btLength) then begin
Dest := sSrc;
Result := True;
end;
end;
function DecryptString(Src: string): string;
begin
Result := ReverseStr(Base64DecodeStr(Src));
end;
function EncryptString(Src: string): string;
begin
Result := Base64EncodeStr(ReverseStr(Src));
end;
function EncryptBuffer(Buf: PChar; bufsize: Integer): string;
var
Src: string;
begin
setlength(Src, bufsize + 1);
Move(Buf^, Src[1], bufsize + 1);
Result := EncryptString(Src);
end;
procedure DecryptBuffer(Src: string; Buf: PChar; bufsize: Integer);
var
Dest: string;
begin
Dest := DecryptString(Src);
if Dest <> '' then
Move(Dest[1], Buf^, bufsize);
end;
initialization
begin
InitializeCriticalSection(CSEncode);
end;
finalization
begin
DeleteCriticalSection(CSEncode);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -