从公钥字节数组重新创建椭圆曲线的 X 和 Y [英] Recreate X and Y of Elliptic curve from public key byte array

查看:44
本文介绍了从公钥字节数组重新创建椭圆曲线的 X 和 Y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对密码学真的很陌生,但是 - 我想要做的是从公钥的字节数组表示中获取 X 和 Y 坐标.我正在使用 secp256r1 曲线.

I am really new to cryptography but - what I am trying to do is get the X and Y coordinates from byte array representation of public key. Iam using secp256r1 curve.

// get curve
X9ECParameters x9 = ECNamedCurveTable.GetByName("secp256r1");
ECCurve curve = x9.Curve;

// get coordinates from ASN.1 encoded public key point
var asn1 = (Asn1Sequence)Asn1Object.FromByteArray(publicKeyBytes);
var at1 = (DerBitString)asn1[1];
var bytes = at1.GetBytes();
var x = bytes.Skip(1).Take(32).Reverse().ToArray();
var y = bytes.Skip(33).Take(32).Reverse().ToArray();

// get affine X and Y using point on curve from X and Y
var ecPoint = curve.CreatePoint(new Org.BouncyCastle.Math.BigInteger(1, x), new Org.BouncyCastle.Math.BigInteger(1, y));
ECDomainParameters dParams = new ECDomainParameters(curve, ecPoint, x9.N);
ECPublicKeyParameters pubKey = new ECPublicKeyParameters(ecPoint, dParams);
var affineX = pubKey.Q.AffineXCoord.ToBigInteger().ToByteArrayUnsigned();
var affineY = pubKey.Q.AffineYCoord.ToBigInteger().ToByteArrayUnsigned();

// return a tuple of the coordinates
return (affineX, affineY);

我收到 X 和 Y 坐标,但这些可能不正确.我究竟做错了什么?谢谢

I receive X and Y coordinates but these are probably not right. What am I doing wrong? Thanks

推荐答案

好的,代码几乎没有问题.这是有效的版本,也许它会帮助某人

Ok, so, there were few issues with the code. Here is the version that works, maybe it will help someone

internal static (string x, string y) GetCertificateCoordinates(byte[] publicKeyBytes)
{
    // parse based on asn1 format the content of the certificate
    var asn1 = (Asn1Sequence)Asn1Object.FromByteArray(publicKeyBytes);
    var at1 = (DerBitString)asn1[1];
    var xyBytes = at1.GetBytes();

    //retrieve preddefined parameters for P256 curve
    X9ECParameters x9 = ECNamedCurveTable.GetByName("P-256");
    //establish domain we will be looking for the x and y
    ECDomainParameters domainParams = new ECDomainParameters(x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed());
    ECPublicKeyParameters publicKeyParams = new ECPublicKeyParameters(x9.Curve.DecodePoint(xyBytes), domainParams);
    //get the affine x and y coordinates
    var affineX = EncodeCordinate(publicKeyParams.Q.AffineXCoord.ToBigInteger());
    var affineY = EncodeCordinate(publicKeyParams.Q.AffineYCoord.ToBigInteger());

    return (affineX, affineY);
}

public static string EncodeCordinate(Org.BouncyCastle.Math.BigInteger integer)
{
    var notPadded = integer.ToByteArray();
    int bytesToOutput = (256 + 7) / 8;
    if (notPadded.Length >= bytesToOutput)
        return Jose.Base64Url.Encode(notPadded);
    var padded = new byte[bytesToOutput];
    Array.Copy(notPadded, 0, padded, bytesToOutput - notPadded.Length, notPadded.Length);
    return Jose.Base64Url.Encode(padded);
}

我在 android 中使用此代码作为 JWT 向服务器端提供 X 和 Y 作为标题中的 jwk

I am using this code in android for the JWT providing X and Y to server side as jwk in header

这篇关于从公钥字节数组重新创建椭圆曲线的 X 和 Y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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