【发布时间】:2021-02-03 16:41:14
【问题描述】:
public class EccGenerator
{
public AsymmetricCipherKeyPair GenerateKeyPairs(int keySize)
{
ECKeyPairGenerator gen = new ECKeyPairGenerator("ECDSA");
KeyGenerationParameters keyGenParam = new KeyGenerationParameters(new SecureRandom(), keySize);
gen.Init(keyGenParam);
AsymmetricCipherKeyPair keyPair = gen.GenerateKeyPair();
return keyPair;
}
public static void PrintPublicX_Y(AsymmetricKeyParameter publicKey)
{
ECPublicKeyParameters key = (ECPublicKeyParameters)publicKey;
string X = Encoding.ASCII.GetString(UrlBase64.Encode(key.Q.XCoord.ToBigInteger().ToByteArrayUnsigned()));
string Y = Encoding.ASCII.GetString(UrlBase64.Encode(key.Q.YCoord.ToBigInteger().ToByteArrayUnsigned()));
Console.WriteLine(X + "--" + Y);
}
public static void PrintPrivateX_Y(AsymmetricKeyParameter privateKey)
{
ECPrivateKeyParameters key = (ECPrivateKeyParameters)privateKey;
string D = Encoding.ASCII.GetString(UrlBase64.Encode(key.D.ToByteArrayUnsigned()));
string X = Encoding.ASCII.GetString(UrlBase64.Encode(key.Parameters.G.XCoord.ToBigInteger().ToByteArrayUnsigned()));
string Y = Encoding.ASCII.GetString(UrlBase64.Encode(key.Parameters.G.YCoord.ToBigInteger().ToByteArrayUnsigned()));
Console.WriteLine(X + "--" + Y);
}
public static void Main(string[] args)
{
EccGenerator ecc = new EccGenerator();
AsymmetricCipherKeyPair pair = ecc.GenerateKeyPairs(256);
PrintPublicX_Y(pair.Public);
PrintPrivateX_Y(pair.Private);
}
}
这里的问题是 public 和 private 中的 X 不完全相同,Y 也一样,我怎样才能得到正确的 x 和 y 以及 private 的 D
我正在使用 Bouncy Castle C#
【问题讨论】:
-
该代码无法编译,充满了错误,因此无法重现。请先修复这些错误。
-
对不起,我编辑了,只包括充气城堡
-
G 是基点。公钥与私钥相乘得到,即将
key.Parameters.G替换为key.Parameters.G.Multiply(key.D).Normalize()。 -
像魅力一样工作 :) 你能发布答案来投票吗?)
-
当然,我已经发布了答案。
标签: bouncycastle elliptic-curve