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
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
import base64
# Load private key
private_key = RSA.import_key(open('private_key.pem').read())
# Encrypted text (assuming it's base64-encoded)
encrypted_text_base64 = "some_encrypted_text"
# Decode base64
ciphertext = base64.b64decode(encrypted_text_base64)
# Create a PKCS1_v1_5 cipher object
cipher = PKCS1_v1_5.new(private_key)
# Decrypt the ciphertext
plaintext = cipher.decrypt(ciphertext, None)
print("Decrypted plaintext:", plaintext.decode('utf-8'))
its working fine . but wnat same functionaly in pascal .
the code that writed in lazarus is
program project1;
uses
Classes,
SysUtils,
openssl,
base64;
function DecryptRSA(const EncryptedData: ansistring; const PrivateKey: ansistring): ansistring;
var
RSAKey: pRSA;
Bio: pBIO;
DecryptedLen: integer;
DecryptedData: array of byte;
begin
Bio := BIO_new_mem_buf(@PrivateKey[1], Length(PrivateKey));
RSAKey := PEM_read_bio_PrivateKey(Bio, nil, nil, nil);
SetLength(DecryptedData, RSA_size(RSAKey));
DecryptedLen := RSA_private_decrypt(Length(EncryptedData), @EncryptedData[1], @DecryptedData[0], RSAKey, RSA_PKCS1_PADDING);
Result := ansistring(Copy(ansistring(DecryptedData), 1, DecryptedLen));
end;
var
EncryptedString, PrivateKey: ansistring;
DecryptedString: ansistring;
begin
EncryptedString := DecodeStringBase64('...');
PrivateKey := '...';
DecryptedString := DecryptRSA(EncryptedString, PrivateKey);
WriteLn('Decrypted Data: ', DecryptedString);
end.
but not working .