program project_en;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes, SysUtils, CustApp,
StrUtils,
//IdMultipartFormData,
//IdGlobal, IdCoderMIME, idCoder,
//IdSSLOpenSSL, IdGlobalProtocols, LazUTF8Classes,
//DCPrijndael, Base64,
DCPsha1, DCPsha256, DCPcrypt2,
DCPrijndael, DCPbase64, base64,
DCPconst, ClpEncoders,
ClpCipherUtilities, ClpParametersWithIV, ClpIParametersWithIV,
ClpIBufferedCipher, ClpParameterUtilities,
OpenSSL
{ you can add units after this };
// encrypt function 1
function my_encrypt1(Data: string; key, IV: string): string;
var
Cipher: TDCP_rijndael;
DataString, encryption_key: ansistring;
l, i: Integer;
begin
l := 16 - (Length(Data) mod 16);
for i := 1 to l do
Data := Data + Chr(l);
DataString := Data;
encryption_key := key;
if (Length(encryption_key) < 32) then
while Length(encryption_key) < 32 do encryption_key := encryption_key + ansichar(0);
encryption_key := Copy(encryption_key, 1, 32);
Cipher := TDCP_rijndael.Create(nil);
Cipher.CipherMode := cmCBC;
Cipher.Init(encryption_key[1], Length(encryption_key) * 8, @IV[1]);
Cipher.EncryptCBC(DataString[1], DataString[1], Length(DataString));
Result := EncodeStringBase64(DataString);
Cipher.Free;
end;
// encrypt function 2
function my_encrypt2(Data: String; key, IV: String): String;
var
KeyBytes, IVBytes, Buf, InBuf: TBytes;
KeyParametersWithIV: IParametersWithIV;
cipher: IBufferedCipher;
LBlockSize, LBufStart, Count: Integer;
DataString, encryption_key: Ansistring;
begin
encryption_key := key;
if (Length(encryption_key) < 32) then
while Length(encryption_key) < 32 do encryption_key := encryption_key + Ansichar(0);
encryption_key := Copy(encryption_key, 1, 32);
DataString := Data;
SetLength(KeyBytes, Length(encryption_key));
Move(encryption_key[1], KeyBytes[0], Length(encryption_key));
SetLength(IVBytes, Length(IV));
Move(IV[1], IVBytes[0], Length(IV));
SetLength(InBuf, Length(DataString));
Move(DataString[1], InBuf[0], Length(DataString));
cipher := TCipherUtilities.GetCipher('RIJNDAEL/CBC/PKCS7');
KeyParametersWithIV := TParametersWithIV.Create(TParameterUtilities.CreateKeyParameter('AES', KeyBytes), IVBytes);
cipher.Init(True, KeyParametersWithIV);
LBlockSize := cipher.GetBlockSize;
SetLength(Buf, Length(InBuf) + LBlockSize);
LBufStart := 0;
Count := cipher.ProcessBytes(InBuf, 0, Length(DataString), Buf, LBufStart);
Inc(LBufStart, Count);
Count := cipher.DoFinal(Buf, LBufStart);
Inc(LBufStart, Count);
SetLength(Buf, LBufStart);
SetLength(DataString, LBufStart);
Move(Buf[0], DataString[1], LBufStart);
Result := EncodeStringBase64(DataString);
end;
var
Key : AnsiString;
IV : AnsiString;
Data : AnsiString;
CBC1, CBC2 : AnsiString;
begin
//
Data := 'Hello World';
Key := '1234567890______';
IV := '______1234567890';
CBC1 := my_encrypt1(Data, Key, IV); //EncodeUrl(my_encrypt
CBC2 := my_encrypt2(Data, Key, IV); //EncodeUrl(my_encrypt
//writeLn('加密:');
WriteLn('KEY : ', Key , ' (', Length(Key) , ')');
WriteLn('IV : ', IV , ' (', Length(IV) , ')');
WriteLn('DATA : ', Data , ' (', Length(Data), ')');
//WriteLn('CBC : ', BinStr2Hex(CBC) , ' (', Length(CBC) , ')');
//CBC := (CBC);
WriteLn('CBC1_BASE64 : ', CBC1 , ' (', Length(CBC1) , ')');
WriteLn('CBC2_BASE64 : ', CBC2 , ' (', Length(CBC2) , ')');
end.