有弹性的城堡ECDSA从私钥创建公钥 [英] Bouncy Castle ECDSA Create Public Key from Private Key

查看:168
本文介绍了有弹性的城堡ECDSA从私钥创建公钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用C#签署比特币交易.我有2位要尝试完成的代码.我可以使用Bouncy城​​堡创建一组私钥和公钥.我可以将其转换为钱包导入格式.

I am trying to sign a bitcoin transaction in c#. I have 2 bits of code I am trying to complete. I can create a set of private and public keys using Bouncy castle. I can convert this to wallet import format ok.

我还可以从ECDSA公钥生成一个比特币地址.

I can also generate a bitcoin address from the ECDSA public key.

但是,我想签署交易,而我所拥有的只是我的私钥.我不想导入钱包并签名.那么,仅给定私钥,如何生成公钥?

However, I want to sign a transaction and all I have is my private key. I don't want to have to import into a wallet and sign. So how can I generate the public key, given only the private key?

我已经找到了执行此操作的javascript方法:

I have found a javascript method that does this:

ecparams.getG().multiply(this.priv).getEncoded();

我在Bouncy Castle中看到的唯一方法是生成一个随机对.

The only way I've seen in Bouncy Castle is to generate a random pair.

private static AsymmetricCipherKeyPair GenerateKeys(int keySize)
{
  ECKeyPairGenerator gen = new ECKeyPairGenerator();
  SecureRandom secureRandom = new SecureRandom();
  KeyGenerationParameters keyGenParam = new KeyGenerationParameters(secureRandom, keySize);
  gen.Init(keyGenParam);
  return gen.GenerateKeyPair();
}

推荐答案

如果要处理DER编码的键,它甚至更简单:

If you're dealing with DER encoded keys, it's even simpler:

  var privateKey = PrivateKeyFactory.CreateKey(bytes) as ECPrivateKeyParameters;
  if (privateKey == null)
       return null;
  Org.BouncyCastle.Math.EC.ECPoint q = privateKey.Parameters.G.Multiply(privateKey.D);
  var publicParams = new ECPublicKeyParameters(privateKey.AlgorithmName, q, privateKey.PublicKeyParamSet);
  return SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicParams).GetDerEncoded();

这篇关于有弹性的城堡ECDSA从私钥创建公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆