Package crypt import import import

This preview shows page 141 - 146 out of 342 pages.

We have textbook solutions for you!
The document you are viewing contains questions related to this textbook.
Network+ Guide to Networks
The document you are viewing contains questions related to this textbook.
Chapter A / Exercise 67
Network+ Guide to Networks
Dean/West
Expert Verified
*/ package crypt; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.KeyStore; import java.security.KeyStore.PasswordProtection; import java.util.Enumeration;
We have textbook solutions for you!
The document you are viewing contains questions related to this textbook.
Network+ Guide to Networks
The document you are viewing contains questions related to this textbook.
Chapter A / Exercise 67
Network+ Guide to Networks
Dean/West
Expert Verified
import javax.crypto.SecretKey; import javax.swing.JOptionPane; /** * * @author merid */ public class CryptKeyStore { /** * Creates a new instance of kStore */ public CryptKeyStore() { } /** * This method is used to create a new key store. */ public int createKeystore(String keyFilePath, String password) { KeyStore ks = null; int result; /*Create concrete KeyStore instance and then *Load needs to be done before accessing the keystore. In *this case we are creating it empty by passing in null. *We then store the file and close it*/ try { ks = KeyStore.getInstance("JCEKS"); ks.load(null, password.toCharArray()); FileOutputStream fos = new FileOutputStream(keyFilePath); ks.store(fos, password.toCharArray()); fos.close(); result = 1;
//success } catch (Exception e) { System.out.println(e); result = 0; //failure } return result; } /** * This method is used to add a key to a current key store */ public int addKey(String keyFilePath, String password, SecretKey sKey, String alias) { KeyStore ks = null; //creates the keystore int result; PasswordProtection pass = new PasswordProtection(password.toCharArray()); //creates the password protection for the keystore /*Initialize (load) the keystore*/ try { ks = this.initKeystore(keyFilePath, password); KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(sKey); ks.setEntry(alias, skEntry, pass); FileOutputStream fos = new FileOutputStream(keyFilePath); ks.store(fos, password.toCharArray()); fos.close(); result = 1; //success } catch (Exception e) { result = 0; //failure System.out.println(e); } return result; } /**
* This method is used to add a key to a current key store */ public int deleteKey(String keyFilePath, String password, String alias) { KeyStore ks = null; //creates the keystore int result; PasswordProtection pass = new PasswordProtection(password.toCharArray()); //creates the password protection for the keystore /*Initialize (load) the keystore*/ try { ks = this.initKeystore(keyFilePath, password); ks.deleteEntry(alias); FileOutputStream fos = new FileOutputStream(keyFilePath); ks.store(fos, password.toCharArray()); fos.close(); result = 1; //success } catch (Exception e) { result = 0; //failure System.out.println(e); } return result; } /** * This method is used to retrieve a key from a current key store */ public SecretKey getKey(String keyFilePath, String password, String alias) { System.out.println("getKey"); KeyStore ks = null; //creates the keystore SecretKey sKey = null; //creates the secret key that will be returned PasswordProtection pass = new PasswordProtection(password.toCharArray()); //creates the password protection for the keystore /*Initialize (load) the keystore*/
try { ks = this.initKeystore(keyFilePath, password); KeyStore.SecretKeyEntry skEntry = (KeyStore.SecretKeyEntry) ks.getEntry(alias, pass); sKey = skEntry.getSecretKey(); } catch (Exception e) { System.out.println(e); } System.out.println(sKey.getEncoded()); return sKey; } /** * This method is a private method that is used to initialize (load) the key * store */ private KeyStore initKeystore(String keyFilePath, String password) { System.out.println("initKeystore"); KeyStore ks = null;

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture