问题
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;
}
}
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/13474048/how-to-import-java-security-in-my-gwt-application