?? rsasigtest.java
字號:
package MIDlets;import java.io.*;import java.util.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;import javax.microedition.rms.*;//import XMLDsig.*;import org.kxml.*;import org.kxml.parser.*;import org.kxml.kdom.*;public class RSASigTest extends MIDlet implements CommandListener { private Display display; // Three on-screen command buttons. private Command cancelCommand; private Command doneCommand; private Command fetchCommand; // TextField allows the user to input URL. private TextField textField; // The fetch truncates after 2048 bytes to avoid using // too much memory. private int length = 2048; public RSASigTest() throws Exception { // Initialize display and command objects. display = Display.getDisplay(this); cancelCommand = new Command("CANCEL", Command.CANCEL, 1); doneCommand = new Command("DONE", Command.CANCEL, 1); fetchCommand = new Command("FETCH", Command.SCREEN, 1); } /** * Ask for URL in the initial screen. **/ public void startApp() { // Form with TextField to ask for URL. Form form = new Form("RSA Sig Test"); textField = new TextField("URL:", "", 80, TextField.ANY); form.append( textField ); // Two buttons on the form. form.addCommand(cancelCommand); form.addCommand(fetchCommand); // The CommandListener is this MIDlet itself. form.setCommandListener( (CommandListener) this); // Display the form on the LCD screen. display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } /** * Handle events generated by the Commands in the GUI. **/ public void commandAction(Command command, Displayable screen) { if (command == cancelCommand) { // Quit this MIDlet if "CANCEL" button is pressed. destroyApp(false); notifyDestroyed(); } else if (command == doneCommand) { // "DONE" is the same as "CANCEL" destroyApp(false); notifyDestroyed(); } else if (command == fetchCommand) { try { // Get the target URL from the TextField. String url = "http://" + textField.getString(); url = url.trim(); HttpConnection conn = (HttpConnection) Connector.open( url ); conn.setRequestMethod(HttpConnection.GET); // Get the response stream from the server. DataInputStream is = conn.openDataInputStream(); InputStreamReader reader = new InputStreamReader(is); XmlParser parser = new XmlParser (reader); Document doc = new Document(); doc.parse(parser); Element signedMesg = doc.getRootElement(); String mesg = signedMesg.getElement("mesg").getText(); String digest = signedMesg.getElement("Signature").getElement("SignedInfo").getElement("DigestValue").getText(); String mod = signedMesg.getElement("Signature").getElement("KeyInfo").getElement("KeyValue").getElement("RSAKeyValue").getElement("Modulus").getText(); String pubExp = signedMesg.getElement("Signature").getElement("KeyInfo").getElement("KeyValue").getElement("RSAKeyValue").getElement("Exponent").getText(); String signature = signedMesg.getElement("Signature").getElement("SignatureValue").getText(); // Close response stream. is.close(); String displayMesg = mesg;// if ( digest.equals(RSASigUtil.getDigest(mesg)) ) {// displayMesg = displayMesg + "\nDigest is verified";// if ( RSASigUtil.verify(mesg, signature, mod, pubExp) ) {// displayMesg = displayMesg + "\nSignature is verified";// } else {// displayMesg = displayMesg + "\nSignature is failed";// }// } else {// displayMesg = displayMesg + "\nDigest is failed";// } TextBox textBox = new TextBox("Message", null, length, TextField.ANY); textBox.setString( displayMesg ); textBox.addCommand(doneCommand); textBox.setCommandListener( (CommandListener) this); display.setCurrent(textBox); conn.close(); } catch (Exception e) {// Alert alert = new Alert("Error",// "An Error has Occured!",// null,// AlertType.ERROR);// alert.setTimeout(Alert.FOREVER);// display.setCurrent(alert); System.out.print(e.getMessage()); e.printStackTrace(); } } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -