A
A
AlexandrBirukov2021-07-18 13:15:56
PHP
AlexandrBirukov, 2021-07-18 13:15:56

Why are RSA signatures different in php and python?

I'm not strong in php, there is such a code that gives a signature

$json_data=json_encode($data);
$sha1_json_data=sha1($json_data);
$priv_key = openssl_pkey_get_private($key);
$encrypted = '';
openssl_private_encrypt($sha1_json_data, $encrypted, $priv_key);
$b64_encrypted=base64_encode($encrypted);


I tried to translate it into python, I used several methods, but the result is different from that in php, provided that the input data is the same

jd = json.dumps(data).replace(' ', '').encode('utf8')
# тут данные получаются один в один как в php

with open(BASE_DIR / "local_files/contract.key", "rb") as key_file:
    import rsa
    key = key_file.read()

    contract_key = rsa.PrivateKey.load_pkcs1(key)
    signature = rsa.sign(jd, contract_key, 'SHA-1')
    print(str(b64encode(signature), encoding="utf-8"))

    from cryptography.hazmat.primitives import serialization
    from cryptography.hazmat.primitives import hashes
    from cryptography.hazmat.primitives.asymmetric import padding

    private_key = serialization.load_pem_private_key(key, password=None)
    signature = private_key.sign(
        jd,
        padding.PKCS1v15(),
        hashes.SHA1()
    )
    print(str(b64encode(signature), encoding="utf-8"))


Can anyone come across this already, how to achieve the same result?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shamanov, 2021-07-18
@SilenceOfWinter

https://habr.com/ru/post/255799/

D
Developer, 2021-07-18
@samodum

I faced. But my Java/.NET had
How to export RSA private key from Java as XML?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question