11package com .cryptoexamples .java ;
22
3- import javax .crypto .*;
3+ import javax .crypto .Cipher ;
4+ import javax .crypto .CipherInputStream ;
5+ import javax .crypto .CipherOutputStream ;
6+ import javax .crypto .KeyGenerator ;
7+ import javax .crypto .NoSuchPaddingException ;
8+ import javax .crypto .SecretKey ;
9+ import javax .crypto .SecretKeyFactory ;
410import javax .crypto .spec .GCMParameterSpec ;
511import javax .crypto .spec .PBEKeySpec ;
612import javax .crypto .spec .SecretKeySpec ;
1420import java .util .logging .Logger ;
1521
1622/**
17- * All in one example for encryption and decryption of a file in one method;
18- * Including
23+ * Example for encryption and decryption of a file in one method.
1924 * - Random password generation using strong secure random number generator
2025 * - Random salt generation
2126 * - Key derivation using PBKDF2 HMAC SHA-512,
2227 * - AES-256 authenticated encryption using GCM
2328 * - BASE64-encoding as representation for the byte-arrays
2429 * - Exception handling
2530 */
26- public class ExampleFileEncryptionInOneMethod {
27- private static final Logger LOGGER = Logger .getLogger (ExampleFileEncryptionInOneMethod .class .getName ());
31+ public class ExampleFileEncryption {
32+ private static final Logger LOGGER = Logger .getLogger (ExampleFileEncryption .class .getName ());
2833
29- public static void main (String [] args ) {
30- String plainText = "Multiline text:" ;
34+ /**
35+ * Demonstrational method that encrypts a file using a password (that is used to derive the required key).
36+ * @param fileName
37+ * @param plainText
38+ * @param password
39+ * @return true if encryption and decryption were successful, false otherwise
40+ */
41+ public static boolean demonstrateFileEncryption (String fileName , String plainText , String password ) {
3142 try {
32- String password = null ;
3343 // GENERATE password (not needed if you have a password already)
3444 if (password == null || password .isEmpty ()) {
3545 KeyGenerator keyGen = KeyGenerator .getInstance ("AES" );
@@ -61,37 +71,42 @@ public static void main(String[] args) {
6171
6272 // SET UP OUTPUT STREAM and write content of String
6373 try (
64- FileOutputStream fileOutputStream = new FileOutputStream ("encryptedFile.enc" );
74+ FileOutputStream fileOutputStream = new FileOutputStream (fileName );
6575 CipherOutputStream encryptedOutputStream = new CipherOutputStream (fileOutputStream , cipher );
6676 InputStream stringInputStream = new ByteArrayInputStream (plainText .getBytes (StandardCharsets .UTF_8 ))
6777 ) {
6878 byte [] buffer = new byte [8192 ];
69- while (stringInputStream .read (buffer ) > 0 ) {
70- encryptedOutputStream .write (buffer );
79+ int nread ;
80+ while ((nread = stringInputStream .read (buffer )) > 0 ) {
81+ encryptedOutputStream .write (buffer , 0 , nread );
7182 }
83+ encryptedOutputStream .flush ();
7284 }
7385
7486 // READ ENCRYPTED FILE
7587 StringBuilder stringBuilder = new StringBuilder ();
7688 cipher .init (Cipher .DECRYPT_MODE , key , spec );
77-
89+ String decryptedCipherText ;
7890 try (
79- FileInputStream fileInputStream = new FileInputStream ("encryptedFile.enc" );
80- CipherInputStream cipherInputStream = new CipherInputStream (fileInputStream , cipher )
91+ FileInputStream fileInputStream = new FileInputStream (fileName );
92+ CipherInputStream cipherInputStream = new CipherInputStream (fileInputStream , cipher );
93+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
8194 ) {
8295 byte [] buffer = new byte [8192 ];
83- while (cipherInputStream .read (buffer ) > 0 ) {
84- stringBuilder .append (new String (buffer , StandardCharsets .UTF_8 ));
96+ int nread ;
97+ while ((nread = cipherInputStream .read (buffer )) > 0 ) {
98+ byteArrayOutputStream .write (buffer , 0 , nread );
8599 }
100+ byteArrayOutputStream .flush ();
101+ decryptedCipherText = new String (byteArrayOutputStream .toByteArray (), StandardCharsets .UTF_8 );
86102 }
87- // TODO trim() should not be needed!
88- String decryptedCipherText = stringBuilder .toString ().trim ();
89- LOGGER .log (Level .INFO , decryptedCipherText );
90103
104+ LOGGER .log (Level .INFO , decryptedCipherText );
91105 LOGGER .log (Level .INFO ,
92106 () -> String .format ("Decrypted file content and original plain text are the same: %b" ,
93107 decryptedCipherText .compareTo (plainText ) == 0 )
94108 );
109+ return decryptedCipherText .compareTo (plainText ) == 0 ;
95110 } catch (NoSuchAlgorithmException |
96111 NoSuchPaddingException |
97112 InvalidKeyException |
@@ -100,7 +115,13 @@ public static void main(String[] args) {
100115 InvalidKeySpecException |
101116 IOException e ) {
102117 LOGGER .log (Level .SEVERE , e .getLocalizedMessage ());
118+ return false ;
103119 }
104120 }
105121
122+
123+ public static void main (String [] args ) {
124+ demonstrateFileEncryption ("encryptedFile.enc" ,"Multiline text:\n Multiline text:\n " ,null );
125+ }
126+
106127}
0 commit comments