3

i want to develop a plugin using gwt. It has to use java.security.* for the key generation on client side. i have made all requirement But it is showing following error.

Loading modules

coreservlets.GwtApp1

Loading inherited module 'coreservlets.GwtApp1'
    Loading inherited module 'java.security.KeyPair'
       [ERROR] Unable to find 'java/security/KeyPair.gwt.xml' on your classpath; >could be a typo, or maybe you forgot to include a classpath entry for source?
    [ERROR] Line 15: Unexpected exception while processing element 'inherits'

i have inherited all the related class like "java.security.KeyPair" in my gwtapp1.gwt.xml file

also i included jar in classpath itself.but still the error has not gone. what should i do.plz suggest here is my java code

package coreservlets.client;

import java.io.UnsupportedEncodingException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;


public class Keygen {

private PrivateKey privKey;
private PublicKey pubKey;
private static Keygen keygen = null;

private Keygen() {

}

public static Keygen getInstance() {

    if (keygen == null) {
        keygen = new Keygen();
    }

    return keygen;
}

public void KeyGenerator(String ALGORITHAM) {
    KeyPairGenerator keyGen = null;
    SecureRandom random = null;
    try {
        keyGen = KeyPairGenerator.getInstance(ALGORITHAM);
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    }
    try {
        random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        //random = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();

    } catch (NoSuchProviderException ex) {
        ex.printStackTrace();
    }

    //keyGen.initialize(1024, random);
    keyGen.initialize(1024);
    KeyPair key = keyGen.generateKeyPair();
    privKey = key.getPrivate();
    pubKey = key.getPublic();

}

public String getPubKeyasString() {
    //return Base64.encodeBase64String(pubKey.getEncoded());
    try {
        return new String(pubKey.getEncoded(),"ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
public String getPriKeyasString() {
    //return Base64.encodeBase64String(privKey.getEncoded());
    try {
        return new String(privKey.getEncoded(),"ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

}

Community
  • 1
  • 1
powerPlug
  • 41
  • 1
  • 6

2 Answers2

2

For key generation you can use the gwt-crypto library, but be prepared to certain performance issues and unsupported features.

[Edit] Some time ago, I did success wrapping a pure js rsa solution using jsni. The js I took was the jsbn.js library

Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27
  • thanks for suggestion but i dont have to use gwt-chrypto lib.do you have any other possible way to sort it out. – powerPlug Nov 21 '12 at 04:07
  • Unfortunately java.security is not emulated in gwt-jre, so you would not be able to use those classes although you created a module including the original java files (from sun/oracle). I have edited my response to include a jsni possible solution. – Manolo Carrasco Moñino Nov 21 '12 at 08:11
  • that can be my option too.On the whole what i have to do is i have to develop browser plugin which on installation will generate keys on client side and public key will be send to server which will encrypt the file and that encrypted file will be decrypted by my private key on client side.one possible way i am doing is generating keys using gwt in that above problem is occuring.so i need suggestion what i should do,also is their any other way to generate public key which should work on server side algo,as its not working by generating keys using js.i am new to gwt.thanks again for your reply. – powerPlug Nov 21 '12 at 09:05
  • keys generated with jsbn should work in server side using openssl, so I guess that you can use javax.security in server since it is compatible with openssl. Before wrapping jsbn you can play with the demo page in theirs site (http://www-cs-students.stanford.edu/~tjw/jsbn/rsa.html) generating keys and messages and trying to decrypt/encrypt with your server java implementation. Once you have that working, you can work in client wrapping the js methods you needed – Manolo Carrasco Moñino Nov 21 '12 at 17:08
  • @ManoloCarrascoMoñino Could you please help me with this related question? http://stackoverflow.com/questions/25777287/how-do-i-use-rsa-between-server-and-gwt-client – Michael Sep 11 '14 at 00:44
  • @ManoloCarrascoMoñino Do you use jsbn.js with a gwt-crypto backend? – Michael Sep 25 '14 at 14:44
  • Yes finally I used jsbn in my project without lib-crypto because I had to create keys in client side. In server side I used bouncycastle. If your keys are generated in server, you could use lib-crypto I did succeeded in my research. This was a couple of years ago though. – Manolo Carrasco Moñino Sep 28 '14 at 20:27
0

In a .gwt.xml file <inherits ...> refers to another .gwt.xml file. I suspect that you have used it like a java import ... instead?

What you should do, if you want to use java.security.KeyPair clientside in GWT is to ascertain that the source is available to the GWT compiler. This is done using a <source path="..." /> entry in the .gwt.xml file.

You may want to visit the GWT JRE Emulation Reference page, where sadly you will see that none of the javax.* packages are supported, so good luck with your project... Perhaps you should keep the code using KeyPair on the server side only?

Hope that helps.

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • thanks for your help, actually i am new to gwt.i have seen various examples of developing gwt app like [this example](http://www.vogella.com/articles/GWTModules/article.html) but unable to get how to use java.security.* jar.could you please explain me more about it.thanks again – powerPlug Nov 21 '12 at 04:05
  • The GWT compiler needs the source code for each and every class that goes in the client. You may be able to extract the `KeyPair` (and related/referenced classes!) from `src.zip` in your JDK and make that work. – Anders R. Bystrup Nov 21 '12 at 08:52