BSOne.SFC/eCrmHE/EXE_eCrmHomeEdition/CrmLogger.pas

160 lines
4.0 KiB
Plaintext

{*******************************************************}
{ }
{ CrmLogger }
{ }
{ Copyright (C) 2022 kku }
{ }
{*******************************************************}
unit CrmLogger;
interface
uses
System.SysUtils, System.Classes, Winapi.Windows;
function EncryptStrToBase64(sStr: AnsiString; sKey: UTF8String = ''): AnsiString;
function EncryptStrToBinStr(sStr: AnsiString; sKey: UTF8String = ''): AnsiString;
function DecryptStr(sEncStr: String; sKey: UTF8String = ''): AnsiString;
implementation
uses
Tocsg.Hash, Tocsg.Safe, Soap.EncdDecd, aes_ecb, aes_type, Tocsg.Hex;
const
KEY_STR = 'Z8LSq0wWwB5v+6YJzurcP463H3F12iZh74fDj4S74oUH4EONkiKb2FmiWUbtFh97' +
'GG/c/lbDE47mvw6j94yXxKHOpoqu6zpLKMKPcOoSppcVWb2q34qENBJkudXUh4MW' +
'creondLmLL2UyydtFKuU9Sa5VgY/CzGaVGJABK2ZR94=';
function GenKey(sKey: UTF8String = ''): TBytes;
begin
if sKey = '' then
sKey := KEY_STR;
SetLength(Result, 16);
CopyMemory(Result, @ConvStrToSha1A_Bin(UTF8String(sKey))[0], 16);
end;
function EncryptStrToBase64(sStr: AnsiString; sKey: UTF8String = ''): AnsiString;
var
pBuf, pEncBuf: TBytes;
nLen, nEncLen: Integer;
ACtx: TAESContext;
begin
Result := '';
if sStr = '' then
exit;
if sKey = '' then
sKey := KEY_STR;
nLen := Length(sStr);
ZeroMemory(@ACtx, SizeOf(ACtx));
if AES_ECB_Init_Encr(GenKey(sKey)[0], 128, ACtx) <> 0 then
begin
exit;
end;
if (nLen mod 16) <> 0 then
nEncLen := nLen - (nLen mod 16) + 16
else
nEncLen := nLen;
SetLength(pBuf, nEncLen);
// ZeroMemory(pBuf, nEncLen);
FillMemory(pBuf, nEncLen, 13);
SetLength(pEncBuf, nEncLen);
ZeroMemory(pEncBuf, nEncLen);
CopyMemory(pBuf, @sStr[1], nLen);
if AES_ECB_Encrypt(@pBuf[0], @pEncBuf[0], nEncLen, ACtx) <> 0 then
begin
exit;
end;
Result := EncodeBase64(pEncBuf, nEncLen);
// Result := DecryptStr(Result);
end;
function EncryptStrToBinStr(sStr: AnsiString; sKey: UTF8String = ''): AnsiString;
var
pBuf, pEncBuf: TBytes;
nLen, nEncLen: Integer;
ACtx: TAESContext;
begin
Result := '';
if sStr = '' then
exit;
if sKey = '' then
sKey := KEY_STR;
nLen := Length(sStr);
ZeroMemory(@ACtx, SizeOf(ACtx));
if AES_ECB_Init_Encr(GenKey(sKey)[0], 128, ACtx) <> 0 then
begin
exit;
end;
if (nLen mod 16) <> 0 then
nEncLen := nLen - (nLen mod 16) + 16
else
nEncLen := nLen;
SetLength(pBuf, nEncLen);
// ZeroMemory(pBuf, nEncLen);
FillMemory(pBuf, nEncLen, 13);
SetLength(pEncBuf, nEncLen);
ZeroMemory(pEncBuf, nEncLen);
CopyMemory(pBuf, @sStr[1], nLen);
if AES_ECB_Encrypt(@pBuf[0], @pEncBuf[0], nEncLen, ACtx) <> 0 then
begin
exit;
end;
Result := ConvBinToStr(PAnsiChar(pEncBuf), nEncLen);
// Result := DecryptStr(Result);
end;
function DecryptStr(sEncStr: String; sKey: UTF8String = ''): AnsiString;
var
pSrcBuf, pDecBuf: TBytes;
nLen: Integer;
ACtx: TAESContext;
begin
Result := '';
if sEncStr = '' then
exit;
pSrcBuf := DecodeBase64(sEncStr);
nLen := Length(pSrcBuf);
ZeroMemory(@ACtx, SizeOf(ACtx));
if AES_ECB_Init_Decr(GenKey(sKey)[0], 128, ACtx) <> 0 then
begin
exit;
end;
SetLength(pDecBuf, nLen);
if AES_ECB_Decrypt(@pSrcBuf[0], @pDecBuf[0], nLen, ACtx) <> 0 then
begin
exit;
end;
SetLength(Result, nLen);
ZeroMemory(@Result[1], nLen);
CopyMemory(@Result[1], @pDecBuf[0], nLen);
// 음.. 끝에 ASCII - 3 3 3 이렇게 찍히는데 이유는 딱히 모름
// decryptedValue = decryptedValue.replace("\r", " ");
// decryptedValue = decryptedValue.replace(" ", System.getProperty("line.separator"));
// 이렇게 되어 있는데... 대충 따라해봄
Result := StringReplace(Result, #3, ' ', [rfReplaceAll]);
Result := Trim(StringReplace(Result, ' ', #13#10, [rfReplaceAll]));
end;
end.