我正在尝试在Java卡上重新创建公共密钥,并使用它来加密一些数据。
这是构建我正在使用的公钥的代码:
rsaPublicId = (RSAPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_2048, false);
rsaPublicId.setExponent(rsaExponent, (short) 0, (short) rsaExponent.length);
rsaPublicId.setModulus(rsaPublicModulus, (short) 12, (short) ((short) rsaPublicModulus.length - (short) 12));
cipherId.init(rsaPublicId, Cipher.MODE_ENCRYPT);
当我尝试加密数据时,我使用以下代码:
cipherId.doFinal(serviceBytes, (short) 0, (short) 16, buffer, (short) 0);
但是它会发生javacard.security.cryptoException,其详细消息为“ null”
cipherId = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);
模数设置为308,由Java创建并发送到卡。
您的模数应与buildKey
通话期间设置的大小完全相同。因此,如果您的大小为308(308是什么?),则您的密钥太小(以位为单位)或太大(如果以字节为单位指定)。
是否应允许使用较小的密钥(最多308位,这是一个很小的最小值)是一个讨论的主题。但是,在当前规范中,不允许使用比方法调用中给定的键更小的键。键-因此模数-必须是指定的确切大小。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句