Recent

Author Topic: pascal cipher ecb  (Read 9946 times)

Packs

  • Sr. Member
  • ****
  • Posts: 278
Re: pascal cipher ecb
« Reply #30 on: August 23, 2024, 10:08:07 am »
I want to encrypt from  angular and decrypt from free pascal and vices versa .

https://stackblitz.com/edit/angular-crypto-aes?file=src%2Fapp%2Fapp.component.ts  ( angular link )

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, DCPblowfish,
  9.   DCPcast256, DCPrijndael, Base64;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Edit1: TEdit;
  19.     Edit2: TEdit;
  20.     Edit3: TEdit;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure Button2Click(Sender: TObject);
  23.   private
  24.     function DCPDecrypt(Data: string; key: string): string;
  25.  
  26.     function ECBEncrypt(Data: string; key: string): string;
  27.   public
  28.  
  29.   end;
  30.  
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37.  
  38. {$R *.lfm}
  39.  
  40. function TForm1.DCPDecrypt(Data: string; key: string): string;
  41.   //Data - encrypted data in base64
  42. var
  43.   Cipher: TDCP_rijndael;
  44.   DataString: string;
  45.   l: integer;
  46. begin
  47.   DataString := DecodeStringBase64(Data);
  48.  
  49.   Cipher := TDCP_rijndael.Create(nil);
  50.   Cipher.MaxKeySize := 16;
  51.   Cipher.Init(key[1], Length(key) * 8, nil);
  52.   Cipher.DecryptECB(DataString[1], DataString[1]);
  53.   Cipher.Free;
  54.   //remove padding pcks7
  55.   l := Ord(DataString[Length(DataString)]);
  56.   if l in [1..16] then
  57.     SetLength(DataString, Length(DataString) - l);
  58.  
  59.   Result := DataString;
  60. end;
  61.  
  62. function TForm1.ECBEncrypt(Data: string; key: string): string;
  63. var
  64.   Cipher: TDCP_rijndael;
  65.   DataString: string;
  66.   ldata, i: integer;
  67. begin
  68.   //Add padding pcks7
  69.   ldata := 16 - (Length(Data) mod 16);
  70.   for i := 1 to ldata do
  71.     Data := Data + Chr(ldata);
  72.   DataString := Data;
  73.  
  74.   Cipher := TDCP_rijndael.Create(nil);
  75.   Cipher.MaxKeySize := 16;
  76.   Cipher.Init(key[1], Length(key) * 8, nil);
  77.   Cipher.EncryptECB(DataString[1], DataString[1]);
  78.   Cipher.Free;
  79.  
  80.   // Added by prakash
  81.   Result := EncodeStringBase64(DataString);
  82.  
  83.   //Result := DataString;
  84. end;
  85.  
  86. { TForm1 }
  87.  
  88. procedure TForm1.Button1Click(Sender: TObject);
  89. begin
  90.   ShowMessage(DCPDecrypt(Edit2.Text, Edit3.Text));
  91. end;
  92.  
  93. procedure TForm1.Button2Click(Sender: TObject);
  94. begin
  95.   Edit2.Text := ECBEncrypt(Edit1.Text, Edit3.Text);
  96. end;
  97.  
  98.  
  99.  

above code is working with free pascal encryption and decryption .

But I need Angular encryption and decrypt in free pascal .

paweld

  • Hero Member
  • *****
  • Posts: 1215
Re: pascal cipher ecb
« Reply #31 on: August 23, 2024, 11:46:50 am »
The code I provided in this post: https://forum.lazarus.freepascal.org/index.php/topic,67874.msg523743.html#msg523743
works without any problem - screenshot attached
Best regards / Pozdrawiam
paweld

Packs

  • Sr. Member
  • ****
  • Posts: 278
Re: pascal cipher ecb
« Reply #32 on: August 23, 2024, 11:59:31 am »
Thank sir .

It working . I am sorry .


Packs

  • Sr. Member
  • ****
  • Posts: 278
Re: pascal cipher ecb
« Reply #33 on: August 23, 2024, 12:16:55 pm »
@paweid

may I get CL4PEncrypt code

Packs

  • Sr. Member
  • ****
  • Posts: 278
Re: pascal cipher ecb
« Reply #34 on: August 23, 2024, 12:50:36 pm »
it is working on command line application .

it is not working on windows form application .

I have shared the code

Packs

  • Sr. Member
  • ****
  • Posts: 278
Re: pascal cipher ecb
« Reply #35 on: August 23, 2024, 12:51:31 pm »
code

paweld

  • Hero Member
  • *****
  • Posts: 1215
Re: pascal cipher ecb
« Reply #36 on: August 23, 2024, 01:41:29 pm »
not this value you enter into the decryption edit box. You need to enter “Encrypted text”.
In addition, you need to take into account that Angular adds quotation marks at the beginning and end of the text.
Best regards / Pozdrawiam
paweld

Packs

  • Sr. Member
  • ****
  • Posts: 278
Re: pascal cipher ecb
« Reply #37 on: August 23, 2024, 05:47:21 pm »
it is my mistake . I have shared one more case it is not working with json data.

paweld

  • Hero Member
  • *****
  • Posts: 1215
Re: pascal cipher ecb
« Reply #38 on: August 23, 2024, 06:24:05 pm »
@Prakash: You are comparing quite different strings of characters, you can recognize this by the beginning of the text.
And those question marks and "bars" indicate that the problem is character encoding.   
   
Until you give specific examples, it will be difficult to deduce anything
Best regards / Pozdrawiam
paweld

Packs

  • Sr. Member
  • ****
  • Posts: 278
Re: pascal cipher ecb
« Reply #39 on: August 23, 2024, 06:48:31 pm »
@paweid

We have angular application which collect data from the form elements and encrypt the data and send to server .

we have backend application which is written in golang. which is working perfectly .

but in some case we want to use lazarus ( rest api ) . which should read the token and encrypted json data.

   

paweld

  • Hero Member
  • *****
  • Posts: 1215
Re: pascal cipher ecb
« Reply #40 on: August 23, 2024, 07:56:32 pm »
That's what I understood from the previous posts.
I mean example data (in text form) that is not decrypted in Lazarus correctly, e.g..
Code: [Select]
sample 1 

Encryted: fhtR5FtE+==
Key: mypassword123456
Decrypted: some text 

------- 

Sample 2 

Encrypted: d6rT8Art=
Key: 1234567890qwerty
Decrypted: {"field1": "value1", "field2": 2}

Etc.
« Last Edit: August 23, 2024, 07:59:27 pm by paweld »
Best regards / Pozdrawiam
paweld

Packs

  • Sr. Member
  • ****
  • Posts: 278
Re: pascal cipher ecb
« Reply #41 on: August 23, 2024, 08:14:39 pm »
Cipher Key : 467bd06c266d46bf
encrypted : UadR/z7wnYgTiz8JjT5l+JHDFgAX1FB72IycgBvpcUgl2lDBq3EjEy8Dj+W5VzMBk0Wtas7+zekVowXw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0IT3Lwc8atK0f3ETDpu0UUBB51CM09wtPkkpZqC06hi7N+/NV0ACHBzQrJ0UtjBjSk8aZnFF3P4rJOBb0fJMDK6wwy+5Ld3clWAjySbBh7rTHMZfnf08zEn/jpoyyxWk4uCZynbVW4dd6kebWpKUL1dgoxutV3pQcLjM1N3aRCSimGfCk8L2fd2jOnRA8y0+ytN+yKDWE75jgMGHlnYtEWEKC4T2KchJpl3+Zd6repLF

now check

out put should be json data : {
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915,
   "emp_code": 1001,
   "Finyear": 2024,
   "empno": 482577036915
}

paweld

  • Hero Member
  • *****
  • Posts: 1215
Re: pascal cipher ecb
« Reply #42 on: August 24, 2024, 11:15:03 am »
actually DCPcrypt has a decryption problem, but using CryptoLib4Pascal works flawlessly - example attached
Best regards / Pozdrawiam
paweld

cdbc

  • Hero Member
  • *****
  • Posts: 1514
    • http://www.cdbc.dk
Re: pascal cipher ecb
« Reply #43 on: August 24, 2024, 11:36:56 am »
Hi
@paweld: Excuse me for asking, but what might that problem be?!? I use DCPCrypt, _any_ problems would be nice to know about...
Btw. To me, the above results look exactly the same  %)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

paweld

  • Hero Member
  • *****
  • Posts: 1215
Re: pascal cipher ecb
« Reply #44 on: August 24, 2024, 12:05:23 pm »
@cdbc: at the moment I don't know what the cause is, maybe I did the wrong decryption function using DCPcrypt (AES ECB), but decrypting the text from this post: https://forum.lazarus.freepascal.org/index.php/topic,67874.msg527629.html#msg527629 returns an incorrect result.For now I don't have time to check it, but when I determine it I will come back with feedback.
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018