Recent

Author Topic: pascal cipher ecb  (Read 10451 times)

Packs

  • Sr. Member
  • ****
  • Posts: 366
Re: pascal cipher ecb
« Reply #15 on: July 16, 2024, 08:46:45 am »
@Thaddy

I have tried all options
  Cipher := TDCP_rijndael.Create(nil);
  Cipher.Init(Key, Sizeof(Key) * 8, nil);
  Cipher.EncryptECB(aString, sValue);
  Cipher.Free;

it is having EncryptECB but it is not working .

Thaddy

  • Hero Member
  • *****
  • Posts: 16156
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #16 on: July 16, 2024, 09:14:11 am »
No, that will never work for the reasons I mentioned: you have to give me the detailed information which type of encryption ecb uses, which type of padding it uses, and if the Evp key exchange is expected the key exchange algorithm too.
ecb on its own does nothing.
Read my previous post. You simply do not give inough information. Better ask the person that wrote the external code for the specifications of the ecb that is used.
If I smell bad code it usually is bad code and that includes my own code.

paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: pascal cipher ecb
« Reply #17 on: July 16, 2024, 11:36:25 am »
Works for both DCPCrypt and CryptoLib4Pascal (https://github.com/Xor-el/CryptoLib4Pascal)
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes, SysUtils, Base64,
  7.   DCPcrypt2, DCPrijndael, //dcp
  8.   ClpCipherUtilities, ClpKeyParameter, ClpIKeyParameter, ClpIBufferedCipher, ClpParameterUtilities; //cryptolib
  9.  
  10.   function DCPDecrypt(Data: String; key: String): String; //Data - encrypted data in base64
  11.   var
  12.     Cipher: TDCP_rijndael;
  13.     DataString: String;
  14.     l: Integer;
  15.   begin
  16.     DataString := DecodeStringBase64(Data);
  17.  
  18.     Cipher := TDCP_rijndael.Create(nil);
  19.     Cipher.Init(key[1], Length(key) * 8, nil);
  20.     Cipher.DecryptECB(DataString[1], DataString[1]);
  21.     Cipher.Free;
  22.     //remove padding pcks7
  23.     l := Ord(DataString[Length(DataString)]);
  24.     if l in [1..16] then
  25.       SetLength(DataString, Length(DataString) - l);
  26.  
  27.     Result := DataString;
  28.   end;
  29.  
  30.   function CL4PDecrypt(Data: String; key: String): String; //Data - encrypted data in base64
  31.   var
  32.     KeyBytes, Buf, InBuf: TBytes;
  33.     KeyParameter: IKeyParameter;
  34.     cipher: IBufferedCipher;
  35.     LBlockSize, LBufStart, Count: Integer;
  36.     DataString: String;
  37.   begin
  38.     DataString := DecodeStringBase64(Data);
  39.  
  40.     SetLength(KeyBytes, Length(key));
  41.     Move(key[1], KeyBytes[0], Length(key));
  42.     SetLength(InBuf, Length(DataString));
  43.     Move(DataString[1], InBuf[0], Length(DataString));
  44.     cipher := TCipherUtilities.GetCipher('AES/ECB/PKCS7');
  45.     KeyParameter := TKeyParameter.Create(KeyBytes);
  46.     cipher.Init(False, KeyParameter);
  47.     LBlockSize := cipher.GetBlockSize;
  48.     SetLength(Buf, Length(InBuf) + LBlockSize);
  49.     LBufStart := 0;
  50.     Count := cipher.ProcessBytes(InBuf, 0, Length(DataString), Buf, LBufStart);
  51.     Inc(LBufStart, Count);
  52.     Count := cipher.DoFinal(Buf, LBufStart);
  53.     Inc(LBufStart, Count);
  54.     SetLength(Buf, LBufStart);
  55.     SetLength(DataString, LBufStart);
  56.     Move(Buf[0], DataString[1], LBufStart);
  57.     Result := DataString;
  58.   end;
  59.  
  60. begin
  61.   WriteLn(DCPDecrypt('w31qyyrgYIytlJYh7q/SHQ==', 'qwertyuiop123456'));
  62.   WriteLn(CL4PDecrypt('w31qyyrgYIytlJYh7q/SHQ==', 'qwertyuiop123456'));
  63.   readln;
  64. end.
  65.          
  66.  
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 16156
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #18 on: July 16, 2024, 02:33:50 pm »
The Evp_  key exchange part is missing, but otherwise looks ok.
(without the key exchange, ecb is highly insecure, even with the steps you implemented.
 I don't believe that his third party code would not have implemented that )
That does not mean that your code indeed works as I concluded out that half baked code snippet in c/pascal he gave us  ;D I just think it is not enough.)
« Last Edit: July 16, 2024, 02:45:51 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Packs

  • Sr. Member
  • ****
  • Posts: 366
Re: pascal cipher ecb
« Reply #19 on: July 17, 2024, 09:47:38 am »
Code: Pascal  [Select][+][-]
  1. function ECBEncrypt(Data: string; key: string): string; //Data - encrypted data in base64
  2. var
  3.   Cipher: TDCP_rijndael;
  4.   DataString: string;
  5.   l: integer;
  6. begin
  7.   DataString := EncodeStringBase64(Data);
  8.  
  9.   Cipher := TDCP_rijndael.Create(nil);
  10.   Cipher.Init(key[1], Length(key) * 8, nil);
  11.   Cipher.EncryptECB(DataString[1], DataString[1]);
  12.   Cipher.Free;
  13.  
  14.   //remove padding pcks7
  15.   l := Ord(DataString[Length(DataString)]);
  16.   if l in [1..16] then
  17.     SetLength(DataString, Length(DataString) - l);
  18.  
  19.   Result := DataString;
  20. end;
  21.  
  22.  

I have created ECBEncrypt from your code . please check this .

if  you have encrypted code please share that to me

Packs

  • Sr. Member
  • ****
  • Posts: 366
Re: pascal cipher ecb
« Reply #20 on: July 17, 2024, 09:50:33 am »
I have checked the following class . it don't have any property or method to specify the padding type "Pkcs7"

Code: Pascal  [Select][+][-]
  1. TDCP_rijndael= class(TDCP_blockcipher128)
  2.   protected
  3.     numrounds: longword;
  4.     rk, drk: array[0..MAXROUNDS,0..7] of DWord;
  5.     procedure InitKey(const Key; Size: longword); override;
  6.   public
  7.     class function GetID: integer; override;
  8.     class function GetAlgorithm: string; override;
  9.     class function GetMaxKeySize: integer; override;
  10.     class function SelfTest: boolean; override;
  11.     procedure Burn; override;
  12.     procedure EncryptECB(const InData; var OutData); override;
  13.     procedure DecryptECB(const InData; var OutData); override;
  14.   end;
  15.  


Thaddy

  • Hero Member
  • *****
  • Posts: 16156
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #21 on: July 17, 2024, 10:09:15 am »
These things are separate from the encryption. first check if the code from paweld works in either of his excellent examples. (and I have tested them, they are in any way conformant to all standards except EVP.)
Note my remarks: if the ecb code is encoded with Evp it still will NOT work. The padding is the least of your problems: you need to give us all specs about how the third party code is encoded: not the source but all 4 or 5 protocols involved.
You have to understand that all of the protocols serve a different purpose.
You really have to ask, because ecb on its own is not encryption, but a protocol on how to handle blocks.
If paweld's code does not work we need the Evp details, since that can also be encrypted in many ways, like dh, ecdc and more. Give us the exact specs. Ask the third party and we can do that.
Now there is too much cloud in this discussion and everybody is trying to help.
You do not know anything yet about what is involved in secure protocols, do you?
Ask at the source what protocol chain it is... plz...
I hope you understand ecb is not encryption on its own.
ecb, which ecb? aes or des?
aes, which aes? 128 or 256 or....

the padding we know, that is pkcs7

But:
if evp is used, which algorithm?

If all that is known you will get a working answer.

You do not seem to get the urgency that we need to know all that, since you are working with a third party, interface code from others, and the others know.

Note that except for the key exchange protocol, paweld's code is fully standards compliant for aes-ecb, so if that does NOT work, we need the bitness for aes and the protocol for EVP.
EVP uses usually one of the elliptic curve protocols, but which one?

« Last Edit: July 17, 2024, 10:34:51 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Packs

  • Sr. Member
  • ****
  • Posts: 366
Re: pascal cipher ecb
« Reply #22 on: July 17, 2024, 01:20:55 pm »
I understand .

My problem is third party company written decrypt in golang . which I have already shred the code .

I have to encrypt and send from lazarus pascal .

We have tried with typescript it is working .

I am not getting any hint in pascal .


paweld

  • Hero Member
  • *****
  • Posts: 1268
Re: pascal cipher ecb
« Reply #23 on: July 17, 2024, 08:32:39 pm »
When encrypting, padding must set before encrypt, ie:

Code: Pascal  [Select][+][-]
  1. function ECBEncrypt(Data: string; key: string): string;
  2. var
  3.   Cipher: TDCP_rijndael;
  4.   DataString: string;
  5.   l, i: Integer;
  6. begin
  7.   //Add padding pcks7
  8.   l := 16 - (Length(Data) mod 16);
  9.   for i := 1 to l do
  10.     Data := Data + Chr(l);
  11.   DataString := Data;
  12.  
  13.   Cipher := TDCP_rijndael.Create(nil);
  14.   Cipher.Init(key[1], Length(key) * 8, nil);
  15.   Cipher.EncryptECB(DataString[1], DataString[1]);
  16.   Cipher.Free;
  17.  
  18.   Result := DataString;
  19. end;  
More samples: https://forum.lazarus.freepascal.org/index.php/topic,67865.msg523358.html#msg523358
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 16156
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #24 on: July 18, 2024, 07:17:29 am »
Prakash,
The language does not matter: protocols are strictly defined and language agnostic.
It is about the protocol chain, if we don't know the correct protocol chain, we can'thelp you.
The order is:
1. the key exchange protocol, likely dh, this is asymmetric encryption
2. the encryption protocol itself, in the case of ecb likely aes or des, symmetric
3. the bitness of that: likely 128 or 256, but can be higher
4. the padding method

The examples from paweld take care of 2,3 and 4 and assumes 2/3. to be aes128 and 4. to be pkcs7. (which we derived from your code snippet)
We need to know 1. and we need to know the exact 3. Otherwise there is no way to help you any further. As I already wrote Pawel's code is 100% standards compliant, but does not implement 1. and assumes aes128.
We really need to know the things we asked, so get that information.We also need to know if the block protocol is really ecb and not cbc. (the latter makes more sense)
You can't write proper code without knowing the exact! protocol chain.

exact, exact,exact,exact. Clear?

And the programming language really has nothing to do with your issue, it may as well be written in BrainFuck, we need the exact protocols.
If we have that, your problem would have been solved in a day instead of almost a week. Get that information, only then you will make progress.
Both Pawel and I spend quite some time to help you and we have put you in the right direction, but you give us incomplete information.
Now , please make sure you ask for the protocols in detail.
You already forced us to make assumptions and in the case of encrypted data that is never a good idea. You can not make assumptions. It is the detail.
If the third-party gave you interface code, plz give that as complete as possible (and not half pascalized). Both Pawel and I are proficient in many programming languages. Good enough to see what we need to do to make it work in Pascal.
« Last Edit: July 18, 2024, 07:55:36 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Packs

  • Sr. Member
  • ****
  • Posts: 366
Re: pascal cipher ecb
« Reply #25 on: July 18, 2024, 11:26:59 am »
Thaddy,

I would like  to implement same javascript code in pascal .

below javascript code . This is working for me

Code: Pascal  [Select][+][-]
  1. export function Ecbencrypt(request: any): string {
  2.   let _key = CryptoJS.enc.Utf8.parse(encrypt_key);
  3.   let _iv = CryptoJS.enc.Utf8.parse(encrypt_key);
  4.  
  5.   let encrypted = CryptoJS.AES.encrypt(
  6.     JSON.stringify(request), _iv, {
  7.         keySize: 16,
  8.         iv: _iv,
  9.         mode: CryptoJS.mode.ECB,
  10.         padding: CryptoJS.pad.Pkcs7
  11.     });
  12.   return encrypted.toString();
  13. }
  14.  

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: pascal cipher ecb
« Reply #26 on: July 18, 2024, 11:56:00 am »
@Prakash:
Out of curiosity how many more examples do you require that people write for you.

Obviously you seem to miss the point regarding Thaddy's posts since you seem to keep insisting for us to replicate some code from some language that has its own interpretation of how encryption should work.

How other languages work with regards to encryption is their problem and examples how you could approach this in/with FPC have been provided. If those example do not work for you then you need to figure out how that other language implements its version of encryption and replicate that in/with Pascal. stringifying json strings, unrelated string encodings, dumb padding and what not more is not part of the encoding itself.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

Thaddy

  • Hero Member
  • *****
  • Posts: 16156
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #27 on: July 18, 2024, 12:44:29 pm »
After a bit of research for you:
https://stackoverflow.com/questions/64797987/what-is-the-default-aes-config-in-crypto-js

So simply change Pawel's code to 256 and it will work.
If not, report back.
If it works, give Pawel a compliment.
(note that is still insecure without a secure key exchange)
« Last Edit: July 18, 2024, 12:52:08 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Packs

  • Sr. Member
  • ****
  • Posts: 366
Re: pascal cipher ecb
« Reply #28 on: July 19, 2024, 07:38:06 am »
Code: Pascal  [Select][+][-]
  1. function ECBEncrypt(Data: string; key: string): string;
  2. var
  3.   Cipher: TDCP_rijndael;
  4.   DataString: string;
  5.   l, i: Integer;
  6. begin
  7.   //Add padding pcks7
  8.   l := 16 - (Length(Data) mod 16);
  9.   for i := 1 to l do
  10.     Data := Data + Chr(l);
  11.   DataString := Data;
  12.  
  13.   Cipher := TDCP_rijndael.Create(nil);
  14.   Cipher.Init(key[1], Length(key) * 8, nil);
  15.   Cipher.EncryptECB(DataString[1], DataString[1]);
  16.   Cipher.Free;
  17.  
  18.   Result := DataString;
  19. end;  
  20.  

this is powel code . where I have to change . I am not pascal expert .
Please guide me

Thaddy

  • Hero Member
  • *****
  • Posts: 16156
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #29 on: July 19, 2024, 09:49:20 am »
Gimme the specs! the protocols. Otherwise we are going nowhere. Plz?
Read everything what Pawel and I wrote again.
Now we rely on deduction, not factual information.
Let's see where we are now:
- padding is pkcs7, done
- aes is aes256, covered(you should have told us that immidiately)
- you keep insisting it is ecb, unlikely
- we do not know the key exchange.
I have tested your code in Javascript - it does work - and if that works there should be no problem.
You already got that code, because Pawel wrote it. Show us where it goes wrong.
I suspect the error is in your pascal code, but we haven't seen that.
     now, and this is important, finally give us every little detail.
« Last Edit: July 19, 2024, 10:11:13 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018