APJII lab 04

4 54 0
APJII lab 04

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

Thông tin tài liệu

AdvJ-Lab1-Thread APJ-II -Lab4-Cryptography Advanced Programming in Java - II Lab Guide for Session 4: Module 5: Cryptography Session Objectives In this session, you will be practicing with  Cipher object  KeyGenerator and SecretKey to make symmetric encryption  PBEParameterSpec, PBEKeySpec, SecretKeyFactory and SecretKey to make encryption with password Part – Getting started (30 minutes) Create a simple application to encrypt and decrypt a simple text (15 minutes) Scan the code first, type the code, compile, run and observe the result import import import import import public java.util.logging.Level; java.util.logging.Logger; javax.crypto.Cipher; javax.crypto.KeyGenerator; javax.crypto.SecretKey; class SimpleEncryption { public static void main(String[] args) { try { //plain text to encrypt String o = "I love Java"; System.out.println("Plain text:" + o); //Initialize KeyGenerator generator = KeyGenerator.getInstance("DES"); SecretKey key = generator.generateKey(); //Get cipher Cipher c = Cipher.getInstance("DES/ECB/PKCS5Padding"); //Encrypt c.init(Cipher.ENCRYPT_MODE, key); //encrypt data store to e byte[] e = c.doFinal(o.getBytes()); System.out.println("encrypted:" + new String(e)); © 2009 FPT-Aptech © 2011 FPT-Aptech Page / Page / AdvJ-Lab1-Thread APJ-II -Lab4-Cryptography //Decrypt c.init(Cipher.DECRYPT_MODE, key); } //Decrypt and transform to text to show to screen System.out.println("decrypted:" + new String(c.doFinal(e))); } catch (Exception ex) { Logger.getLogger(SimpleEncryption class.getName()).log(Level.SEVERE, null, ex); } } 2.Create an application to encrypt a text with password(30 minutes) Scan the code first, type the code, compile, run and observe the result import import import import import import import import import import import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; java.io.BufferedReader; java.io.DataOutputStream; java.io.FileOutputStream; java.io.FileReader; java.util.Scanner; javax.crypto.Cipher; javax.crypto.SecretKey; javax.crypto.SecretKeyFactory; javax.crypto.spec.PBEKeySpec; javax.crypto.spec.PBEParameterSpec; public class Encrypt { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { try { //Declaration String fileOriginal; String fileEncrypted; //Input System.out.println("File to encrypt:"); fileOriginal = keyboard.nextLine(); System.out.println("Encrypted file:"); fileEncrypted = keyboard.nextLine(); //Getting secrete key © 2009 FPT-Aptech © 2011 FPT-Aptech Page / Page / AdvJ-Lab1-Thread APJ-II -Lab4-Cryptography System.out.print("Enter your password:"); char[] password = keyboard.nextLine().toCharArray(); //Init byte[] salt = { (byte) 0x12, (byte) 0x32, (byte) 0x12, (byte) 0x56, (byte) 0x12, (byte) 0x32, (byte) 0x12, (byte) 0x56 }; int count = 20; PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, count); PBEKeySpec keySpec = new PBEKeySpec(password); SecretKeyFactory keyFactor = SecretKeyFactory getInstance("PBEWithMD5AndDES"); SecretKey sk = keyFactor.generateSecret(keySpec); encrypt(fileOriginal, fileEncrypted, sk, parameterSpec); } catch (Exception ex) { } } private static void encrypt( String dataFile, String encrytedFile, SecretKey skey, PBEParameterSpec parameterSpec) { try { //Read plain from file BufferedReader br = new BufferedReader( new FileReader(dataFile)); String original = br.readLine(); br.close(); //Get Cipher Cipher c = Cipher.getInstance("PBEWithMD5AndDES"); //Init c.init(Cipher.ENCRYPT_MODE, skey, parameterSpec); //Encrypt byte[] encrypted = c.doFinal(original.getBytes()); //Save to file DataOutputStream dos = new DataOutputStream( new FileOutputStream(encrytedFile)); dos.writeUTF(Base64.encode(encrypted)); © 2009 FPT-Aptech © 2011 FPT-Aptech Page / Page / AdvJ-Lab1-Thread APJ-II -Lab4-Cryptography dos.close(); } catch (Exception ex) { } } } Part – Workshops (30 minutes) • • Quickly look at Module 5’s workshops for reviewing basic steps to encrypt and encrypt with password 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 (45 minutes) Do the assignment for Module carefully Discuss with your class-mates and your instructor if needed Part – Do it your self Create an application to decrypt a text by programm of Exercise at Getting Started Rewrite application at Exercise at Getting Started and Exercise at Do it yourselft with GUI © 2009 FPT-Aptech © 2011 FPT-Aptech Page / Page /

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

Mục lục

    Advanced Programming in Java - II

    Part 1 – Getting started (30 minutes)

    Part 3 – Lab Assignment (45 minutes)

    Part 4 – Do it your self

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan