/* * CryptHandler.java * Karl-Adam Karlsson * 06-10-15 * * Crypthandler, laddar en nyckel från fil (om en path ges). * Sköter sedan kryptering och dekryptering av strängar. * * Där keyPath är path till filen innehållande nyckeln, eller null om man vill använda en default nyckel. */ import javax.crypto.*; import java.security.*; import java.io.*; public class CryptHandler{ Cipher enCipher, deCipher; SecretKey akey; /* * Konstruktor * */ public CryptHandler(String keyPath){ //LOADING KEY if(keyPath==null||keyPath.equals("")){ System.out.println("Using DefaultKey for message text encryption."); keyPath = "DefaultKey"; } akey = loadKey(keyPath); // CREATING ENCRYPTCIPHER try{ enCipher = Cipher.getInstance(akey.getAlgorithm() ); enCipher.init(Cipher.ENCRYPT_MODE, akey); }catch(NoSuchAlgorithmException alEx){ System.err.print(alEx); System.exit(1); }catch(InvalidKeyException iKe){ System.err.print(iKe); System.exit(1); }catch(NoSuchPaddingException padEx){ System.err.print("NO SUCH PADDING"); System.exit(1); } // DONE CREATING CIPHER // CREATING DECRYPT CIPHER try{ deCipher = Cipher.getInstance(akey.getAlgorithm() ); deCipher.init(Cipher.DECRYPT_MODE, akey); }catch(NoSuchAlgorithmException alEx){ System.err.print(alEx); System.exit(1); }catch(InvalidKeyException iKe){ System.err.print(iKe); System.exit(1); }catch(NoSuchPaddingException padEx){ System.err.print("NO SUCH PADDING"); System.exit(1); } // DONE CREATING CIPHER }//end of constructor /* * encrypt * Takes a String and returns it encrypted * @param String toEncrypt The String to Encrypt * @return String A encrypted String. */ public String encrypt(String toEncrypt){ try { // Gör om strängen till bytes byte[] myByte = toEncrypt.getBytes("UTF8"); // kryptera datat byte[] enc = enCipher.doFinal(myByte); // Gör om till sträng från bytes och returnera return new sun.misc.BASE64Encoder().encode(enc); } catch (javax.crypto.BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); } return "ENCRYPTION FAILED"; }//end of encrypt /* * decrypt, decrypts a String and returns it. * @param Strin toDecrypt A encrypted String * @return String The decrypted String * */ public String decrypt(String toDecrypt){ System.out.println("DECPYTING: "+toDecrypt); try { // Gör från Sträng till bytes byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(toDecrypt); // Decrypt byte[] myByte = deCipher.doFinal(dec); // Tillbaka till sträng och returnera. return new String(myByte, "UTF8"); } catch (javax.crypto.BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); } return "DECRYPTION FAILED"; }//end of decrypt /* * loadKey * Loads a SecretKey Object from the file named * @param keyFileName Name of file to load key from * @return SecretKey the loaded key. */ private SecretKey loadKey(String keyFileName){ ObjectInputStream oInStr = null; SecretKey theKey = null; try{ FileInputStream inFstream = new FileInputStream(keyFileName); oInStr = new ObjectInputStream(inFstream); }catch(FileNotFoundException fEx){ System.err.print(fEx); System.exit(1); }catch(IOException ioe){ System.err.print(ioe); System.exit(1); } try{ theKey = (SecretKey)oInStr.readObject(); oInStr.close(); }catch(IOException io2){ System.err.print(io2); System.exit(1); }catch(ClassNotFoundException cNf){ System.err.print(cNf); System.exit(1); } return theKey; }//end of loadKey }//End of class