Cryptography 如何使用RSA加密为Java卡上的数字供电

Cryptography 如何使用RSA加密为Java卡上的数字供电,cryptography,rsa,smartcard,javacard,diffie-hellman,Cryptography,Rsa,Smartcard,Javacard,Diffie Hellman,我是Java卡开发方面的新手,我尝试在Java卡上实现NAXOS协议,我的问题是给变量加电。我的JavaCard版本是2.2.1,我使用这样的代码: package RsaEncryption; import javacard.framework.*; import javacard.security.KeyBuilder; import javacard.security.RSAPrivateKey; import javacardx.crypto.Cipher; public class

我是Java卡开发方面的新手,我尝试在Java卡上实现NAXOS协议,我的问题是给变量加电。我的JavaCard版本是2.2.1,我使用这样的代码:

package RsaEncryption;

import javacard.framework.*;
import javacard.security.KeyBuilder;
import javacard.security.RSAPrivateKey;
import javacardx.crypto.Cipher;

public class RsaEncryption extends Applet {

    static final byte PLAIN_CLA = (byte) 0x00;
    private RSAPrivateKey privKey;
    Cipher cipher;
    public static void install(byte[] bArray,short bOffset,byte bLength) {
        new RsaEncryption(bArray, bOffset, bLength);
    }

    private RsaEncryption(byte[] bArray, short bOffset, byte bLength){
        register();
    }

    public boolean select() {
        return true;
    }

    public void deselect() {
    }


    public void process(APDU apdu) {
        if (selectingApplet()) {
            return;
        }
        byte[] buffer = apdu.getBuffer();
        apdu.setIncomingAndReceive();
        short lenOfData = (short)(buffer[ISO7816.OFFSET_LC]);
        byte[] tmp = new byte[lenOfData];




        privKey = (RSAPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE, KeyBuilder.LENGTH_RSA_1024, false);
        cipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);

        byte[] G = {0x02};
        byte[] P = {0x05};
        byte[] x = {0x03};

        short maxL = 256;
        privKey.setModulus(P, (short)0, maxL);
        privKey.setExponent(x, (short)0, maxL);

        cipher.init(privKey, Cipher.MODE_DECRYPT);

        // Execute G^x mod P using RSA's decrypt
        cipher.doFinal(G, (short) 0, maxL, tmp, (short) 0);


      // tmp[2] = 0x5;
        //buffer[6] = tmp[0];
       // buffer[7] = tmp[1];

        //Util.arrayCopyNonAtomic(buffer, ISO7816.OFFSET_CDATA, tmp, (short)0, (short)tmp.length);

        for(short i=(short)0; i<lenOfData;i++){
          buffer[i]= tmp[(short)(i)];
        }
        //apdu.sendBytesLong(tmp, (short)0, (short)5);
        apdu.setOutgoingAndSend((short) 0, (short)tmp.length);
    }

}
包加密;
导入javacard.framework.*;
导入javacard.security.KeyBuilder;
导入javacard.security.RSAPrivateKey;
导入javacardx.crypto.Cipher;
公共类加密扩展了Applet{
静态最终字节PLAIN_CLA=(字节)0x00;
私钥私钥私钥;
密码;
公共静态无效安装(字节[]bArray,短bOffset,字节bLength){
新的RSA加密(bArray、bOffset、bLength);
}
专用RSA加密(字节[]bArray,短bOffset,字节bLength){
寄存器();
}
公共布尔选择(){
返回true;
}
公共无效取消选择(){
}
公共作废流程(APDU APDU){
如果(选择Applet()){
返回;
}
byte[]buffer=apdu.getBuffer();
apdu.setIncomingAndReceive();
短lenOfData=(短)(缓冲区[ISO7816.OFFSET_LC]);
字节[]tmp=新字节[lenOfData];
privKey=(rsaprovatekey)KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE,KeyBuilder.LENGTH_RSA_1024,false);
cipher=cipher.getInstance(cipher.ALG_RSA_NOPAD,false);
字节[]G={0x02};
字节[]P={0x05};
字节[]x={0x03};
短maxL=256;
设置模数(P,(短)0,最大值);
privKey.setExponent(x,(短)0,maxL);
cipher.init(privKey,cipher.MODE_DECRYPT);
//使用RSA的decrypt执行G^x mod P
密码doFinal(G,(short)0,maxL,tmp,(short)0);
//tmp[2]=0x5;
//缓冲区[6]=tmp[0];
//缓冲区[7]=tmp[1];
//Util.arrayCopyNonAtomic(缓冲区,ISO7816.OFFSET\U CDATA,tmp,(短)0,(短)tmp.length);
对于(短i=(短)0;i 00A4040007A000002471201
包装命令-->00A4040007A000002471201
响应800000009010203040506070809FF
已包装命令-->800000009010203040506070809FF

响应尝试Diffie-Hellman原语可能会更好。Diffie-Hellman的模幂运算与RSA的模幂运算相比,不太可能受到其他约束的影响


在这两种情况下,您当然会受到模块化算法的约束——原因很明显。

调用
setmodule()
setExponent()
使用无效长度(
maxL
256
,数组的长度
1
)考虑使用<代码> G ,<代码> P<代码>代码>代码> x/COD> 128字节-并使用非常大的模数来获得正确的结果(您的G^ x将是模5,您可能不想要)…祝你好运!谢谢你的回复,我将maxL变量改为1,并尝试计算简单的数字,即2为3的幂,但仍然不起作用。如果你尝试128字节长的
G
P
x
数组,谢谢你,它成功了,由于RSA的实现,我成功地将一个数字增加到另一个。你可以st taht作为一个答案,所以我可以接受。顺便说一句。我注意到,当基数不同于2时,它不起作用。有什么建议吗?谢谢你的回答,我相信你是对的,DH也会做这项工作,但我设法使用RSA实现为两个数字加电。+1建议使用DH协议来做这件事。太好了,很高兴你解决了这个问题。Be s你也来分享你的答案吧!
mode_211
enable_trace
establish_context
card_connect -readerNumber 1
select -AID A0000002471201
Command --> 00A4040007A0000002471201
Wrapped command --> 00A4040007A0000002471201
Response <-- 9000
send_apdu -APDU 8000000009010203040506070809FF
Command --> 8000000009010203040506070809FF
Wrapped command --> 8000000009010203040506070809FF
Response <-- 6F00
send_APDU() returns 0x80206F00 (Unknown ISO7816 error: 0x6F00)