System.out.println("TEST: preserving flag : " + flag);
System.out.println("TEST: preserving inFileName : " + inFileName);
System.out.println("TEST: preserving X-String : " + X);
// start encryption
final InputStream fin = new CipherInputStream(new FileInputStream(inFile), ecipher);
final OutputStream fout = new FileOutputStream(outFileName);
// write magic and properties of original file
// - we encrypt the original date, the encryption date, the file size, the flag
// and file name together to the string A and calculate the length AL of that string
// - the length of the file name is therefore equal to AL-(17+17+11+1) = AL-46
// - AL is then b64-ed and also encrypted which results into string B
// - the length of B is BL; BL is then b64-ed to a string C of fixed length 1
// - after the magic String we write C, B and A
try {
final String A = UTF8.String(ecipher.doFinal(X.getBytes("UTF8")));
final String B = UTF8.String(ecipher.doFinal(Base64Order.standardCoder.encodeLongSB(A.length(), 2).toString().getBytes("UTF8"))); // most probable not longer than 4
final String C = Base64Order.standardCoder.encodeLongSB(B.length(), 1).toString(); // fixed length 1 (6 bits, that should be enough)
fout.write(UTF8.getBytes(magicString)); // the magic string, used to identify a 'crypt'-file
fout.write(UTF8.getBytes(C));
fout.write(UTF8.getBytes(B));
fout.write(UTF8.getBytes(A));
// write content of file
copy(fout, fin, 512);
}
catch (final javax.crypto.IllegalBlockSizeException e) {System.err.println("ERROR:" + e.getMessage());}
catch (final javax.crypto.BadPaddingException e) {System.err.println("ERROR:" + e.getMessage());}
// finished files
fin.close();
fout.close();
} catch (final FileNotFoundException e) {
System.err.println("ERROR: file '" + inFileName + "' not found");
} catch (final IOException e) {
System.err.println("ERROR: IO trouble");