Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: BlackHatCoiner on November 30, 2020, 07:12:02 AM



Title: C# - Encrypting messages with ECDSA?
Post by: BlackHatCoiner on November 30, 2020, 07:12:02 AM
Based on my previous thread (PGP Questions) (https://bitcointalk.org/index.php?topic=5290130.0) I can admit that encrypting messages with PGP on C# is pretty difficult to understand. What I want to know is if there is an easy way to encrypt/decrypt and sign/verify messages from a library like NBitcoin on C#.

What functions does Bitcoin Core use? For example, are there any simple functions like:
Code:
ECDSA_Encypt(message, publicKey)


Title: Re: C# - Encrypting messages with ECDSA?
Post by: pooya87 on November 30, 2020, 07:38:03 AM
PGP is not used anywhere in of Bitcoin protocol so no bitcoin library should have that option. They do however have ECC (Elliptic Curve Cryptography) so technically they can also implement ECIES (Elliptic Curve Integrated Encryption Scheme) for message encryption and decryption.

I don't know any library that has ECIES but it shouldn't be hard to implement yourself on top of that library if the ECC methods are public. You just need an RNG (found in .net or the RFC6979), EC point multiplication (found in the library), a symmetric encryption (like AES found in .net), a KDF (found in .net), a message authentication code (like HMACSHA512 found in .net).

Read on wikipedia: https://en.wikipedia.org/wiki/Integrated_Encryption_Scheme


Title: Re: C# - Encrypting messages with ECDSA?
Post by: BlackHatCoiner on November 30, 2020, 09:58:03 AM
This is it? If I find a library that contains an ECIES function and create a digital signature will it be verified from programs like Bitcoin Core and Electrum? I searched it a little bit and it seems kinda hard to implement it on c#, but I guess it is possible.


Title: Re: C# - Encrypting messages with ECDSA?
Post by: pooya87 on November 30, 2020, 11:23:24 AM
You don't verify encrypted messages, you decrypt them if you have the private key. (the encryption is done using the public key of the receiver).
Electrum already has this (Tools > Encrypt/decrypt messages) but I don't think bitcoin core does though.


Title: Re: C# - Encrypting messages with ECDSA?
Post by: BlackHatCoiner on November 30, 2020, 11:48:05 AM
Are these examples of ECIES implemented on C#?
https://csharp.hotexamples.com/examples/Org.BouncyCastle.Math.EC/FpCurve/-/php-fpcurve-class-examples.html

Is it bad that I don't understand a thing? Why isn't there a simple command to do that...


Title: Re: C# - Encrypting messages with ECDSA?
Post by: NeuroticFish on November 30, 2020, 01:38:10 PM
What I want to know is if there is an easy way to encrypt/decrypt and sign/verify messages from a library like NBitcoin on C#.

Although I didn't use it, NBitcoin seems to actually be able to sign/verify message.
If you look in the second example from here: https://csharp.hotexamples.com/examples/-/NBitcoin.Crypto/-/php-nbitcoin.crypto-class-examples.html
It has near the end 2 lines that should be of interest:

Code:
var signature = secret.PrivateKey.SignMessage(test.Message);
Assert.True(((BitcoinPubKeyAddress)Network.Main.CreateBitcoinAddress(test.Address)).VerifyMessage(test.Message, signature));



Title: Re: C# - Encrypting messages with ECDSA?
Post by: BrewMaster on November 30, 2020, 02:08:32 PM
Although I didn't use it, NBitcoin seems to actually be able to sign/verify message.

the way i understand it is that signing/verifying messages is different from encrypting/decrypting messages.
in the first one you publish the message itself plus the public key (or the address) that the signing was from. and the purpose is to prove you own the private key to that address or public key.
in the second one (encrypting) you only publish the encrypted message and nothing else. and only the receiver who owns the private key can decrypt the message and see what it says.


Title: Re: C# - Encrypting messages with ECDSA?
Post by: NeuroticFish on November 30, 2020, 02:27:23 PM
the way i understand it is that signing/verifying messages is different from encrypting/decrypting messages.

(Corrected after re-reading your message)
I see them similar to what you wrote. Indeed, encrypting should involve a transformation that will allow through decryption to restore the original message. I don't know if this is what OP wants, hence I didn't try to answer to that part (although I know that BitCrypt does this).

And since OP also said he is interested about signing and verifying messages in a simple way, I tried to cover that part.


Title: Re: C# - Encrypting messages with ECDSA?
Post by: BlackHatCoiner on November 30, 2020, 02:29:27 PM
Although I didn't use it, NBitcoin seems to actually be able to sign/verify message.
If you look in the second example from here: https://csharp.hotexamples.com/examples/-/NBitcoin.Crypto/-/php-nbitcoin.crypto-class-examples.html
It has near the end 2 lines that should be of interest:

Code:
var signature = secret.PrivateKey.SignMessage(test.Message);
Assert.True(((BitcoinPubKeyAddress)Network.Main.CreateBitcoinAddress(test.Address)).VerifyMessage(test.Message, signature));

Thank you! As for the difference between signing and encrypting yeah, they're not the same. Although I was searching for a way to implement both of them on c#. NeuroticFish helped me with signing/verifying. Now I'll have to figure out how to encrypt messages which is more important.

Question, from the same page NeuroticFish mentioned: public EncryptedKeyResult GenerateEncryptedSecret (https://csharp.hotexamples.com/examples/-/NBitcoin.Crypto/-/php-nbitcoin.crypto-class-examples.html#0x8324a3340b937a63b436a04eb5a786d976d89b42bd2caeee8518412c16b0b180-209,,279,). Could this be what I'm looking for?