How To Encrypt Too Decrypt An Object Inwards Too From A File Inwards Java

There are times when nosotros bespeak to write something on a file, merely doesn't desire it to live readable equally a apparently text. In this instance nosotros tin purpose whatever type of encryption mechanism, merely what if nosotros desire to decrypt the encrypted file dorsum together with read its contents. For instance a configuration file, etc.

Let's demo roughly codes equally usual:

public cast CipherUtils {   populace static lastly String CIPHER_MODE = "AES/CBC/PKCS5Padding";   individual CipherUtils() {   }   populace static void encode(Serializable object, String password, String path)    throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,    IllegalBlockSizeException, InvalidAlgorithmParameterException {   Cipher nix = Cipher.getInstance(CIPHER_MODE);   cipher.init(Cipher.ENCRYPT_MODE, fromStringToAESkey(password), novel IvParameterSpec(new byte[16]));    // read object from file   SealedObject sealedObject = novel SealedObject(object, cipher);   FileOutputStream fos = novel FileOutputStream(path);   CipherOutputStream cipherOutputStream = novel CipherOutputStream(new BufferedOutputStream(fos), cipher);    ObjectOutputStream outputStream = novel ObjectOutputStream(cipherOutputStream);   outputStream.writeObject(sealedObject);   outputStream.close();   fos.close();  }   populace static Serializable decode(String password, String path)    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException,    ClassNotFoundException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {   Cipher nix = Cipher.getInstance(CIPHER_MODE);    // write object to file   cipher.init(Cipher.DECRYPT_MODE, fromStringToAESkey(password), novel IvParameterSpec(new byte[16]));   CipherInputStream cipherInputStream = novel CipherInputStream(new BufferedInputStream(new FileInputStream(path)),     cipher);    ObjectInputStream inputStream = novel ObjectInputStream(cipherInputStream);   SealedObject sealedObject = (SealedObject) inputStream.readObject();   Serializable serializeableObject = (Serializable) sealedObject.getObject(cipher);   inputStream.close();    furnish serializeableObject;  }   populace static SecretKey fromStringToAESkey(String s) throws UnsupportedEncodingException {   // 128bit substitution bespeak xvi byte   byte[] rawKey = novel byte[16];   // if you lot don't specify the encoding you lot powerfulness larn weird results   byte[] keyBytes = s.getBytes("UTF-8");   System.arraycopy(keyBytes, 0, rawKey, 0, keyBytes.length);    furnish novel SecretKeySpec(rawKey, "AES");  } } 

This utility cast tin encrypt together with decrypt whatever serialize-able object nosotros have.

And here's how nosotros purpose it:

 populace cast CipherUtilsTest {   individual Person person;   @Before  populace void init() {   mortal = novel Person();   person.setFirstname("Shirayuki");   person.setLastname("Hime");   person.setAge(18);  }   @Test  populace void testEncodeDecode() {   endeavor {    CipherUtils.encode(person, "shirayuki", "c://temp//cipher");    Person decodedPerson = (Person) CipherUtils.decode("shirayuki", "c://temp//cipher");    assertEquals(person.getAge(), decodedPerson.getAge());    assertEquals(person.getFirstname(), decodedPerson.getFirstname());    assertEquals(person.getLastname(), decodedPerson.getLastname());   } choose deal of (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException     | IOException | ClassNotFoundException | BadPaddingException | InvalidAlgorithmParameterException e) {    // TODO Auto-generated choose deal of block    e.printStackTrace();   }  }  } 
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.