Chapter 12 - Code Snippets Test Basic Security with an Applet: Phase II - Create a Java Applet to Write a Text File p. 476 Step 18. Insert the following method, just before the closing } at the end of the file: public void paint (Graphics g) { try{ String dirName = "C:\\test"; BufferedWriter out = new BufferedWriter (new OutputStreamWriter ( new FileOutputStream(dirName + File.separator + "fileout.txt") , "UTF8" )); out.write("Hello World - This is a successful test"); out.newLine(); out.close(); g.drawString("You just completed writing to: fileout.txt ", 10, 10); } catch (SecurityException se) { g.drawString("FileOUT: SecurityException: " + se, 25, 25); } catch (UnsupportedEncodingException uee) { g.drawString("FileOUT: UnsupportedEncodingException", 25, 25); } catch (IOException ioe) { g.drawString("FileOUT: I/O Exception", 25, 25); } } You can also use PrintWriter instead of BufferedWriter. If you use PrintWriter, the code within the "try" block changes to the following: PrintWriter out = new PrintWriter (new FileWriter ("C:\\test\\fileout.txt") ); out.println("Hello World - This is a successful test"); out.close(); g.drawString("You just completed writing to: fileout.txt ", 10, 10); If you use this code, you would no longer need the "catch" for the UnsupportedEncodingException. ----------------------------------------------------------------------------- p. 483 Phase VI - Test the Applet in Appletviewer Start Notepad in Windows (Start | Programs | Accessories | Notepad) and enter the following command on one line, leaving one white space between segments. You must use the forward slashes for all operating systems. The trailing & means to run asynchronously (do not wait for command to terminate). appletviewer -J-Djava.security.policy=file:/c:/test/file.policy file:/c:/test/MyFileOutPRJ_html/MyFileOutPRJ_MyFileOut.html & ----------------------------------------------------------------------------- Implement Certificate Security - Phase 1: Prepare the Sender to Use a Digital Signature p. 488 Step 3. To create your first set of keys, enter the following command at the C:\test prompt. The entire command must be entered on a single line. keytool -genkey -alias signfilekey -validity 365 -keypass xyzzy123-keystore MyKeyRepository -storepass Plough#1974 --------------------------------------------------------------------------- p. 489 Step 1. To create a certificate, enter the following command at the C:\test prompt. The entire command must be entered on a single line. keytool -export -keystore MyKeyRepository -alias signfilekey-file MarkJonesSun.cer Step 1. (under Obtain a Fingerprint for the Certificate), keytool -printcert -file MarkJonesSun.cer --------------------------------------------------------------------------- p. 490 Step 1. Navigate to the C:\test directory and rename MyFileOutPRJ.zip to "unsigned.zip" using this command line command: ren MyFileOutPRJ.zip unsigned.zip Step 2. Next, enter the following command at the C:\test prompt. The entire command must be entered on a single line. jarsigner -keystore MyKeyRepository -signedjar MyFileOutPRJ.zip unsigned.zip signfilekey -------------------------------------------------------------------------------- Phase II: Prepare the Receiveer to use a Digital Signature p. 492 Step 3: To obtain a certificate fingerprint, use the following script on the C:\test prompt command line. The entire command must be entered on a single line. keytool -printcert -file MarkJonesSun.cer Step 4. To store your certificate, enter the following command at the C:\test prompt. The entire command must be entered on a single line. keytool -import -alias signfilekey -file MarkJonesSun.cer -keystore ClientKeyRepository ----------------------------------------------------------------------------------- p. 494 Step 1. Start Notepad and enter the following code on just one line, leaving one white space between segments: you must use the forward slashes for all operating systems. appletviewer -J-Djava.security.policy=file:/c:/test/clientfile.policy file:/c:/test/MyFileOutPRJ_html/MyFileOutPRJ_MyFileOut.html --------------------------------------------------------------------------- p. 497 Step 3. To create a new certificate and key file for Internet Explorer, enter the following code on a single line and press ENTER. makecert -n "CN=MarkJonesIEcert" -m 99 -r -sv MarkJonesIE.pvk MarkJonesIE.cer Step 6. To convert your certificate file to a Microsoft-compatible file (.spc), enter the following command at the C:\test prompt and press ENTER. cert2spc MarkJonesIE.cer MarkJonesIE.spc --------------------------------------------------------------------------- Creating Security Files to Support Internet Explorer - Phase II p. 498 Step 2. To store your Java code in a cabinet file, enter the following code on a single line: cabarc -p n MyFileOutPRJforIE.cab myfileout/MyFileOut.class Sign the Code and Test the Applet Step 1. To sign your code with your digital signature, and to specify the modified security requirements, enter the following code on a single line. You may view the signcode options by entering "signcode" at the prompt. signcode -j javasign.dll -jp permissions.ini -spc MarkJonesIE.spc-v MarkJonesIE.pvk MyFileOutPRJforIE.cab ---------------------------------------------------------------------------- p. 499 Step 5. Next, using a command prompt, change to the C:\test\unzip directory and enter the following command: cabarc -p -r n jdev-rt.cab *.* Step 6. Copy the new cabinet .cab file to C:\test\jdev-rt.cab by entering copy C:\test\unzip\jdev-rt.cab C:\test\jdev-rt.cab Step 7. Using Notepad or another text editor, insert the following line just above the ending applet tag "" in the file C:\test\MyFileOutPRJ_html\MyFileOutPRJ_MyFileOUT.html Step 12. To run your applet with Internet Explorer, open Internet Explorer and enter the following address: C:\test\MyFileOutPRJ_html\MyFileOutPRJ_MyFileOUT.html ----------------------------------------------------------------------------