// From http://code.calum.org/ public static byte[] encrypt(byte[] plain, PublicKey pk) { Logger log = Logger.getLogger("foo"); try { Cipher cipher = Cipher.getInstance(Main.ASYMMETRIC); cipher.init(Cipher.ENCRYPT_MODE, pk); return cipher.doFinal(plain); } catch (InvalidKeyException ex) { System.err.println(ex.toString()); } catch (NoSuchAlgorithmException ex) { System.err.println(ex.toString()); } catch (IllegalBlockSizeException ex) { System.err.println(ex.toString()); } catch (NoSuchPaddingException ex) { System.err.println(ex.toString()); } catch (BadPaddingException ex) { System.err.println(ex.toString()); } return null; }