8 Replies Latest reply on May 25, 2011 5:47 PM by safarmer

    RSA encryption error

    864325
      Hi everybody,

      I'm a student who is programming on Java card (Javacard 2.2.2 with cref) for a master thesis. here is my problem: I try to write an exponential modular method on the javacard but it isn't working! Here is my method:
      public static byte[] modPow(byte[] base, byte[] pow, byte[] mod){
           byte[] result = new byte[mod.length];
                
           // generate own rsa_keypair
           KeyPair rsa_KeyPair;
           RSAPublicKey rsa_PublicKey;
           Cipher cipherRSA;
              
              //generate the key (e, n) such that c = m^e mod n
              rsa_KeyPair= new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1024);
              rsa_PublicKey = (RSAPublicKey) rsa_KeyPair.getPublic();
              rsa_PublicKey.setExponent(pow, (short)0, (short)pow.length);
              rsa_PublicKey.setModulus(mod, (short)0, (short)mod.length);
              
              //prepare the modPow operation
              cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, false);
              cipherRSA.init(rsa_PublicKey, Cipher.MODE_ENCRYPT);
              
              //perform the modPow
              cipherRSA.doFinal(base, (short)0, (short)base.length, result, (short) 0);
              
              return result;
              
      }
      So, I try just
      public static byte[] modPow(byte[] base, byte[] pow, byte[] mod){
           byte[] result = new byte[mod.length];
                
           // generate own rsa_keypair
           KeyPair rsa_KeyPair;
              
              //generate the key (e, n) such that c = m^e mod n
              rsa_KeyPair= new KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1024);
              
              return result;
              
      }
      and it's not working at all :-(

      Do you have an idea?

      Thank you

      PS: I don't have the JCOp tools, so it's very annoying to debug the applet every time. I don't have it because I don't know where to download it and activate it.

      Edited by: 861322 on May 25, 2011 7:41 PM
        • 1. Re: RSA encryption error
          Sebastien_Lorquet
          A keypair is just an empty shell.

          you either have to use genKeyPair() or use the constructor with existing keys, or use setPublic&al.

          If you just need a public or private key, don't use a keypair.

          ciphering algorithms dont't use keypairs but keys. The sole purpose of this class is to support keypair generation in a clean way.

          PS: you don't need NXP proprietary tools to develop javacard code (add debug commands and SWs - this is what I call printf debugging), and you don't need to subtlely ask where to downloadcrack it ;)
          • 2. Re: RSA encryption error
            859723
            Another problem with your code is, that each time you call the modPow() method you will create a new KeyPair Object and a new byte array. Calling this method repeatedly will make you run out of memory very fast.
            • 3. Re: RSA encryption error
              864325
              ok but if you check my second try, just create an empty shell didn't work.

              I try the following code as well, and it don't work:
              public class Crypto {
                   
              
                  static RSAPublicKey rsa_PublicKey;
              
                   
                   
                   public static byte[] modPow(byte[] base, byte[] pow, byte[] mod){
                        byte[] result = new byte[mod.length];
                      
                             //generate the key (e, n) such that c = m^e mod n
                          rsa_PublicKey = (RSAPublicKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PUBLIC, KeyBuilder.LENGTH_RSA_1024, false);
                      
                            return result;
                      
                   }
                   
              
              }
              PS: Is there any code markup? I just found the code markup ;-)
              PPS: Excuse me but I just tell that I don't use JCOP, it is all. How can I do printf debugging on the applet side? to do you have an example?

              Edited by: 861322 on May 25, 2011 7:42 PM
              • 4. Re: RSA encryption error
                864325
                The code above works if I replace KeyBuilder.LENGTH_RSA_1024 by KeyBuilder.LENGTH_RSA_512.

                why?
                • 5. Re: RSA encryption error
                  safarmer
                  You need to read the documentation that comes with the JCDK. There is a PDF that lists the cryptographic algorithms supported by the emulators in the JCDK. Only 512bit RSA keys are supported by CREF and JCWDE.

                  Cheers,
                  Shane
                  • 6. Re: RSA encryption error
                    safarmer
                    JCWDE also runs in a Java SE JVM so you can debug with Eclipse as long as you start the emulator with the standard java debug flags. Then you can step through your code in any version of Eclipse or NetBeans.

                    Cheers,
                    Shane
                    • 7. Re: RSA encryption error
                      864325
                      I'm currently using Eclipse and I have JCWDE installed on it. How can I perform this debug? Is it complicated to configure?

                      Thanks a lot.
                      • 8. Re: RSA encryption error
                        safarmer
                        If you are using the Eclipse plugin, you can check the user guide: http://eclipse-jcde.sourceforge.net/user-guide.htm

                        In the JCWDE menu there is a Debug option.

                        Cheers,
                        Shane