Bitcoin Forum
May 09, 2024, 03:10:32 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Can we convert a HEX private key to RSA private key?  (Read 379 times)
CounterEntropy (OP)
Full Member
***
Offline Offline

Activity: 214
Merit: 278


View Profile
January 28, 2019, 08:18:23 PM
Last edit: January 29, 2019, 04:49:52 PM by CounterEntropy
 #1

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() 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.
No Gods or Kings. Only Bitcoin
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715267432
Hero Member
*
Offline Offline

Posts: 1715267432

View Profile Personal Message (Offline)

Ignore
1715267432
Reply with quote  #2

1715267432
Report to moderator
1715267432
Hero Member
*
Offline Offline

Posts: 1715267432

View Profile Personal Message (Offline)

Ignore
1715267432
Reply with quote  #2

1715267432
Report to moderator
darosior
Sr. Member
****
Offline Offline

Activity: 279
Merit: 435


View Profile
January 28, 2019, 09:13:01 PM
Last edit: January 29, 2019, 10:37:28 PM by darosior
Merited by joniboini (3), ABCbits (1), LeGaulois (1)
 #2

If yes, how? Especially  in PHP...
Hi,

Do you mean a secp256k1 private key ? Anyway private keys are just numbers and in the case of secp256k1 it is a 256bit long number. So you can get a 256bit long RSA private key, which is not much secure... 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).
ABCbits
Legendary
*
Offline Offline

Activity: 2870
Merit: 7490


Crypto Swap Exchange


View Profile
January 29, 2019, 06:25:11 AM
Merited by HeRetiK (1)
 #3

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.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
CounterEntropy (OP)
Full Member
***
Offline Offline

Activity: 214
Merit: 278


View Profile
January 29, 2019, 04:46:59 PM
 #4

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.
HeRetiK
Legendary
*
Offline Offline

Activity: 2926
Merit: 2091


Cashback 15%


View Profile
January 29, 2019, 09:59:09 PM
Merited by joniboini (3), ABCbits (1), LeGaulois (1)
 #5

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() 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.

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
odolvlobo
Legendary
*
Offline Offline

Activity: 4298
Merit: 3214



View Profile
January 30, 2019, 01:50:24 AM
 #6

I want to sign a message with a Bitcoin address using PHP. I am using openssl_sign() 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

Join an anti-signature campaign: Click ignore on the members of signature campaigns.
PGP Fingerprint: 6B6BC26599EC24EF7E29A405EAF050539D0B2925 Signing address: 13GAVJo8YaAuenj6keiEykwxWUZ7jMoSLt
ABCbits
Legendary
*
Offline Offline

Activity: 2870
Merit: 7490


Crypto Swap Exchange


View Profile
January 30, 2019, 06:38:45 PM
Merited by bones261 (4), joniboini (3)
 #7

How about this library 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.

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
CounterEntropy (OP)
Full Member
***
Offline Offline

Activity: 214
Merit: 278


View Profile
February 05, 2019, 07:50:49 PM
 #8

How about this library 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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!