#3. Encrypted the whole class with javax.crypto

Many may have been googled around with this very topic and have came across with many example; but most of them haven’t depicted the practical one.

So, here I tried to conduct practical one. Hope this will help :D

We’ll started with how to generated the SecretKey without creating a new one every time you have run the software (no more random).I’ll illustrate this example with the “DES” algorithm. The “DES” algorithm required at least 8 bytes key. Here is SecretKey generator function.

   1: public static SecretKey generateKey(String pass) 
   2:     throws InvalidKeyException 
   3: {
   4:         SecretKey key = null;
   5:         
   6:         DESKeySpec ks = new DESKeySpec( pass.getBytes() );    // required 8 bytes at least
   7:         SecretKeyFactory factory;
   8:         try {
   9:             factory = SecretKeyFactory.getInstance( "DES" );
  10:             key = factory.generateSecret( ks );
  11:         } catch (NoSuchAlgorithmException e) {
  12:             e.printStackTrace();
  13:         } catch (InvalidKeySpecException e) {
  14:             e.printStackTrace();
  15:         }
  16:         return key;
  17:     }

After you have a function for generated the key; Let’s translate your class to array of byte and vice versa.

   1: public static byte[] createByteArrayFromObject(Serializable o) throws IOException {
   2:     ByteArrayOutputStream baos = new ByteArrayOutputStream();
   3:     ObjectOutputStream oos;
   4:     oos = new ObjectOutputStream(baos);
   5:     oos.writeObject(o);
   6:     oos.close();
   7:     return baos.toByteArray();
   8: }
   9:  
  10: public static Object createObjectFromByteArray(byte[] byteObject) throws IOException, ClassNotFoundException {
  11:     ByteArrayInputStream bais;
  12:     ObjectInputStream ois;
  13:     bais = new ByteArrayInputStream(byteObject);
  14:     ois = new ObjectInputStream(bais);
  15:     return ois.readObject();
  16:     
  17: }

This is how to encrypt the class; supply the class’s array of byte through bytes parameter.

   1: public static String encryptBytes(SecretKey key, byte[] bytes) {
   2:     try {
   3:         Cipher ecipher = Cipher.getInstance("DES");
   4:         ecipher.init(Cipher.ENCRYPT_MODE, key);
   5:  
   6:         // re-adjust the bytes accordingly to the protocol
   7:         byte[] arranged = bytes;
   8:         
   9:         // Encrypt
  10:         byte[] enc = ecipher.doFinal(arranged);
  11:  
  12:         // Encode bytes to base64 to get a string
  13:         return new sun.misc.BASE64Encoder().encode(enc);
  14:     } catch (javax.crypto.BadPaddingException e) {
  15:     } catch (IllegalBlockSizeException e) {
  16:     } catch (javax.crypto.NoSuchPaddingException e) {
  17:     } catch (java.security.NoSuchAlgorithmException e) {
  18:     } catch (java.security.InvalidKeyException e) {
  19:     }
  20:     return null;
  21: }

Now to decrypt:
   1: public byte[] decryptBytes(String str) {
   2:     try {
   3:         Cipher dcipher;
   4:  
   5:         dcipher = Cipher.getInstance("DES");
   6:         dcipher.init(Cipher.DECRYPT_MODE, key);
   7:  
   8:         // Decode base64 to get bytes
   9:         byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
  10:  
  11:         // Decrypt
  12:         byte[] unarranged = dcipher.doFinal(dec);
  13:         // re-adjust the bytes accordingly to the protocol
  14:         byte[] arranged = unarranged;
  15:         
  16:         return arranged;
  17:     } catch (javax.crypto.BadPaddingException e) {
  18:     } catch (IllegalBlockSizeException e) {
  19:     } catch (UnsupportedEncodingException e) {
  20:     } catch (java.io.IOException e) {
  21:     } catch (javax.crypto.NoSuchPaddingException e) {
  22:     } catch (java.security.NoSuchAlgorithmException e) {
  23:     } catch (java.security.InvalidKeyException e) {
  24:     }
  25:     return null;
  26: }

 
Now encrypted this class’s array of byte with the key generated by the first function.
 
encrypt:
 
   1: String password = "this is the password string used as key to encrypt";
   2:  
   3: SecretKey key = generateKey(password);
   4:  
   5: ObjectToEncrypt ote = new ObjectToEncrypt();
   6: String encryptedString = encryptBytes(key, createByteArrayFromObject(ote)); 

 
decrypt:

   1: String password = "the same password that used earlier";
   2:  
   3: String encryptedString = "...."; // this is encrypted string
   4:  
   5: SecretKey key = generateKey(password);
   6:  
   7: ObjectToEncrypt ote = (ObjectToEncrypt) createObjectFromByteArray(decryptBytes(key, encryptedString));

Note that the object to encrypted/decrypted must implements Serializable.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.