Forum Stats

  • 3,837,402 Users
  • 2,262,255 Discussions
  • 7,900,269 Comments

Discussions

how to set RSA public key in java card?

abdou-boulegh
abdou-boulegh Member Posts: 14
edited May 14, 2014 6:55AM in Cryptography

Hello, I think I found the source of the problem and i think i can now ask for help in a mor effective way.

I'm using java card 2.2.2 with Rmi, i'm Trying to generate an RSA public key on a desktop app then send it to the java card applet, and this is the code i'm using on my smart card :

    public class CompteurImpl extends CardRemoteObject implements ICompteur {

   

       

    short expo;

    private byte[] ret = new byte[67];

    private byte[] R1 = new byte[64];

    private byte[] R2 = new byte[8];

    private byte[] pinSig = new byte[64];

    private byte[] xor = new byte[8];

    private byte[] cipherText = new byte[64];

    private short challenge = 0;

    private RSAPublicKey rsa_PublicKey;

    private Cipher cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);

    private KeyPair rsa_KeyPair= new KeyPair( KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_512);

    private Signature sig = Signature.getInstance(Signature.ALG_RSA_SHA_PKCS1, false);

   

    RSAPrivateKey priv ;

   

   

  

   

   

    public byte[] getR1(){

       

        rsa_KeyPair.genKeyPair();

        priv =  (RSAPrivateKey) rsa_KeyPair.getPrivate();

        priv.getModulus(R1, (short)0);

       

        return R1;

    }

   

   

   

    /* (non-Javadoc)

     * @see sid2.ICompteurr#verify(byte[])

     */

    public short verify (byte[] pin){

       

       

        sig.init(rsa_PublicKey, Signature.MODE_VERIFY);

        if( sig.verify(pin, (short)0, (short)pin.length, pinSig, (short)0, (short)pinSig.length)){

            return 1;

        }else{

            return 0;

        }

    }

   

    /* (non-Javadoc)

     * @see sid2.ICompteurr#setXor(byte[])

     */

    public void setXor( byte[] xor){

       

        javacard.framework.Util.arrayCopy(xor, (short) 0,

                this.xor, (short) 0, (short) 8);

       

    }

   

   

    /* (non-Javadoc)

     * @see sid2.ICompteurr#setpinSig(byte[])

     */

    public void setpinSig( byte[] sig){

           

        javacard.framework.Util.arrayCopy(sig, (short) 0,

                pinSig, (short) 0, (short) 64);

       

    }

   

    /* (non-Javadoc)

     * @see sid2.ICompteurr#encrypt(byte[])

     */

    public byte[] encrypt(byte[] clearText){

       

        cipherRSA.init(rsa_PublicKey, Cipher.MODE_ENCRYPT);

        cipherRSA.doFinal(clearText, (short)0,  (short)clearText.length,cipherText, (short)0 );

        return cipherText;

    }

   

   

    /* (non-Javadoc)

     * @see sid2.ICompteurr#setPub(byte[], byte[])

     */

    public void setPub(byte[] expo,byte[] mod){

        rsa_PublicKey.clearKey();

       

       

        rsa_PublicKey.setExponent(expo, (short)0, (short)expo.length);

        rsa_PublicKey.setModulus(mod, (short)0, (short)mod.length);

    }

   

   

   

    /* (non-Javadoc)

     * @see sid2.ICompteurr#getR1()

     */

    //public byte[] getR1(){

       

       

        //r1.generateData(R1, (short)0, (short)8);

        //return R1;

    //}

   

    /* (non-Javadoc)

     * @see sid2.ICompteurr#verifyR1(byte[])

     */

    public short verifyR1(byte[] R1sign){

       

        return verify(R1sign);

                               

    }

   

    public byte[] getPub(){

       

        short length = rsa_PublicKey.getExponent(ret, (short)0);

        rsa_PublicKey.getModulus(ret, length);

        return ret;

    }

   

   

    /* (non-Javadoc)

     * @see sid2.ICompteurr#initialiser(byte)

     */

    public void initialiser(byte v) throws RemoteException, UserException {

       

       

        rsa_KeyPair.genKeyPair();

        rsa_PublicKey = (RSAPublicKey) rsa_KeyPair.getPublic();

              

       

       

    }

    /* (non-Javadoc)

     * @see sid2.ICompteurr#getCipherText()

     */

    public byte[] getCipherText() {

        return cipherText;

    }

    /* (non-Javadoc)

     * @see sid2.ICompteurr#setCipherText(byte[])

     */

    public void setCipherText(byte[] cipherText) {

        this.cipherText = cipherText;

    }

    /* (non-Javadoc)

     * @see sid2.ICompteurr#getChallenge()

     */

    public short getChallenge() {

        return challenge;

    }

    /* (non-Javadoc)

     * @see sid2.ICompteurr#setChallenge(short)

     */

    public void setChallenge(short challenge) {

        this.challenge = challenge;

    }

    /* (non-Javadoc)

     * @see sid2.ICompteurr#getR2()

     */

    public byte[] getR2() {

        return R2;

    }

    /* (non-Javadoc)

     * @see sid2.ICompteurr#setR2(byte[])

     */

    public void setR2(byte[] r2) {

        R2 = r2;

    }

   

   

   

    }

I think my problem relies withit the setPub method, or the way i'm handling my keypair, everytime i try deploying a key on the card i always end up with the same value. when i try to decrypt a value encrypted by the smart card's public key i get the following exception :

    BadPaddingException : Data must start with zero

what is the best way to generate export a public key to a java card with it's modulus and exponent ?

This discussion has been closed.