java - Diffie Hellman JavaCard

标签 java javacard diffie-hellman

我在 JavaCard 上遇到 DiffieHellman 问题。我有这门课: https://pastebin.com/2F2sQ2Pe (https://github.com/ASKGLab/DHApplet)(它的文件更大,所以我上传到pastebin,不确定它是否有问题)

然后我创建它的 2 个实例并像这样调用它(仅显示一个实例):

DiffieHellman dh = new DiffieHellman();
dh.init();
dh.getY(hostY, (short)0);
dh.setY(cardY, (short) 0, (short) cardY.length, (short) 0); 
AESKey encKey = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES_TRANSIENT_RESET, KeyBuilder.LENGTH_AES_128, false);
dh.doFinal(encKey);

hostY 和cardY 是公共(public)值。我在桌面应用程序上尝试过,所以我保证与JavaCard的通信没有问题。所以我的问题是,毕竟这些 SharedSecret 有所不同,我不知道为什么,因为我通过 RSA 的解密执行 Y = G^bobPrivKey mod P 来让 Y 传输它们,然后通过 RSA 的解密执行 S = Y^a mod p 。

感谢您的提前答复。

最佳答案

(假设您在桌面上使用 jCardSim 进行 Java Card API 模拟)

jCardSim 存在一个问题,它始终使用 CRT 私钥(所使用的 RSAKeyPairGenerator 始终生成始终实现 RSAPrivateCrtKeyParameters 的 CRT 私钥 - 请参阅 herehere)。

因此,每个 jCardSim RSA 私钥(即使是使用 ALG_RSA 生成的私钥)都是由 RSAPrivateCrtKeyImpl 实现的(您可以使用 .getClass().getCanonicalName( ))。

真正的问题是 RSAPrivateCrtKeyImpl 类在进行实际加密时忽略模数的值:

AssymetricCipherImpl.init() :

// ...some code above skipped...
KeyWithParameters key = (KeyWithParameters) theKey;
engine.init(theMode == MODE_ENCRYPT, key.getParameters());
// ...some code below skipped...

RSAPrivateCrtKeyImpl.getParameters() -- 没有使用 modulus 字段:

public CipherParameters getParameters() {
    if (!isInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }
    // modulus = p * q;
    return new RSAPrivateCrtKeyParameters(p.getBigInteger().multiply(q.getBigInteger()), null,
            null, p.getBigInteger(), q.getBigInteger(),
            dp1.getBigInteger(), dq1.getBigInteger(), pq.getBigInteger());
}

所以 setModulus() call用于设置所需的 DH 组素数不起作用,并使用原始(生成的)模数。

祝你好运!

关于java - Diffie Hellman JavaCard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43746590/

相关文章:

c# - 如何在全局平台卡外存储库上设置 key ?

java - 我为练习编写的代码有效吗?

java - Spring Boot - 如何将 application.yml 属性定义为 application.properties

java - Intellij Idea如何切换到其他git账号

javacard - securechannel.processSecurity(apdu) 中的错误 0x6700

java - 为什么我的 Java Card 小程序返回 16 个字节的零而不是 AES 加密值?

python - 在 Python 中使用 Diffie-Hellman 的服务器端 SSL

Java 生成 DH 公钥大小与文档中的示例大小不同

java - org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean

java - Hibernate @ManyToOne 删除一侧的条目,在多侧将 FK 设置为 NULL