1. Trang chủ
  2. » Giáo Dục - Đào Tạo

APJII lab 05

7 49 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 7
Dung lượng 122,15 KB

Nội dung

SJava-Lab5- Digital Signature Working with Database and Security in Java Module 8: Digital Signature Module 9: ClassNote XML Signature Lab Guide for Lab5 Session Objectives In this session, you will be practicing with  How to create Digital Signature  How to verify Digital Signature Part – Getting started (30 minutes) Create GUI as the following: Type the code below to add some functions This exercise will more help you understand about signature and verify signature © 2011 FPT-Aptech Page / SJava-Lab5- Digital Signature public void doGenerateSignature() { try { KeyPairGenerator g = KeyPairGenerator.getInstance("DSA","SUN"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG","SUN"); g.initialize(1024,random); KeyPair keys = g.genKeyPair(); PublicKey publickey = keys.getPublic(); PrivateKey privatekey = keys.getPrivate(); java.security.Signature asign = java.security.Signature.getInstance("SHA1withDSA","SUN"); asign.initSign(privatekey); FileInputStream fin = new FileInputStream(fsign); byte [] data = new byte[1024]; while(fin.available()!=0) { int len = fin.read(data); asign.update(data,0,len); } fin.close(); FileOutputStream fout = null; fout = new FileOutputStream(fpublic); fout.write(publickey.getEncoded()); fout.close(); fout = new FileOutputStream(fsigned); fout.write(asign.sign()); fout.close(); JOptionPane.showMessageDialog(null,"Sign to successful","Signature",JOptionPane.INFORMATION_MESSAGE); } catch(Exception e) { System.out.println(e); } } public void doVerifySignature() { try { FileInputStream fin = new FileInputStream(fpublic); byte [] encodekey = new byte[fin.available()]; fin.read(encodekey); fin.close(); X509EncodedKeySpec spec = new X509EncodedKeySpec(encodekey); KeyFactory keys = KeyFactory.getInstance("DSA","SUN"); PublicKey publickey = keys.generatePublic(spec); java.security.Signature asign = java.security.Signature.getInstance("SHA1withDSA","SUN"); © 2011 FPT-Aptech Page / SJava-Lab5- Digital Signature asign.initVerify(publickey); fin = new FileInputStream(fsigned); byte []signtoverify = new byte[fin.available()]; fin.read(signtoverify); fin.close(); fin = new FileInputStream(fsign); byte [] b = new byte[1024]; while(fin.available()!=0) { int len = fin.read(b); asign.update(b,0,len); } fin.close(); boolean isOk ; isOk = asign.verify(signtoverify); if(isOk) JOptionPane.showMessageDialog(null,"Verifysignatute successful","Signature",JOptionPane.INFORMATION_MESSAGE); else JOptionPane.showMessageDialog(null,"Verifysignature unsuccessful “,"Signature",JOptionPane.INFORMATION_MESSAGE); } catch(Exception e) { System.out.println(e); } } Create a class named BookOrder.java and copy the follow code to its source code Run and observe the result Uncomment lines updateOrder() and XMLStreamTest() in main() method and run again to observe result import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLXML; import java.sql.Statement; import javax.xml.stream.FactoryConfigurationError; © 2011 FPT-Aptech Page / SJava-Lab5- Digital Signature import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; /** * * @author FPT-Aptech Teacher */ public class BookOrder { static String jdbcUrl = "jdbc:sqlserver://localhost;databaseName=test;"; static String jdbcUser = "sa"; static String jdbcPassword = "password"; /** * @param args the command line arguments */ public static void main(String[] args) { createAndInsert(); showOrderDetail(); //updateOrder(); //XMLStreamTest(); } public static void createAndInsert() { try { Connection cn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); Statement stmtDetails = cn.createStatement(); String strSQL = "if exists(select * from sys.tables where name = 'Invoice')"+ "drop table Invoice;"+ "Create table Invoice(Id int, OrderDetails xml);"; stmtDetails.execute(strSQL); SQLXML order = cn.createSQLXML(); order.setString("" + "" + "" + "Book" + "5" + "15.95" + "" + "" + "DVD Player" + "3" + "103.95" + "" © 2011 FPT-Aptech Page / SJava-Lab5- Digital Signature + "" + ""); String SQLString = "Insert into Invoice(Id, OrderDetails) values(?,?)"; PreparedStatement pstDetails = cn.prepareStatement(SQLString); pstDetails.setInt(1, 1000); pstDetails.setSQLXML(2, order); pstDetails.executeUpdate(); order.free(); } catch (SQLException ex) { ex.printStackTrace(); } } public static void showOrderDetail() { try { Connection cn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); Statement stmtDetails = cn.createStatement(); ResultSet rsDetails = stmtDetails.executeQuery("SELECT * FROM Invoice"); while (rsDetails.next()) { SQLXML order = rsDetails.getSQLXML(2); System.out.println(order.getString()); } } catch (SQLException ex) { ex.printStackTrace(); } } public static void updateOrder() { try { Connection cn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); Statement stmtDetails = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); SQLXML order = cn.createSQLXML(); order.setString("" + "" + "" + "MP3 Player" + "3" + "12.45" + "" + "" + ""); ResultSet rsDetails = stmtDetails.executeQuery("SELECT * FROM Invoice"); if (rsDetails.next()) { rsDetails.updateSQLXML(2, order); rsDetails.updateRow(); } else { System.out.println("There isn't row to update"); } } catch (SQLException ex) { ex.printStackTrace(); } © 2011 FPT-Aptech Page / SJava-Lab5- Digital Signature } public static void XMLStreamTest() { try { Connection cn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); Statement stmtDetails = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rsDetails = stmtDetails.executeQuery("Select * from Invoice"); rsDetails.next(); SQLXML sqlXML = rsDetails.getSQLXML(2); InputStream bStream = sqlXML.getBinaryStream(); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = factory.createXMLStreamReader(bStream); while (streamReader.hasNext()) { int parseEvent = streamReader.next(); if(parseEvent == XMLStreamReader.START_ELEMENT) System.out.println(""); if(parseEvent==XMLStreamReader.CHARACTERS) System.out.println(streamReader.getText()); if(parseEvent == XMLStreamReader.END_ELEMENT) System.out.println(""); } } catch (SQLException ex) {ex.printStackTrace();} catch(FactoryConfigurationError ex){ex.printStackTrace();} catch(XMLStreamException ex){ex.printStackTrace();} } } Part – Workshops (30 minutes) • • Quickly look at workshops for Module for reviewing basic steps Try to compile, run and observe the output of sample code provided for related workshop Discuss with your class-mate and your instructor if needed Part – Lab Assignment (60 minutes) Do the assignment for Module carefully Discuss with your class-mates and your instructor if needed Part –Do it yourself Write a Java application generate digital signature and public key The output of the program is as shown as below © 2011 FPT-Aptech Page / SJava-Lab5- Digital Signature © 2011 FPT-Aptech Page /

Ngày đăng: 27/10/2019, 09:27

TỪ KHÓA LIÊN QUAN

w