JavaCard的小程序不与RSA加密工作 [英] JavaCard applet is not working with RSA encryption

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

问题描述

我开发一个JavaCard的小程序。小程序在构造和APDU命令加密一些字节数组生成RSA公钥和私钥:

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(这意味着:没有precise诊断)。

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

你有任何想法是怎么回事?

Do you have any idea what's going on?

推荐答案

我坚信,您的小程序的安装过程中得到的错误是不相关的encryptData方法。

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

我会建议你使用尝试捕捉你的构造函数里面搭上由JCVM引发的异常。例如,在创建密钥对对象,它可以如该算法和密钥长度不是由平台支撑引发错误。

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.

您可以尝试这样的事情:

You can try something like this:

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天全站免登陆