【发布时间】:2016-06-27 16:45:19
【问题描述】:
我编写了一个程序,使用我的 Java 卡上的 RSA 公钥加密 10 字节随机数。每次卡接收到该 APDU 命令时都会生成随机数,并且由于我的小程序中相关的密码对象块大小为 2048 位,因此我在此 10 字节随机数的末尾附加了 242 个字节的 0x00 到使其成为256 字节长度。
问题是有时响应是带有05 值的加密异常。如您所知以及提到的 JC API 文档:
0x05= ILLEGAL_USEpublic static final short ILLEGAL_USE
此原因码用于 表示签名或密码算法不填充 传入消息和输入消息不是块对齐的。
由于输入长度在我的小程序中是固定的,我无法解决我的问题!
这是我的小程序的相关部分:
protected final void getEncChallenge(APDU apdu) {
random.generateData(generatedChall, (short) 0, (short) 10);
initAsymCipher(ENTITY_BOB, MODE_ENC);
Util.arrayCopy(generatedChall, (short) 0, transientData, (short) 0, (short) 10);
Util.arrayFillNonAtomic(transientData, (short) 10, (short) 246, (byte) 0x00);
rsaCrypto(transientData, persistentData);
apdu.setOutgoing();
apdu.setOutgoingLength((short) 256);
apdu.sendBytesLong(persistentData, (short) 0, (short) 256);
}
protected final void rsaCrypto(byte[] inData, byte[] outData) {
try{
asymCipher.doFinal(inData, (short) 0, (short) 256, outData, (short) 0);
}catch(CryptoException e){
short reason = e.getReason();
ISOException.throwIt((short)(0x6d00 | reason));
}
}
下面是回复:
transientData ---> APDU Response
---------------------------------
80 ..(Eight Random Bytes).. BD ..(246 bytes of "0x00").. ---> OK (i.e. 256 byte encrypted data)
EO ..(Eight Random Bytes).. 64 ..(246 bytes of "0x00").. ---> 6D05
02 ..(Eight Random Bytes).. B3 ..(246 bytes of "0x00").. ---> OK
CB ..(Eight Random Bytes).. 35 ..(246 bytes of "0x00").. ---> 6D05
17 ..(Eight Random Bytes).. 97 ..(246 bytes of "0x00").. ---> OK
0C ..(Eight Random Bytes).. 0C ..(246 bytes of "0x00").. ---> OK
D3 ..(Eight Random Bytes).. 91 ..(246 bytes of "0x00").. ---> 6D05
86 ..(Eight Random Bytes).. E2 ..(246 bytes of "0x00").. ---> OK
C2 ..(Eight Random Bytes).. 23 ..(246 bytes of "0x00").. ---> 6D05
有人知道我的小程序有什么问题吗?
【问题讨论】:
-
你使用什么 ALG_?
-
@vojta
asymCipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);
标签: java cryptography rsa javacard