Bitcoin Forum
February 23, 2019, 01:45:46 AM *
News: Latest Bitcoin Core release: 0.17.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Can we convert a HEX private key to RSA private key?  (Read 184 times)
CounterEntropy
Full Member
***
Offline Offline

Activity: 154
Merit: 102


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.
1550886346
Hero Member
*
Offline Offline

Posts: 1550886346

View Profile Personal Message (Offline)

Ignore
1550886346
Reply with quote  #2

1550886346
Report to moderator
1550886346
Hero Member
*
Offline Offline

Posts: 1550886346

View Profile Personal Message (Offline)

Ignore
1550886346
Reply with quote  #2

1550886346
Report to moderator
1550886346
Hero Member
*
Offline Offline

Posts: 1550886346

View Profile Personal Message (Offline)

Ignore
1550886346
Reply with quote  #2

1550886346
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
darosior
Member
**
Offline Offline

Activity: 112
Merit: 121


View Profile WWW
January 28, 2019, 09:13:01 PM
Last edit: January 29, 2019, 10:37:28 PM by darosior
Merited by ETFbitcoin (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).

ETFbitcoin
Legendary
*
Offline Offline

Activity: 1596
Merit: 1555

Use SegWit and enjoy lower fees.


View Profile WWW
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.

CounterEntropy
Full Member
***
Offline Offline

Activity: 154
Merit: 102


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
Hero Member
*****
Offline Offline

Activity: 1022
Merit: 955


the forkings will continue until morale improves


View Profile
January 29, 2019, 09:59:09 PM
Merited by ETFbitcoin (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.

odolvlobo
Legendary
*
Online Online

Activity: 2408
Merit: 1231



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

Buy bitcoins with cash from somebody near you: LocalBitcoins
Buy stuff on Amazon at a discount with bitcoins or convert Amazon points to bitcoins: Purse.io
Join an anti-signature campaign: DannyHamilton's ignore list
ETFbitcoin
Legendary
*
Offline Offline

Activity: 1596
Merit: 1555

Use SegWit and enjoy lower fees.


View Profile WWW
January 30, 2019, 06:38:45 PM
Merited by bones261 (4)
 #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.

CounterEntropy
Full Member
***
Offline Offline

Activity: 154
Merit: 102


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:  

Bitcointalk.org is not available or authorized for sale. Do not believe any fake listings.
Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!