Recent

Author Topic: pascal cipher ecb  (Read 10497 times)

Packs

  • Sr. Member
  • ****
  • Posts: 368
pascal cipher ecb
« on: July 13, 2024, 10:57:40 am »
I would like to decrypt string by using cipher ecb.

I am getting encrypted data from other application .

please guide me

cdbc

  • Hero Member
  • *****
  • Posts: 1655
    • http://www.cdbc.dk
Re: pascal cipher ecb
« Reply #1 on: July 13, 2024, 11:17:38 am »
Hi
You can use CryptoLib4Pascal: https://github.com/Xor-el/CryptoLib4Pascal
or look for DCPCrypt for fpc/lazarus
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

Packs

  • Sr. Member
  • ****
  • Posts: 368
Re: pascal cipher ecb
« Reply #2 on: July 14, 2024, 08:56:49 am »
no it don't have ecb option

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #3 on: July 14, 2024, 09:01:03 am »
Yes, it has. It also supports multiple ecb derivatives.
Nowadays ecb is very seldom used on its own, it is usually used in combination with other cyphers, like aes-ecb. All te important ones are implemented by Xor-El's library.
OpenSSL also supports it.
« Last Edit: July 14, 2024, 09:08:23 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Packs

  • Sr. Member
  • ****
  • Posts: 368
Re: pascal cipher ecb
« Reply #4 on: July 14, 2024, 02:27:52 pm »
can you share the library name

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #5 on: July 14, 2024, 02:42:08 pm »
can you share the library name
Benny already did that:
https://github.com/Xor-el/CryptoLib4Pascal

But I suspect you do not yet know what you are doing and your information is possibly incomplete.

If you want to use openSSL instead, go to openssl.org for complete documentation.

For an example I need to know how ecb is used.
Common is aes-ecb or des-ecb (deprecated), so tell us more. Only then can I give you an example.
« Last Edit: July 14, 2024, 02:47:31 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Packs

  • Sr. Member
  • ****
  • Posts: 368
Re: pascal cipher ecb
« Reply #6 on: July 14, 2024, 03:01:49 pm »
Code: Pascal  [Select][+][-]
  1.  
  2. func Decrypt_ecb(ct, key []byte) ([]byte,error) {
  3.     var(
  4.        empty []byte
  5.     )
  6.     block, err := aes.NewCipher(key)
  7.     if err != nil {
  8.        return empty,err
  9.     }
  10.  
  11.     mode := ecb.NewECBDecrypter(block)
  12.     pt := make([]byte, len(ct))
  13.     mode.CryptBlocks(pt, ct)
  14.  
  15.     padder := padding.NewPkcs7Padding(mode.BlockSize())
  16.     pt, err = padder.Unpad(pt) // unpad plaintext after decryption
  17.     if err != nil {
  18.        return empty,err
  19.     }
  20.  
  21.     return pt,nil
  22. }
  23. [code=pascal][font=Lucida Console][size=3pt][color=rgb(0, 0, 0)][font=Verdana][size=2px]this is the code is written in golang which decrypt the data. [/color][/font][/size][/font][/size][font=Lucida Console][size=3pt][color=rgb(0, 0, 0)][font=Verdana][size=2px]I have to create encryption in lazarus pascal code and pass to golang for decrypt .[/color][/font][/size][/font][/size][font=Lucida Console][size=3pt][color=rgb(0, 0, 0)][font=Verdana][size=2px]
  24. [/color][/font][/size][/font][/size][font=Lucida Console][size=3pt][color=rgb(0, 0, 0)][font=Verdana][size=2px] [/color][/font][/size][/font][/size][font=Lucida Console][size=3pt][color=rgb(0, 0, 0)][font=Verdana][size=2px]
  25. [/color][/font][/size][/font][/size]

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #7 on: July 14, 2024, 03:12:03 pm »
Ok that is what I wanted to see. The code is aes-ecb. You left that important bit out.
Not today, but that is not to difficult to write an example for. I will assume 128 bit aes.
The other important part you left out is pkcs7 padding..

Without that essential info it was never going to work.
« Last Edit: July 14, 2024, 03:24:18 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Packs

  • Sr. Member
  • ****
  • Posts: 368
Re: pascal cipher ecb
« Reply #8 on: July 14, 2024, 03:44:32 pm »
@Thaddy Please guide me . how to solve this issue

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #9 on: July 14, 2024, 05:45:04 pm »
I will solve it but not today, but tomorrow. Today two big sporting events to watch.
If I smell bad code it usually is bad code and that includes my own code.

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: pascal cipher ecb
« Reply #10 on: July 15, 2024, 07:31:26 am »
There is a Rijndael example for DCPCrypt on the wiki
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

cdbc

  • Hero Member
  • *****
  • Posts: 1655
    • http://www.cdbc.dk
Re: pascal cipher ecb
« Reply #11 on: July 15, 2024, 07:53:49 am »
Hi
One way, to go about this, would be to use DCPCrypt:
Code: Pascal  [Select][+][-]
  1. uses base64, DCPrijndael, DCPsha256;
  2. ....
  3. function bcDecryptStrAES(const aKeyStr, aStr: string; out aDec: string;out aLen: ptrint): boolean;
  4. var
  5.   dc: TDCP_rijndael; // cipher
  6.   ds,ss: TStringStream;
  7. begin
  8.   Result:= false; aDec:= ''; aLen:= 0; // initialize result vars
  9.   dc:= TDCP_rijndael.Create(nil);      // create our cipher, rijndael = aes
  10.   dc.InitStr(aKeyStr,TDCP_sha256);     // init cipher with our keystr & 256bit hash
  11.   ss:= TStringStream.Create(aStr);     // create our source stream
  12.   ds:= TStringStream.Create;           // create our decrypted stream
  13.   dc.DecryptStream(ss,ds,ss.Size);     // now decrypt our data
  14.   aDec:= ds.DataString;                // return the resulting string in out param
  15.   aLen:= ds.Size;                      // we don't know what's in the stream, raw not utf8
  16.   dc.Burn;                             // now completeley destroy the cipher
  17.   dc.Free;                             // we're done with cipher
  18.   ds.Free; ss.Free;                    // done with streams too
  19.   Result:= aLen > 0;                   // do we have anything to show for our efforts?
  20. end;
  21.  
  22. function bcDecryptB64ToStr(const aKeyStr, aB64Str: string): string;
  23. var rlen: ptrint = 0;
  24. begin
  25.   if ((aKeyStr = '') or (aB64Str = '')) then exit('');
  26.   if not bcDecryptStrAES(aKeyStr,DecodeStringBase64(aB64Str),Result,rlen) then Result:= '';
  27. end;
HTH
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

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #12 on: July 15, 2024, 11:19:26 am »
Benny, from his code snippet I interpret it as aes128-ecb with pkcs7 padding (16 byte padding )
But indeed all this can probably done in dcpcrypt too.
If I smell bad code it usually is bad code and that includes my own code.

cdbc

  • Hero Member
  • *****
  • Posts: 1655
    • http://www.cdbc.dk
Re: pascal cipher ecb
« Reply #13 on: July 15, 2024, 12:33:51 pm »
Hi Thaddy
Quote
But indeed all this can probably done in dcpcrypt too.
I just copy/pasted something I had lying around...
...good enough for /spoon-feeding/  :D
There's a wealth of possibilities in the DCPCrypt code as well as in Xor-El's,
that's what I wanted to show him, if he only reaches out and grabs it.
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

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
Re: pascal cipher ecb
« Reply #14 on: July 15, 2024, 03:17:52 pm »
I tried some other simple example for OpenSSL and that only works if I compile OpenSSL from source with the decrecated flag ON.(there are no longer binary distributions for it )
So unless he also uses EVP (so evp-aes-ecb) I an out of idea's unless he really needs the deprecated code. But otherwise it would be presenting an example of highly insecure code.
Can be done, though, but I then still need more information. Do you really want/need insecure code? Or was it cbc after all and not ecb? pkcs7 for padding suggests you also need evp because they are always used in combination.

I suspect Prakash also left out the evp part? Which makes it again a different story.
Can you confirm? And are you sure it is ecb, because that is the insecure part without evp.

whould this be what you want?
const EVP_CIPHER *EVP_aes_192_ecb(void); // aes192!!!
+ the pkcs7 padding?

That looks a lot more logical to me. The above is from:
https://wiki.openssl.org/index.php/EVP

- ecb on its own is not encryption, it defines how given a certain encryption like des or aes the encryption blocks should be handled.
- evp takes care of the secure key exchange and on its own may use something like DH
- pkcs takes care of the salt (the padding up to ecb block size and depadding on the original decrypted data, simply put)

If all these details, all important, none left out, are known to me I can finally write an example.

« Last Edit: July 15, 2024, 03:47:53 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018