public static SecretKey decryptKey(String contents) throws CryptException { final AsymmetricAlgorithm alg = new RSA(); alg.setKey(getKey().getPrivate()); byte[] result = null; SecretKey key = null; alg.initDecrypt(); result = alg.decrypt(contents, new Base64Converter()); key = new SecretKeySpec(result, "AES"); return key; }
/** * Decrypt with my private key. borrowing from * http://www.itcsolutions.eu/2011/08/24/how-to-encrypt-decrypt-files-in-java-with-aes-in-cbc-mode-using-bouncy-castle-api-and-netbeans-or-eclipse/ * . * * @param contents * @return * @throws IOException */ public static String encryptKey(SecretKey newKey, XMPNode target) throws IOException { final AsymmetricAlgorithm alg = new RSA(); alg.setKey(target.getCert().getPublicKey()); String result = null; try { alg.initEncrypt(); result = alg.encrypt(newKey.getEncoded(), new Base64Converter()); } catch (CryptException e) { logger.error("Error in encryption. ", e); e.printStackTrace(); } return result; }