Created
July 26, 2014 14:21
-
-
Save jordanbaucke/5869e2ceb1b18f2398fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String encryptByteArray(byte[] clearData, | |
PGPPublicKey encKey, boolean withIntegrityCheck, boolean armor) | |
throws IOException, PGPException, NoSuchProviderException { | |
ByteArrayOutputStream encOut = new ByteArrayOutputStream(); | |
OutputStream out = encOut; | |
if (armor) { | |
out = new ArmoredOutputStream(out); | |
} | |
ByteArrayOutputStream bOut = new ByteArrayOutputStream(); | |
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator( | |
PGPCompressedDataGenerator.ZIP); | |
OutputStream cos = comData.open(bOut); // open it with the final | |
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator(); | |
OutputStream pOut = lData.open(cos, PGPLiteralData.BINARY, | |
PGPLiteralData.CONSOLE, clearData.length, | |
new Date() // current time | |
); | |
pOut.write(clearData); | |
lData.close(); | |
comData.close(); | |
PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator( | |
PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(), | |
"BC"); | |
cPk.addMethod(encKey); | |
byte[] bytes = bOut.toByteArray(); | |
OutputStream cOut = cPk.open(out, bytes.length); | |
cOut.write(bytes); | |
cOut.close(); | |
out.close(); | |
return encOut.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment