public static String encryptWithAsymmetricKey(String data,String encryptionAlgorithm,int encryptionStrength,Key encKey) throws Exception {
  try {
    KeyGenerator keygen=KeyGenerator.getInstance(encryptionAlgorithm);
    if (encryptionStrength != 0) {
      keygen.init(encryptionStrength);
    }
    SecretKey sKey=keygen.generateKey();
    Cipher cipher=Cipher.getInstance(encryptionAlgorithm);
    cipher.init(Cipher.ENCRYPT_MODE,sKey);
    byte[] encData=cipher.doFinal(data.getBytes("UTF-8"));
    cipher=Cipher.getInstance(encKey.getAlgorithm());
    cipher.init(Cipher.WRAP_MODE,encKey);
    byte[] keyWrap=cipher.wrap(sKey);
    byte[] encDataPad=wrapKeyWithEncryptedData(encData,keyWrap);
    return Base64.encode(encDataPad);
  }
 catch (  NoSuchAlgorithmException nse) {
    throw new Exception(nse.getMessage());
  }
catch (  NoSuchPaddingException npe) {
    throw new Exception(npe.getMessage());
  }
catch (  InvalidKeyException ike) {
    throw new Exception(ike.getMessage());
  }
catch (  UnsupportedEncodingException uae) {
    throw new Exception(uae.getMessage());
  }
}

public static String encryptWithAsymmetricKey ( String data , String encryptionAlgorithm , int encryptionStrength , Key encKey ) throws Exception { try { KeyGenerator keygen = KeyGenerator . getInstance ( encryptionAlgorithm ) ; if ( encryptionStrength != 0 ) { keygen . init ( encryptionStrength ) ; } SecretKey sKey = keygen . generateKey ( ) ; Cipher cipher = Cipher . getInstance ( encryptionAlgorithm ) ; cipher . init ( Cipher . ENCRYPT_MODE , sKey ) ; byte [ ] encData = cipher . doFinal ( data . getBytes ( "UTF-8" ) ) ; cipher = Cipher . getInstance ( encKey . getAlgorithm ( ) ) ; cipher . init ( Cipher . WRAP_MODE , encKey ) ; byte [ ] keyWrap = cipher . wrap ( sKey ) ; byte [ ] encDataPad = wrapKeyWithEncryptedData ( encData , keyWrap ) ; return Base64 . encode ( encDataPad ) ; } catch ( NoSuchAlgorithmException nse ) { throw new Exception ( nse . getMessage ( ) ) ; } catch ( NoSuchPaddingException npe ) { throw new Exception ( npe . getMessage ( ) ) ; } catch ( InvalidKeyException ike ) { throw new Exception ( ike . getMessage ( ) ) ; } catch ( UnsupportedEncodingException uae ) { throw new Exception ( uae . getMessage ( ) ) ; } }
encrypts the given data with an asymmetric key . the asymmetric encryption uses symmetric secret key for data encryption and sends the secret key to the recipient by encrypting the same with given transport key ( publick key ) .
