JAVA 密码算法报告.docx
Java暗号 目的本JAVA言語用、共通鍵暗号方式暗号化、復号作成最終目標。Java言語() 所要年数、総預金額計算(a) 課題毎年一定金額預、目標金額達上回年数時総預金額提示作成。(b) 作成public class Calculation public static void main(String args)int goal;int deposit;int amount = 0;int years = 0;try goal = Integer.parseInt(args0); System.out.println("目標金額:"+goal+"円"); deposit = Integer.parseInt(args1); System.out.println("毎年預金額:"+deposit+"円"); while(amount < goal)amount += deposit;years+; System.out.println("所要年数:"+years+"年"); System.out.println("総預金額:"+amount+"円");catch(Exception ex) ex.printStackTrace(); (c) 実行結果目標金額:10000円毎年総預金額:1500円所要年数:7年総預金額:10500円() 標準出力(a) 課題 文字列標準出力作成、後、自分名字字指定、実行。System.out.println代System.out.print、System.out.write(buf)代System.out.write(buf,off,len)使用、StdOutSample.java結果同一StdOutPrac.java作成。、実行行、StdOutSample.jav同結果確認。、以下変数buf格納長(int型)、buf.length得。length。(b) StdOutSample実行結果String標準出力: roka配列標準出力: roka(c) 変更public class StdOutParc public static void main(String args)String dataP;String dataW;byte buf = new byte100;try dataP = args0; System.out.print("String標準出力:"+dataP+"n"); dataW = "配列標準出力:"+args0+"n" buf = dataW.getBytes(); System.out.write(buf, 0, buf.length);catch(Exception ex) ex.printStackTrace(); (d) StdOutPrac実行結果String標準出力:roka配列標準出力:roka例外処理() 同一(a) 課題Exception.java作成、後、以下三種類順次指定実行、結果記録。-1, 50, 100class ArgException extends Exceptionpublic class Except public static void main(String args)try total(Integer.parseInt(args0);catch(ArgException e) System.out.println(":"+e); public static void total(int n) throws ArgExceptionint sum = 0;if(n < 0 | n > 100)throw new ArgException();for(int i = 1; i <= n; i+) sum += i;System.out.println("合計 = "+sum); (b) 実行結果ArgException合計1275合計5050() 異(a) 課題 上記以下部分分割、実行。作成Exception.java及含別配下行。(参考文献2)(b) 分割class ArgException extends Exceptionpublic class ExceptMain public static void main(String args)try ExceptSub.total(Integer.parseInt(args0);catch(ArgException e) System.out.println(":"+e); public class ExceptSub public static void total(int n) throws ArgException int sum = 0; if(n < 0 | n > 100)throw new ArgException(); for(int i = 1; i < n; i+) sum += i;System.out.println("合計 = "+sum); (c) 実行結果ArgException合計1275合計5050入出力処理() 文字列入出力(a) 課題文字列入出力行FileRW作成、実行。、入力(内容自由、日本語記述)作成。、入力文字数出力文字数関係記録、相違有無確認。更、相違場合、理由記述。(b) 実行結果入力文:大阪工業大学文字数合計:6入力文:大阪工業大学情報科学部文字数合計:12(c) 実行結果考察大阪工業大学一行6文字、大阪工業大学¥情報科学部複数行合計12文字。、改行加算、一文字増。() 列入出力(a) 課題 FileInputStream、FileOutputStream使用(StreamRW.java)作成。但、FileRW.java文字数数置換。、読込出力(while内)System.out.write使用。、入力作成(文字列入出力使用使用)。(b) 作成import java.io.*;public class StreamRW public static void main(String args)byte buff = new byte10;int length = 0;int totalLen = 0;try FileInputStream streamRd = new FileInputStream(args0); FileOutputStream streamWt = new FileOutputStream(args1); System.out.println("入力文:"); while(length = streamRd.read(buff) != -1)totalLen = totalLen + length;streamWt.write(buff,0,length);System.out.write(buff,0,length); System.out.println(""); System.out.println("数合計:"+totalLen); streamRd.close(); streamWt.close();catch(Exception ex) ex.printStackTrace(); (c) 実行結果入力文:大阪工業大学情報科学部数合計:35() 入出力(a) 課題 Struct入出力行以下(ObjectIO.java)示箇所補、完成。、後、実行行。本Struct生成後、出力後、入力、入力実行。(b) 作成/Object: 文字列文字列長import java.io.*; public class Struct implements Serializableprivate String str;private int leng;public Struct(String str) this.str = str; this.leng = str.length();public void print() System.out.println("文字列:"+str); System.out.println("文字列長:"+leng); import java.io.*; public class ObjectIOpublic static void main(String args) Struct obj = new Struct(args0); System.out.println("Object生成。"); obj.print(); tryFileOutputStream outFile = new FileOutputStream(args1);ObjectOutputStream outStream = new ObjectOutputStream(outFile);outStream.writeObject(obj);System.out.println("Object書込。");outStream.close(); catch(Exception ex)ex.printStackTrace();System.exit(1); tryFileInputStream inFile = new FileInputStream(args1);ObjectInputStream inStream = new ObjectInputStream(inFile);Struct var = (Struct)inStream.readObject();System.out.println("Object読込。");obj.print();inStream.close(); catch(Exception ex)ex.printStackTrace();System.exit(1); (c) 実行結果Object生成。文字列:abc文字列長: 3Object書込。Object読込。文字列: abc文字列長: 3共通鍵暗号方式暗号化、復号() 共通鍵生成(a) 課題任意暗号対共通鍵生成。KeyGen作成、鍵種類変実行。暗号名、鍵、鍵名受取、指定暗号、鍵持共通鍵生成、鍵出力。、内容標準出力。(b) 作成import java.io.*;import javax.crypto.*;class KeyGen public static void main(String args) tryKeyGenerator kg = KeyGenerator.getInstance(args0);System.out.println("暗号名"+ args0 + "。"); kg.init(Integer.parseInt(args1);SecretKey skey = kg.generateKey();System.out.println("鍵"+args1+"");FileOutputStream fos = new FileOutputStream(args2);ObjectOutputStream b = new ObjectOutputStream(fos);b.writeObject(skey);System.out.println("共通鍵"+args2 +"。");b.close(); catch(Exception e)e.printStackTrace(); (c) 実行結果 暗号名AES。鍵128。共通鍵AES.txt。() 暗号化、復号(a) 課題分割操作任意共通鍵暗号暗号化復号(i) 暗号化、入力文書名、共通鍵名、暗号文名受取、文書暗号化、暗号文格納。、暗号名標準出力。(ii) 復号、暗号文名、共通鍵名、復号結果名受取、暗号文復号、復号結果格納。、暗号名標準出力。(b) 作成(i) 暗号化import java.io.*;import javax.crypto.*;class Encrypt public static void main(String args) byte buf = new byte500;byte encrypted = new byte500;int length = 0;try FileInputStream in = new FileInputStream(args0); FileInputStream f = new FileInputStream(args1); ObjectInputStream ob = new ObjectInputStream(f); SecretKey k = (SecretKey)ob.readObject();/共通鍵読込 Cipher cipher = Cipher.getInstance(k.getAlgorithm();cipher.init(Cipher.ENCRYPT_MODE,k);System.out.println("暗号名"+ k.getAlgorithm() + "。");FileOutputStream out = new FileOutputStream(args2); /*暗号化*/ while(length = in.read(buf) != -1)encrypted = cipher.update(buf, 0, length);out.write(encrypted); encrypted = cipher.doFinal(); out.write(encrypted); / args2書込 in.close(); ob.close(); out.close();catch(Exception e) e.printStackTrace(); (ii) 復号import java.io.*;import javax.crypto.*;class Decrypt public static void main(String args) byte buf = new byte500;byte encrypted = new byte500;int length = 0;try FileInputStream in = new FileInputStream(args0); FileInputStream f = new FileInputStream(args1); ObjectInputStream ob = new ObjectInputStream(f); SecretKey k = (SecretKey)ob.readObject(); /共通鍵読込 Cipher cipher = Cipher.getInstance(k.getAlgorithm(); cipher.init(Cipher.DECRYPT_MODE,k); System.out.println("復号名"+ k.getAlgorithm() + "。"); FileOutputStream out = new FileOutputStream(args2);/*復号*/ while(length = in.read(buf) != -1)encrypted = cipher.update(buf, 0, length);out.write(encrypted); encrypted = cipher.doFinal(); out.write(encrypted); / args2書込 in.close(); ob.close(); out.close();catch(Exception e) e.printStackTrace(); (c) 実行結果(i) 暗号化暗号名AES。(ii) 復号復号名AES。考察() Java移植性優言。理由記述。移植性異環境再利用評価尺度性質一。異環境移起動移植性高。Java時Java呼中間生成、JavaJava VMJava実行環境実行可能機械語。、Java VM各種(UNIX/LINUX/Macintosh/Windows)用準備。言換Java VM各種差吸収。結果、Windows作成UNIX上移、実行。、Java移植性優。() 世中暗号技術使用例挙、暗号技術使方状況記述。電子使。利用際、互認証必要。自分情報第三者漏、暗号化。、電子取引署名暗号技術例一。署名通常3。一公開鍵生成鍵生成。一、利用者秘密鍵用署名生成、利用者公開鍵用署名検証。署名生成処理作出"署名"呼。実用性、署名私解決願全問題単独解決訳。暗号技術、安全環境、人安心見。参考文献():”連載一覧始JAVA”,http:/www.atmarkit.co.jp/fjava/index/index_javaent.html所感三週間勉強通、最低限知用語簡単習。普通呼意味通文章数列平文呼。平文何手段、第三者意味通行為暗号化呼。暗号化文章暗号文、暗号文平文戻復号呼。、JAVA基本的使方、暗号処理必要例外処理、入出力処理勉強。理解上、共通鍵方式、暗号化、復号処理。暗号技術、見、自分情報盗難可能性低。取引時署名電子認証、詐欺避、自分情報守、環境。