Example #1
0
 @Override
 public String decrypt(String input, String key) throws Exception {
   String output = "";
   SecretKeySpec tempKey = null;
   if (input.length() < 1) {
     return null;
   }
   // if user has put in the key, generate a key by the string that user
   // has put in
   if (key != null) {
     tempKey = this.generateKey(key);
   } else {
     // otherwise use default key
     tempKey = defaultKey;
   }
   cypher.init(Cipher.DECRYPT_MODE, tempKey);
   byte[] bytes = SBConvertor.stringToByte(input);
   byte[] bResult = cypher.doFinal(bytes);
   // create string from byte array
   output = new String(bResult);
   return output;
 }
Example #2
0
 @Override
 public String encrypt(String input, String key) throws Exception {
   String output = "";
   SecretKeySpec tempKey = null;
   if (input.length() < 1) { // string is empty
     return null;
   }
   // if user has put in the key, generate a key by the string that user
   // has put in
   if (key != null) {
     tempKey = this.generateKey(key);
   } else {
     // otherwise use default key
     tempKey = StringEncryptor.defaultKey;
   }
   cypher.init(Cipher.ENCRYPT_MODE, tempKey);
   byte[] bytes = input.getBytes("UTF-8");
   byte[] bResult = cypher.doFinal(bytes);
   // convert byte array to string
   output = SBConvertor.byteToString(bResult);
   return output;
 }