JavaCard 小程序不适用于 RSA 加密 [英] JavaCard applet is not working with RSA encryption

查看:20
本文介绍了JavaCard 小程序不适用于 RSA 加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 JavaCard 小程序.Applet 在构造函数中生成 RSA 公钥和私钥,并使用 APDU 命令加密一些字节数组:

I am developing a JavaCard applet. Applet generates RSA public and private keys in constructor and with APDU command encrypt some byte array:

 public RSATestApplet() {
    keyPair = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_2048);
    keyPair.genKeyPair();
    rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    rsaPublicKey = (RSAPublicKey) keyPair.getPublic();

    cipher = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);

    register();
}

主要方法是:

private void encryptData(APDU apdu) {
    if (!rsaPublicKey.isInitialized()) {
        ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
    }
    byte[] apduBuffer = apdu.getBuffer();
    apdu.setIncomingAndReceive();

    cipher.init(rsaPrivateKey, Cipher.MODE_ENCRYPT);
    byte[] encryptedBuffer = new byte[apduBuffer.length];
    Util.arrayFillNonAtomic(encryptedBuffer, (short) 0,
            (short) encryptedBuffer.length, (byte) 0xAA);
    cipher.doFinal(encryptedBuffer, (short) 0, (short) encryptedBuffer.length, apduBuffer, (short) 0);
    // Just for testing send 120 bytes
    apdu.setOutgoingAndSend((short) 0, (short) 120);
}

当我尝试安装小程序时,APDU 响应是 6E00(这意味着:没有精确的诊断).

And when I try to install applet APDU response is 6E00 (which means: No precise diagnosis).

我认为 cipher.doFinal() 执行时可能会出现问题.

I think problem may occurs when cipher.doFinal() is executing.

我尝试过其他小程序,一切正常.

I tried with other applets and everything works fine.

我用 JavaCard 2.2.1 和 Java 1.2 编译我的小程序

I compile my applet with JavaCard 2.2.1 and Java 1.2

你知道发生了什么吗?

推荐答案

我坚信您在安装小程序时遇到的错误与您的 encryptData 方法无关.

I strongly believe that the error you get during your installation of applet is not related to your encryptData method.

我建议您在构造函数中使用 try catch 来捕获 JCVM 抛出的异常.例如,当您创建 KeyPair 对象时,如果平台不支持该算法和密钥长度,它可能会抛出错误.

I would suggest you to use try catch inside your constructor to catch the exception thrown by JCVM. For example, when you create KeyPair object, it can throw an error if the algorithm and key length are not supported by the platform.

你可以试试这样的:

try {
     keyPair = new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_2048);
} catch (CryptoException e) {
     short reason = e.getReason();
     ISOException.throwIt(reason);
}

这篇关于JavaCard 小程序不适用于 RSA 加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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