public static String encryptWithSymmetricKey(String data,String encAlgorithm,String secret) throws Exception {
  try {
    String algorithm=encAlgorithm;
    if (!algorithm.startsWith("PBEWith")) {
      algorithm="PBEWithMD5And" + encAlgorithm;
    }
    SecretKeyFactory skFactory=SecretKeyFactory.getInstance(algorithm);
    PBEKeySpec pbeKeySpec=new PBEKeySpec(secret.toCharArray());
    SecretKey sKey=skFactory.generateSecret(pbeKeySpec);
    Cipher cipher=Cipher.getInstance(algorithm);
    cipher.init(Cipher.ENCRYPT_MODE,sKey,pbeParameterSpec);
    byte[] encData=cipher.doFinal(data.getBytes("UTF-8"));
    encData=addPrefix(encData);
    return Base64.encode(encData);
  }
 catch (  NoSuchAlgorithmException nse) {
    throw new Exception(nse.getMessage());
  }
}

public static String encryptWithSymmetricKey ( String data , String encAlgorithm , String secret ) throws Exception { try { String algorithm = encAlgorithm ; if ( ! algorithm . startsWith ( "PBEWith" ) ) { algorithm = "PBEWithMD5And" + encAlgorithm ; } SecretKeyFactory skFactory = SecretKeyFactory . getInstance ( algorithm ) ; PBEKeySpec pbeKeySpec = new PBEKeySpec ( secret . toCharArray ( ) ) ; SecretKey sKey = skFactory . generateSecret ( pbeKeySpec ) ; Cipher cipher = Cipher . getInstance ( algorithm ) ; cipher . init ( Cipher . ENCRYPT_MODE , sKey , pbeParameterSpec ) ; byte [ ] encData = cipher . doFinal ( data . getBytes ( "UTF-8" ) ) ; encData = addPrefix ( encData ) ; return Base64 . encode ( encData ) ; } catch ( NoSuchAlgorithmException nse ) { throw new Exception ( nse . getMessage ( ) ) ; } }
encrypts the given data with a symmetric key that was generated using given shared secret .
