From 0145a574c672c54f7af37974baed938e2905fbc7 Mon Sep 17 00:00:00 2001 From: Hai HoDac <hhodac@student.unimelb.edu.au> Date: Thu, 24 Oct 2019 22:03:45 +1100 Subject: [PATCH] Merged --- src/remote/EncryptDecrypt.java | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/remote/EncryptDecrypt.java diff --git a/src/remote/EncryptDecrypt.java b/src/remote/EncryptDecrypt.java new file mode 100644 index 0000000..cd6af8a --- /dev/null +++ b/src/remote/EncryptDecrypt.java @@ -0,0 +1,59 @@ +package remote; + +import javax.crypto.*; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +public class EncryptDecrypt { + public static String decryptString (SealedObject sealedObject, SecretKey key){ + + String nonSealedString =""; + Cipher cipher = null; + try { + cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.DECRYPT_MODE,key); + nonSealedString = (String)sealedObject.getObject(cipher); + + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (NoSuchPaddingException e) { + e.printStackTrace(); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + e.printStackTrace(); + } catch (BadPaddingException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + return nonSealedString; + + } + public static SealedObject encryptString (String string, SecretKey key){ + SealedObject sealedObject = null; + + Cipher cipher = null; + try { + cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.ENCRYPT_MODE,key); + sealedObject = new SealedObject(string,cipher); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (NoSuchPaddingException e) { + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } + return sealedObject; + + + } +} -- GitLab