java card的rsa运算,JavaCard的小程序不与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();

}

And main method is:

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);

}

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

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

I tried with other applets and everything works fine.

I compile my applet with JavaCard 2.2.1 and Java 1.2

Do you have any idea what's going on?

解决方案

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

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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值