Xor-el / CryptoLib4Pascal

Crypto for Modern Object Pascal
MIT License
211 stars 65 forks source link

Decrypt function customize fail, advice to me. plz. #19

Closed combachi closed 3 years ago

combachi commented 3 years ago

Hello.

I am fail a Decrypt.

i need AES128 with fixed Key & IV. no SALT. so, i customize UsageSample's AES256CBC Encrypt & Decrypt function like bottom.

my Encrypt function work very well. i confirm it make same result as my partner's result. but, my Decrypt function always raise error. not wrong result, just can not make result. (Key & IV is 16Character like '1234567890123456')

if original PlainText length < 32 then raise EDataLengthCryptoLibException. (FbufOff <> blockSize(16) at TPaddedBufferedBlockCipher.Create) if original PlainText length >= 32 then raise EInvalidCipherTextCryptoLibException. (input array's Last is 0 or over16 at TPkcs7Padding.PadCount) (decrypt function's input is cipheredText)

what is my mistake? what needs to be fixed?

------------------------------- my source code ( i don't know how make box, sorry) ---------------------- unit XorelCrypt;

interface

uses System.SysUtils, ClpIParametersWithIV, ClpIBufferedCipher, ClpCipherUtilities, ClpParametersWithIV, ClpParameterUtilities, ClpConverters, ClpEncoders;

function XorelAESEncrypt( sPlain, sKey, sIV : string; out vCypher:string): boolean; function XorelAESDecrypt( sCipher, sKey, sIV : string; out sPlain: string): Boolean;

implementation

function XorelAESEncrypt(sPlain, sKey, sIV: string; out vCypher:string): boolean; var PlainBytes, KeyBytes, IVBytes, BufBytes: TBytes; KeyParametersWithIV: IParametersWithIV; cipher: IBufferedCipher; LBlockSize, LBufStart, Count: Int32; begin result := False; vCypher := '';

PlainBytes := TConverters.ConvertStringToBytes( sPlain, TEncoding.UTF8); KeyBytes := TConverters.ConvertStringToBytes( sKey, TEncoding.UTF8);

IVBytes := TConverters.ConvertStringToBytes( sIV, TEncoding.UTF8);

// remove Get Key & IV because it is Fixed. cipher := TCipherUtilities.GetCipher('AES/CBC/PKCS7PADDING'); KeyParametersWithIV := TParametersWithIV.Create (TParameterUtilities.CreateKeyParameter('AES', KeyBytes), IVBytes);

cipher.Init(True, KeyParametersWithIV); // init encryption cipher LBlockSize := cipher.GetBlockSize;

System.SetLength(BufBytes, System.Length(PlainBytes) + LBlockSize);

LBufStart := 0;

Count := cipher.ProcessBytes(PlainBytes, 0, System.Length(PlainBytes), BufBytes, LBufStart); System.Inc(LBufStart, Count); Count := cipher.DoFinal(BufBytes, LBufStart); System.Inc(LBufStart, Count);

System.SetLength(BufBytes, LBufStart);

vCypher := TBase64.Encode( BufBytes); result := True; end;

function XorelAESDecrypt(sCipher, sKey, sIV: string; out sPlain: string): Boolean; var CipherBytes, KeyBytes, IVBytes, BufBytes : TBytes; KeyParametersWithIV: IParametersWithIV; cipher: IBufferedCipher; LBufStart, LSrcStart, Count: Int32; begin result := False; sPlain := '';

CipherBytes := TConverters.ConvertStringToBytes( sCipher, TEncoding.UTF8); KeyBytes := TConverters.ConvertStringToBytes( sKey, TEncoding.UTF8); IVBytes := TConverters.ConvertStringToBytes( sIV, TEncoding.UTF8);

// remove Get Key & IV because it is Fixed. LSrcStart := 0;

cipher := TCipherUtilities.GetCipher('AES/CBC/PKCS7PADDING'); KeyParametersWithIV := TParametersWithIV.Create (TParameterUtilities.CreateKeyParameter('AES', KeyBytes), IVBytes);

cipher.Init(False, KeyParametersWithIV); // init decryption cipher

System.SetLength(BufBytes, System.Length(CipherBytes));

LBufStart := 0;

Count := cipher.ProcessBytes(CipherBytes, LSrcStart, System.Length(CipherBytes)

end.

Thanks & Regards,

combachi commented 3 years ago

i have solved.

i tried BASE64 encoded string for encrypted source. i change

CipherBytes := TConverters.ConvertStringToBytes( sCipher, TEncoding.UTF8); to CipherBytes := TBase64.Decode( sCipher); and it work well!

maybe you think i was impatient, I wandered for 3 days.....

anyway sorry about open 'issue'. and I will use CriptoLib4Pascal well. thank you~