我尝试在我的Java程序中读取ECPublicKey。该密钥是由Intel SGX安全区生成的,该安全区通过套接字发送到服务器。
那是我的Java代码(我跳过了读取套接字部分,直接插入了生成的密钥。
byte[] otherPub = Hex.decode("74044d458d4a462205d29fd59dfa3142a821f614879c5c010a20e05867def6a188c9c057225e20f73a4731f8eae265fdf999da4e19df7d11115e269791282b05");
byte [] x = new byte [32];
byte [] y = new byte [32];
for (int i = 0; i<32; i++)
{
x[i] = otherPub[i];
}
for (int i = 0; i<32; i++)
{
y[i] = otherPub[i+32];
}
BigInteger myX= new BigInteger(x);
BigInteger myY= new BigInteger(y);
ECPoint w = new ECPoint (myX,myY);
ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256r1");
KeyFactory kf = KeyFactory.getInstance("ECDH", new BouncyCastleProvider());
ECNamedCurveSpec params = new ECNamedCurveSpec("secp256r1", spec.getCurve(), spec.getG(), spec.getN());
ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(w, params);
ECPublicKey pk = (ECPublicKey) kf.generatePublic(pubKeySpec);
每次我遇到以下错误
Exception in thread "main" java.security.spec.InvalidKeySpecException: invalid KeySpec: x value invalid for SecP256R1FieldElement
at org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi.engineGeneratePublic(Unknown Source)
at java.security.KeyFactory.generatePublic(Unknown Source)
at Connection.serverConnection(Connection.java:158)
at main.main(main.java:12)
Caused by: java.lang.IllegalArgumentException: x value invalid for SecP256R1FieldElement
at org.bouncycastle.math.ec.custom.sec.SecP256R1FieldElement.<init>(Unknown Source)
at org.bouncycastle.math.ec.custom.sec.SecP256R1Curve.fromBigInteger(Unknown Source)
at org.bouncycastle.math.ec.ECCurve.createPoint(Unknown Source)
at org.bouncycastle.math.ec.ECCurve.createPoint(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertPoint(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertPoint(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey.<init>(Unknown Source)
... 4 more
当我在Java中生成一个密钥对并将其插入变量otherPub时,我没有收到错误。我在网上看了一下,使用函数sgx_ecc256_generate_key_pair的sgx中的密钥生成是Little Endian,位于曲线secp256r1上。我怎么了有谁能够帮助我 ?
谢谢
奥马拉马