📄 dsasigtest.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 DSASigTest 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 DSASigTest() 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("DSA 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 key_g = signedMesg.getElement("Signature").getElement("KeyInfo").getElement("KeyValue").getElement("DSAKeyValue").getElement("G").getText(); String key_p = signedMesg.getElement("Signature").getElement("KeyInfo").getElement("KeyValue").getElement("DSAKeyValue").getElement("P").getText(); String key_q = signedMesg.getElement("Signature").getElement("KeyInfo").getElement("KeyValue").getElement("DSAKeyValue").getElement("Q").getText(); String key_y = signedMesg.getElement("Signature").getElement("KeyInfo").getElement("KeyValue").getElement("DSAKeyValue").getElement("Y").getText(); String sig_r = signedMesg.getElement("Signature").getElement("SignatureValue").getElement("R").getText(); String sig_s = signedMesg.getElement("Signature").getElement("SignatureValue").getElement("S").getText(); // Close response stream. is.close();// if ( digest.equals(DSASigUtil.getDigest(mesg)) ) {// mesg = mesg + "\nDigest is verified";// if ( DSASigUtil.verify(digest, sig_r, sig_s,// key_g, key_p, key_q, key_y) ) {// mesg = mesg + "\nSignature is verified";// } else {// mesg = mesg + "\nSignature is failed";// }// } else {// mesg = mesg + "\nDigest is failed";// } TextBox textBox = new TextBox("Message", null, length, TextField.ANY); textBox.setString( mesg ); textBox.addCommand(doneCommand); textBox.setCommandListener( (CommandListener) this); display.setCurrent(textBox); conn.close(); } catch (Exception e) { System.out.print(e.getMessage()); e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -