in src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java [215:267]
public static void processFile(String cipherMode, char[] password, File sourceFile, File targetFile)
throws Exception {
try (FileInputStream fis = new FileInputStream(sourceFile)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CryptoUtilJ8 cryptoUtilJ8 = createCryptoUtil(cipherMode);
if (cryptoUtilJ8 == null) {
System.out.println("Canceling ");
return;
}
if (cipherMode.startsWith("dec")) {
System.out.println("Decrypting " + sourceFile.getAbsolutePath());
// String value = new String(Files.readAllBytes(Paths.get(sourceFile.toURI())));
StringBuffer stringBuffer = new StringBuffer();
int i;
while ((i = fis.read()) != -1) {
stringBuffer.append((char) i);
}
String value = stringBuffer.toString();
if (isHexadecimal(value)) {
byte[] buffer = HexConverter.toBytes(value);
cryptoUtilJ8.decrypt(buffer, baos, password);
} else {
try (FileInputStream fis2 = new FileInputStream(sourceFile)) {
cryptoUtilJ8.decrypt(fis2, baos, password);
}
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
FileOutputStream fos = new FileOutputStream(targetFile);
StreamUtil.copy(bais, fos);
bais.close();
fos.close();
} else if (cipherMode.startsWith("enc")) {
System.out.println("Encrypting " + sourceFile.getAbsolutePath());
cryptoUtilJ8.encrypt(fis, baos, password);
fis.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
FileOutputStream fos = new FileOutputStream(targetFile);
StreamUtil.copy(bais, fos);
bais.close();
fos.close();
} else {
String msg = "Don't know what to do with : " + cipherMode;
throw new IllegalArgumentException(msg);
}
}
}