gpt4 book ai didi

java - Java Card 中的受限椭圆曲线

转载 作者:搜寻专家 更新时间:2023-10-31 20:10:32 25 4
gpt4 key购买 nike

我正在尝试在 Java Card 的椭圆曲线上实现密码算法。

首先,我在 256 位椭圆曲线(NIST 椭圆曲线)上实现了它并且运行良好。

现在我想在 512 位曲线上测试它(而不是像 NIST 那样的 521)。我的卡支持这个大小,我找到了一个这个大小的椭圆曲线数据库(为密码学明确定义)。但是我遇到了一个奇怪的问题......

当我尝试初始化我的 key 时:

ECPublicKey pubKey = (ECPublicKey) KeyBuilder.buildKey(
KeyBuilder.TYPE_EC_FP_PUBLIC, (short) 0x0200, false);

pubKey.setFieldFP(new byte[] { (byte) 0x25, (byte) 0x37,
(byte) 0xD2, (byte) 0x9C, (byte) 0x8B, (byte) 0xFE,
(byte) 0x7D, (byte) 0x9F, (byte) 0x48, (byte) 0x98,
(byte) 0xF7, (byte) 0x60, (byte) 0xF8, (byte) 0x7D,
(byte) 0xBF, (byte) 0x63, (byte) 0x90, (byte) 0x6E,
(byte) 0x28, (byte) 0x99, (byte) 0x0A, (byte) 0x27,
(byte) 0x0C, (byte) 0xA6, (byte) 0x15, (byte) 0xD9,
(byte) 0x1D, (byte) 0xC4, (byte) 0x89, (byte) 0xA8,
(byte) 0xD0, (byte) 0xA1, (byte) 0xA0, (byte) 0xE7,
(byte) 0x52, (byte) 0x43, (byte) 0xB0, (byte) 0x39,
(byte) 0x01, (byte) 0x6A, (byte) 0x61, (byte) 0x43,
(byte) 0x5C, (byte) 0xA5, (byte) 0x91, (byte) 0xE9,
(byte) 0x4B, (byte) 0x1A, (byte) 0xF7, (byte) 0x60,
(byte) 0xC9, (byte) 0xAE, (byte) 0xE2, (byte) 0xCE,
(byte) 0xE0, (byte) 0x15, (byte) 0x53, (byte) 0x51,
(byte) 0x1C, (byte) 0x93, (byte) 0x0E, (byte) 0xF3,
(byte) 0xBA, (byte) 0x0B }, (short) 0x0000, (short) 0x0040);

函数 setFieldFP 引发了一个 CryptoException,原因代码为 ILLEGAL_VALUE,这意味着 key 长度不匹配......但是它确实(0x0200 是以位为单位的曲线大小,0X0040 是以字节为单位的素数长度)!

我说这真的很奇怪,因为如果我尝试使用以下值:

ECPublicKey pubKey = (ECPublicKey) KeyBuilder.buildKey(
KeyBuilder.TYPE_EC_FP_PUBLIC, (short) 0x0200, false);

pubKey.setFieldFP(new byte[] { (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF}, (short) 0x0000, (short) 0x0040);

工作正常...

所以我不得不得出结论,引发的 CryptoException 并不真正涉及参数的大小,因为在这两种情况下,大小是相同的...

那又怎样?我的卡只支持特定字段的椭圆曲线吗?有人遇到过这种问题吗?

最佳答案

您的质数不够大。对于 512 位 F(p) 上的曲线,您应该使用 512 位素数。但是,您的第一个字节 (byte) 0x25 以十六进制数字 2 开头。这意味着第一个字节首先以设置为 0 的 2 个二进制数字开头,这意味着您已经定义了一个 512 - 2 = 510 位素数。

请仅使用明确定义的曲线,例如 NIST P521 曲线或 BrainpoolP512r1 曲线。

关于java - Java Card 中的受限椭圆曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31185206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com