Recent

Author Topic: decrypt rsa string  (Read 1397 times)

victor11109

  • New Member
  • *
  • Posts: 19
decrypt rsa string
« on: February 26, 2024, 06:09:50 pm »
hi.
im trying few days to decrypt an rsa pkca1 encrypted string with privatekey . but no success  :(
i used openssl unit . and also lockbox library .

currently im using this code in python

Code: Python  [Select][+][-]
  1. from Crypto.PublicKey import RSA
  2. from Crypto.Cipher import PKCS1_v1_5
  3. import base64
  4.  
  5. # Load private key
  6. private_key = RSA.import_key(open('private_key.pem').read())
  7.  
  8. # Encrypted text (assuming it's base64-encoded)
  9. encrypted_text_base64 = "some_encrypted_text"
  10.  
  11. # Decode base64
  12. ciphertext = base64.b64decode(encrypted_text_base64)
  13.  
  14. # Create a PKCS1_v1_5 cipher object
  15. cipher = PKCS1_v1_5.new(private_key)
  16.  
  17. # Decrypt the ciphertext
  18. plaintext = cipher.decrypt(ciphertext, None)
  19.  
  20. print("Decrypted plaintext:", plaintext.decode('utf-8'))
  21.  

its working fine . but wnat same functionaly in pascal .

the code that writed in lazarus is

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   Classes,
  5.   SysUtils,
  6.   openssl,
  7.   base64;
  8.  
  9.   function DecryptRSA(const EncryptedData: ansistring; const PrivateKey: ansistring): ansistring;
  10.   var
  11.     RSAKey: pRSA;
  12.     Bio: pBIO;
  13.     DecryptedLen: integer;
  14.     DecryptedData: array of byte;
  15.   begin
  16.     Bio := BIO_new_mem_buf(@PrivateKey[1], Length(PrivateKey));
  17.     RSAKey := PEM_read_bio_PrivateKey(Bio, nil, nil, nil);
  18.     SetLength(DecryptedData, RSA_size(RSAKey));
  19.     DecryptedLen := RSA_private_decrypt(Length(EncryptedData), @EncryptedData[1], @DecryptedData[0], RSAKey, RSA_PKCS1_PADDING);
  20.     Result := ansistring(Copy(ansistring(DecryptedData), 1, DecryptedLen));
  21.   end;
  22.  
  23.  
  24. var
  25.   EncryptedString, PrivateKey: ansistring;
  26.   DecryptedString: ansistring;
  27. begin
  28.   EncryptedString := DecodeStringBase64('...');
  29.   PrivateKey := '...';
  30.   DecryptedString := DecryptRSA(EncryptedString, PrivateKey);
  31.   WriteLn('Decrypted Data: ', DecryptedString);
  32. end.
  33.  

but not working .
« Last Edit: February 28, 2024, 02:01:03 am by victor11109 »

victor11109

  • New Member
  • *
  • Posts: 19
Re: decrypt rsa string
« Reply #1 on: February 27, 2024, 09:25:09 pm »
UPDATE : after spend some time finally solved by my self  %)

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   Classes,
  5.   SysUtils,
  6.   openssl,
  7.   base64;
  8.  
  9. var
  10.   rsa: PRSA;
  11.   bio: PBIO;
  12.   pkey: PEVP_PKEY;
  13.   privateKeyFile, encryptedText: string;
  14.   buf, decryptedBuf: TBytes;
  15.   len: integer;
  16. begin
  17.   privateKeyFile := 'pvkey.pem';
  18.   encryptedText := DecodeStringBase64('encrypted_text');
  19.  
  20.   bio := BIO_new_file(PChar(privateKeyFile), 'r');
  21.   pkey := PEM_read_bio_PrivateKey(bio, nil, nil, nil);
  22.   rsa := PRSA(pkey^.attributes);
  23.  
  24.   len := RSA_size(rsa);
  25.   SetLength(decryptedBuf, len);
  26.   len := RSA_private_decrypt(len, @encryptedText[1], @decryptedBuf[0], rsa, RSA_PKCS1_PADDING);
  27.  
  28.   WriteLn(StringOf(decryptedBuf));
  29.   ReadLn;
  30. end.
  31.  

but i have question in  line that get rsa key from PEVP_PKEY  " rsa := PRSA(pkey^.attributes); " - is this actual way to do this ?

TRon

  • Hero Member
  • *****
  • Posts: 3660
Re: decrypt rsa string
« Reply #2 on: February 27, 2024, 11:50:24 pm »
but i have question in  line that get rsa key from PEVP_PKEY  " rsa := PRSA(pkey^.attributes); " - is this actual way to do this ?
No it isn't (but to my knowledge, it isn't exactly wrong either).

There used to be a function named EVP_PKEY_get0_RSA (e.g. : "const RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey);"  ) but that seems to be deprecated.

My google-foo seems off today (seem to be getting normal for me these days  :'( ) because I failed to find an answer that is better suited... so in that regards your solution is as good as any ?
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

victor11109

  • New Member
  • *
  • Posts: 19
Re: decrypt rsa string
« Reply #3 on: February 28, 2024, 12:08:46 am »
but i have question in  line that get rsa key from PEVP_PKEY  " rsa := PRSA(pkey^.attributes); " - is this actual way to do this ?
No it isn't (but to my knowledge, it isn't exactly wrong either).

There used to be a function named EVP_PKEY_get0_RSA (e.g. : "const RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey);"  ) but that seems to be deprecated.

My google-foo seems off today (seem to be getting normal for me these days  :'( ) because I failed to find an answer that is better suited... so in that regards your solution is as good as any ?

actually all google and GPT solutions was like " rsa := PEM_read_bio_PrivateKey(bio, nil, nil, nil); " and all of them not work till find " pkey^.attributes " by chance  :D
its working now and its ok for me . but are you know updated openssl or any other crypto library to do this ? - synapse openssl3 was little bit Confusing for me . didnt have decrypt rsa function  at all and lockbox3 had its own formats and not workd for my keys .
and this c example did not even use " EVP_PKEY_get0_RSA "
https://hayageek.com/rsa-encryption-decryption-openssl-c/

TRon

  • Hero Member
  • *****
  • Posts: 3660
Re: decrypt rsa string
« Reply #4 on: February 28, 2024, 12:50:03 am »
Call it a coincidence but as luck would have it I was just reading that example while you posted your reply   :)

The problem with my google-foo is that all kinds of examples turn up either using the command-line or using deprecated functionality. The example you point to uses PEM_read_bio_RSAPrivateKey explicitly (and not PEM_read_bio_PrivateKey) which might account for the difference. How to properly use PEM_read_bio_PrivateKey and then obtain the rsa key is unclear to me, or it must be that when you provide the PPEVP_PKEY argument that it automatically returns the correct pointer to the rsa key ?
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

victor11109

  • New Member
  • *
  • Posts: 19
Re: decrypt rsa string
« Reply #5 on: February 28, 2024, 01:24:51 am »
Call it a coincidence but as luck would have it I was just reading that example while you posted your reply   :)

The problem with my google-foo is that all kinds of examples turn up either using the command-line or using deprecated functionality. The example you point to uses PEM_read_bio_RSAPrivateKey explicitly (and not PEM_read_bio_PrivateKey) which might account for the difference. How to properly use PEM_read_bio_PrivateKey and then obtain the rsa key is unclear to me, or it must be that when you provide the PPEVP_PKEY argument that it automatically returns the correct pointer to the rsa key ?


yes it seems to PPEVP_PKEY argument must automatically returns the correct pointer to the rsa key but it didnt.
the PPEVP_PKEY is record of

Code: Pascal  [Select][+][-]
  1.   EVP_PKEY = record
  2.     ktype: integer;
  3.     save_type: integer;
  4.     references: integer;
  5.     pkey: EVP_PKEY_PKEY;
  6.     save_parameters: integer;
  7.     attributes: PSTACK_OFX509;
  8.   end;      
  9.  

and i find function " EvpPkeyAssign(pkey: PEVP_PKEY; _type: cInt; key: Prsa): cInt; " . are this related to problem ?
some ai code generators suggested  rsa := pkey^.pkey.rsa;  but its not work . this shoud be the actual way not ?
« Last Edit: February 28, 2024, 01:40:03 am by victor11109 »

 

TinyPortal © 2005-2018