Java Card中的ALG_RSA_NOPAD

Rao*_*722 6 java cryptography javacard

为了有效地计算Java Card中的正方形,我想使用ALG_RSA_NOPAD指数等于2和模数大于预期结果的算法(因此模块化减少没有影响).

但我无法使用该算法ALG_RSA_NOPAD.事实上,当我打电话的方法doFinal(),我收到了CryptoException这是ILLEGAL_VALUE.在Java Card 2.2.2规范中,它说:

CryptoException.ILLEGAL_USE如果满足以下条件之一:

•此Cipher算法不填充消息,并且消息不是块对齐的.

•此Cipher算法不填充消息,并且inBuff或update()方法中未提供输入数据.

•不支持输入消息长度.

•解密数据不受适当填充字节的限制.

所以我得出结论,我的消息不是块对齐的.但是这个算法的块对齐意味着什么呢?我的信息的长度是否与模数相同?指数?我尝试了不同的东西,但我没有找到......

相应的代码:

   byte[] res_RSA = new byte[(short) 0x0080];
   KeyPair rsa_KeyPair = new   KeyPair(KeyPair.ALG_RSA,KeyBuilder.LENGTH_RSA_1024);
        rsa_KeyPair.genKeyPair();
   RSAPublicKey rsa_PubKey; rsa_PubKey = (RSAPublicKey) rsa_KeyPair.getPublic();


    rsa_PubKey.setExponent(new byte[]{(byte) 0x02}, (short) 0x00000, (short) 0x0001);
    rsa_PubKey.setModulus(new byte[] { (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
        (byte) 0xFF, (byte) 0xFF, }, (short) 0x0000, (short) 0x0080);

    cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);

    x = new byte[] { (byte) 0x0C, (byte) 0xE2, (byte) 0x65, (byte) 0x92,
        (byte) 0x98, (byte) 0x84, (byte) 0x4C, (byte) 0x6C,
        (byte) 0x39, (byte) 0x31, (byte) 0x78, (byte) 0x22,
        (byte) 0x99, (byte) 0x39, (byte) 0xAD, (byte) 0xAD,
        (byte) 0x74, (byte) 0x31, (byte) 0x45, (byte) 0xD2,
        (byte) 0xB9, (byte) 0x37, (byte) 0xB2, (byte) 0x92,
        (byte) 0x7D, (byte) 0x32, (byte) 0xE9, (byte) 0x70,
        (byte) 0x91, (byte) 0x7D, (byte) 0x78, (byte) 0x45,
        (byte) 0xC9, (byte) 0x5C, (byte) 0xF9, (byte) 0xF2,
        (byte) 0xFD, (byte) 0xB9, (byte) 0xAE, (byte) 0x6C,
        (byte) 0xC9, (byte) 0x42, (byte) 0x64, (byte) 0xBA,
        (byte) 0x2A, (byte) 0xCE, (byte) 0x5A, (byte) 0x71,
        (byte) 0x60, (byte) 0x58, (byte) 0x56, (byte) 0x17,
        (byte) 0x2E, (byte) 0x25, (byte) 0xDD, (byte) 0x47,
        (byte) 0x23, (byte) 0x6B, (byte) 0x15, (byte) 0x76,
        (byte) 0x8F, (byte) 0x2A, (byte) 0x87, (byte) 0xC7 };

   cipherRSA.init(rsa_PubKey, Cipher.MODE_ENCRYPT);
   cipherRSA.doFinal(x, (short) 0x0000,
            (short) 0x0040, res_RSA, (short) 0x0000);
Run Code Online (Sandbox Code Playgroud)

所以CryptoException在最后一行提出,但我真的不明白为什么.

(注意,在我的代码中,我将模数设置为128字节长度的最大值,以确保方形不会受到影响.)

Rao*_*722 3

好吧,在我的卡上,消息的长度必须与模数相同......我以为我测试了这种情况,但我的偏移量不匹配。

因此,即使我的消息没有模数那么长(我无法计算平方),我也必须用零填充数组。