package gr.cript; import java.applet.Applet; import gr.cript.jni.*; import gr.cript.jdirect.*; import netscape.javascript.*; public class JCript implements JCriptHighLevelInterface { public static final int IEXPLORER = 24; public static final int STANDARD = 34; public static final int NETSCAPE = 98; private JCriptLowLevelInterface cr; public JCript() { cr = new JniCriptStandard(); } public JCript(Applet ap) { switch (getBrowserCode(ap)) { case IEXPLORER: { cr = new JDirectCript(); return; } case STANDARD: { cr = new JniCriptStandard(); return; } case NETSCAPE: { cr = new JniCriptNetscape(); return; } default: { cr = new JniCriptStandard(); return; } } } public void changePassword(String oldPwd, String newPwd) throws CriptException { int res=cr.changePassword(oldPwd,newPwd); if (res!=0) throw new CriptException(CriptException.solve(res)); } public DecryptResult decrypt(String pwd, JBuffer in) throws CriptException { DecryptResult ds = new DecryptResult(); int res = cr.decrypt(pwd,in,ds); if (res!=0) { throw new CriptException(CriptException.solve(res)); } return ds; } public JBuffer encrypt(Algorithm alg, JDn dn, JBuffer inBuf) throws CriptException { JBuffer outBuf=new JBuffer(); int res = cr.encrypt(alg.intValue(),dn,inBuf,outBuf); if (res!=0) { throw new CriptException(CriptException.solve(res)); } return outBuf; } private static int getBrowserCode(Applet param) { try { JSObject w=(JSObject)JSObject.getWindow(param); JSObject nav=(JSObject)w.getMember("navigator"); String appName=(String)nav.getMember("appName"); if (appName.equals("Netscape")) return JCript.NETSCAPE; if (appName.equals("Microsoft Internet Explorer")) return JCript.IEXPLORER; return JCript.STANDARD; } catch (Throwable js) { js.printStackTrace(); return JCript.STANDARD; } } public JDn login(String pwd) throws CriptException { JDn dn=new JDn(); int res=cr.login(pwd,dn); if (res!=0) { throw new CriptException(CriptException.solve(res)); } return dn; } public JBuffer sign(Algorithm alg, String pwd, JBuffer inBuf) throws CriptException { JBuffer outBuf=new JBuffer(); int res = cr.sign(alg.intValue(),pwd,inBuf,outBuf); if (res!=0) { throw new CriptException(CriptException.solve(res)); } return outBuf; } public VerifyResult verify(JBuffer inBuf) throws CriptException { VerifyResult vs = new VerifyResult(); int res = cr.verify(inBuf,vs); if (res!=0) { throw new CriptException(CriptException.solve(res)); } return vs; } }