Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: CounterEntropy on January 28, 2019, 08:18:23 PM



Title: Can we convert a HEX private key to RSA private key?
Post by: CounterEntropy on January 28, 2019, 08:18:23 PM
If yes, how? Especially  in PHP...

Update: Here is what I want in detail...

I want to sign a message with a Bitcoin address using PHP. I am using openssl_sign() (http://php.net/manual/en/function.openssl-sign.php) for this. But, this appears to accept RSA private key, instead of WIF or HEX format. Now, how do I convert WIF/HEX to the required format for signing?

p.s. I know it can be done by calling Bitcoin Core through JSON-RPC or there are some custom crypto libraries available. But, my purpose is just to automate the signing process and hence I dont want to increase the overhead.


Title: Re: Can we convert a HEX private key to RSA private key?
Post by: darosior on January 28, 2019, 09:13:01 PM
If yes, how? Especially  in PHP...
Hi,

Do you mean a secp256k1 (https://en.bitcoin.it/wiki/Secp256k1) private key ? Anyway private keys are just numbers and in the case of secp256k1 (https://en.bitcoin.it/wiki/Secp256k1) it is a 256bit long number. So you can get a 256bit long RSA private key, which is not much secure (https://en.wikipedia.org/wiki/RSA_(cryptosystem)#Integer_factorization_and_RSA_problem)... Why do you want to do that ?

Update : Since you updated your question here is the updated answer :
Quote
I want to sign a message with a Bitcoin address using PHP.  
You cannot sign a message with an address (which is a hash of a public key), however you can sign a message using the private key used to derive this address.

Quote
I am using openssl_sign() (http://php.net/manual/en/function.openssl-sign.php) for this. But, this appears to accept RSA private key, instead of WIF or HEX format.
You can sign a message using ECDSA on the secp256k1 using any lib (such as this, first search engine result : https://github.com/kornrunner/php-secp256k1) which would be as simple as :
Code:
$signature = $secp256k1->sign($message, $privateKey);
You won't be able to provide a non-forgeable RSA signature from such a short private key.

Quote
Now, how do I convert WIF/HEX to the required format for signing?
The format will be depending on the library you use. However WIF is roughly a base58check encoding so you can easily get hex back from it.

Quote
or there are some custom crypto libraries available. But, my purpose is just to automate the signing process and hence I dont want to increase the overhead.
If you dont want to use secp256k1 you can use another sognature algorithm (I dont know which one the php standard library provides).


Title: Re: Can we convert a HEX private key to RSA private key?
Post by: ABCbits on January 29, 2019, 06:25:11 AM
HEX/Hexadecimal is just a format to represent private key (or any data) while RSA is a type of cryptography. I don't see direct correlation between both of them.
As darosior said, you can convert or treat any hex private key as RSA private key, you just need to make sure your software accept HEX format.

But i think it's XY problem and you better tell us what you're trying achieve rather than tell how you try to achieve your problem/goal.


Title: Re: Can we convert a HEX private key to RSA private key?
Post by: CounterEntropy on January 29, 2019, 04:46:59 PM
Why do you want to do that ?

But i think it's XY problem and you better tell us what you're trying achieve rather than tell how you try to achieve your problem/goal.

OP updated with detailed requirement.


Title: Re: Can we convert a HEX private key to RSA private key?
Post by: HeRetiK on January 29, 2019, 09:59:09 PM
If yes, how? Especially  in PHP...

Update: Here is what I want in detail...

I want to sign a message with a Bitcoin address using PHP. I am using openssl_sign() (http://php.net/manual/en/function.openssl-sign.php) for this. But, this appears to accept RSA private key, instead of WIF or HEX format. Now, how do I convert WIF/HEX to the required format for signing?

p.s. I know it can be done by calling Bitcoin Core through JSON-RPC or there are some custom crypto libraries available. But, my purpose is just to automate the signing process and hence I dont want to increase the overhead.

According to the openssl_sign() documentation the priv_key_id argument is expected to be a PEM formatted string.

Now assuming you really do want to roll this whole thing yourself (which I honestly wouldn't recommend) you'd need to convert the Bitcoin private key to a PEM formatted string. That is, from hex to ASN.1 as DER to Base64  (At least from what I can cobble together from here [1] and here [2]). Note that you need to convert from the raw private key, not the WIF encoded one.

[1] https://support.quovadisglobal.com/kb/a37/what-is-pem-format.aspx
[2] https://wiki.openssl.org/index.php/DER

Here's an example of how to do such an example as a shell script:
https://stackoverflow.com/questions/16975842/how-to-convert-an-ecdsa-key-from-hex-to-pem-format

Here's a JavaScript implementation:
https://holtstrom.com/michael/tools/hextopem.php

Here's some guy's PHP solution:
https://github.com/ionux/php-asn1-library

Note that I haven't verified that any of this code is actually working as intended or even secure. Use at your own peril but maybe it will point you in the right direction.

Also I'm not sure whether Bitcoin actually uses the standard OpenSSL message signing method or some slight derivation of it, so you'd need to double-check. All I know is that signing messages using Bech32 addresses is not yet standardized.

That being said, really do reconsider using Bitcoin Core's JSON-RPC calls. Unless you actually know what you are doing you are more likely to increase your headache rather than to reduce your overhead.


Title: Re: Can we convert a HEX private key to RSA private key?
Post by: odolvlobo on January 30, 2019, 01:50:24 AM
I want to sign a message with a Bitcoin address using PHP. I am using openssl_sign() (http://php.net/manual/en/function.openssl-sign.php) for this. But, this appears to accept RSA private key, instead of WIF or HEX format. Now, how do I convert WIF/HEX to the required format for signing?

Sorry, I know very little about PHP, but this might help: https://www.example-code.com/phpext/ecdsa_sign_data.asp


Title: Re: Can we convert a HEX private key to RSA private key?
Post by: ABCbits on January 30, 2019, 06:38:45 PM
How about this library https://github.com/BitcoinPHP/BitcoinECDSA.php (https://github.com/BitcoinPHP/BitcoinECDSA.php)

It's specifically made to generate address, sign message & verify signed message which means the overhead should be little. I read the code a little, but i'm confused how to import private key and looks like they don't support Hierarchical Deterministic (HD) key.

P.S. I never use this library and can't verify whether it's working, secure or/and work efficiently.


Title: Re: Can we convert a HEX private key to RSA private key?
Post by: CounterEntropy on February 05, 2019, 07:50:49 PM
How about this library https://github.com/BitcoinPHP/BitcoinECDSA.php (https://github.com/BitcoinPHP/BitcoinECDSA.php)

It's specifically made to generate address, sign message & verify signed message which means the overhead should be little. I read the code a little, but i'm confused how to import private key and looks like they don't support Hierarchical Deterministic (HD) key.

P.S. I never use this library and can't verify whether it's working, secure or/and work efficiently.

WoW. This is the code - https://github.com/BitcoinPHP/BitcoinECDSA.php/blob/master/src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php

After some tweaking, it works like a charm.

Thanks a LOT @ETFbitcoin. Those, who have merit, I'd request them to honor you on my behalf.