Title: C# code needed for public-private Bitcoin addresses Post by: Anti-Cen on January 26, 2018, 10:42:46 AM I don't want to use external libraries such as BouncyCastle or NBitcoin but don't mind using
Dot.Net framework 4.5 that includes BigINT Nothing in windows seems easy when it comes to Bitcoin Edit to add found this but it wraps BouncyCastle but will compile in VS2017 but it's still not what I am looking for but it does do WIF conversion if anyone wants it but it Base58 and not 32 like Sewit https://github.com/Thashiznets/KeyCore.NET Title: Re: C# code needed for public-private Bitcoin addresses Post by: Coding Enthusiast on January 26, 2018, 04:36:23 PM https://github.com/TangibleCryptography/Secp256k1
Title: Re: C# code needed for public-private Bitcoin addresses Post by: Anti-Cen on January 26, 2018, 05:40:00 PM https://github.com/TangibleCryptography/Secp256k1 Thanks and this one is looking much better Title: Re: C# code needed for public-private Bitcoin addresses Post by: Anti-Cen on January 27, 2018, 03:25:38 PM Microsoft is useless and I was just skinning some code out to do RSA encryption using this bit of code
that should work on older versions of the DOT.NET framework that don't come with BigINT Code: RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(384); I want to use the old framework because it will work on more clients but after hours of looking around i come across this Quote RSA Private Key Encryption Unfortunately, the RSACryptoServiceProvider class does not provide you this option, so I wrote my own implementation of the RSA algorithm using the basics of the RSACryptoServiceProvider in conjunction with Chew Keong TAN's class: BigInteger (http://www.codeproject.com/KB/cs/biginteger.aspx). At a low level, the RSA algorithm is about implementing mathematical equations on huge (huge) integers, so the BigInteger class is really essential. I couldn't have done it myself. So thanks Microsoft for not throwing an error like "Private key encryption is not possible" instead of "Key not found" from the bloated framework. I thought that I wanted private key encryption for the signature when signing transactions and Bitcoin uses this technique but then I started to read more about it and this guy says https://rdist.root.org/2007/05/03/rsa-public-keys-are-not-private-implementation/ Quote Finally, I’d like to reiterate what I’ve said before. Public key cryptosystems and RSA in particular are extremely fragile. Do not use them differently than they were designed. Better yet, seek expert review or design assistance when working with any design involving crypto. What do you guys think ? Title: Re: C# code needed for public-private Bitcoin addresses Post by: Anti-Cen on January 27, 2018, 06:21:33 PM Well I managed to get something working without using the usual mega big 3rd party solutions if anyone is interested
that works in C# using an extension class that wraps dot.nets RSACryptoServiceProvider Sample usage tested and shown belew Code: string secret = "My secret message"; Complete listing shown below so just add a class to your project and drop this code in the file and you should be good to go with most of the credit going to Dudi Bedner Code: //All credits go to Dudi Bedner at https://www.codeproject.com/Articles/38739/RSA-Private-Key-Encryption Title: Re: C# code needed for public-private Bitcoin addresses Post by: Anti-Cen on January 31, 2018, 10:00:36 AM https://github.com/TangibleCryptography/Secp256k1 HiYes good code but using the test code it would not Verify and return true Quote if (address == message.Address) return true; publicKey.GetBitcoinAddress() needed false adding to it in the class MessageSignerVerifier Code: var signedMessage = new SignedMessage(message, publicKey.GetBitcoinAddress(false), signatureBytes); Title: Re: C# code needed for public-private Bitcoin addresses Post by: Coding Enthusiast on January 31, 2018, 02:51:25 PM Well you wanted an alternative to BouncyCastle, NBitcoin,... which are thoroughly tested. The link I gave you is a small project surely with bugs.
I haven't checked the whole thing myself, I just used the Secp256k1 class for testing. The reason you are passing false in that method to get it working is probably because your public key is uncompressed whereas in the example and the default mode, the public key is compressed. For reference: https://github.com/TangibleCryptography/Secp256k1/blob/master/Secp256k1.Core/Extensions/BigIntExtensions.cs#L171-L178 By the way this is the way you get public key from private key using this library: https://github.com/TangibleCryptography/Secp256k1/blob/master/Secp256k1.Demo/Program.cs#L14 That is a bunch of BigInt multiplications. Title: Re: C# code needed for public-private Bitcoin addresses Post by: Anti-Cen on January 31, 2018, 09:02:28 PM By the way this is the way you get public key from private key using this library: https://github.com/TangibleCryptography/Secp256k1/blob/master/Secp256k1.Demo/Program.cs#L14 That is a bunch of BigInt multiplications. Yes that's the same code supplied for testing it out. Quote BigInteger privateKey = Hex.HexToBigInteger(privKeyHex); ECPoint publicKey = Secp256k1.Secp256k1.G.Multiply(privateKey); Shame both the private address and public address objects are not both of the same type because some times I want to encrypt with the public and decrypt with private and this works with DSA if you force it. I was expecting the signature to work like that too but they do something with the public key and put that in the signature and I've not seen the code to verify the signature that runs on the node and take it that the bool verified = messageSigner.Verify(signedMessage); is client-side (Wallet) testing of the signature but maybe I was reading it wrong compliments to the guy that wrote it anyway |