FolderEncryptor is a Java program designed to provide file encryption and decryption functionality within a folder using the AES (Advanced Encryption Standard) algorithm with a user-specified password. This tool is intended for securing sensitive files on your local storage.
FolderEncryptor operates as follows:
- The
encryptFolder
method is invoked with a folder to encrypt and a user-provided password. - It recursively traverses the folder structure.
- For each file encountered, it reads the file's content into
fileBytes
. - The
encrypt
method is called withfileBytes
and the password. - Inside the
encrypt
method, a secret key is generated from the user's password. - The AES encryption algorithm is initialized with this secret key.
- The file content (
fileBytes
) is encrypted using AES encryption, resulting inencryptedBytes
. - The encrypted content is written to a new file with the extension
.hidden
. - The original file is securely deleted to prevent data leakage.
- The
decryptFolder
method is invoked with a folder to decrypt and the corresponding password. - It recursively traverses the folder structure.
- For each
.hidden
file encountered, it reads its content intofileBytes
. - The
decrypt
method is called withfileBytes
and the password. - Inside the
decrypt
method, the secret key is generated from the provided password. - The AES decryption algorithm is initialized with this secret key.
- The file content (
fileBytes
) is decrypted using AES decryption, resulting indecryptedBytes
. - The decrypted content is written to a new file with the
.hidden
extension removed. - The original
.hidden
file is securely deleted.
- The
generateKey
method creates a 128-bit (16-byte) secret key from the provided password. This key is used for both encryption and decryption. For AES-256 encryption, ensure that your password is 32 bytes long, and consider implementing a secure key derivation function (KDF) for stronger key generation.
- AES encryption is performed in ECB mode with PKCS5 padding (
"AES/ECB/PKCS5Padding"
).
You can download the java file that I have provided. then place them in the respective java src, making sure to change the package according to your application,
Use compileSdkVersion 32
in build.gradle.
How to start encrypted?
Encrypted folder containing files:
File folderToEncrypt = new File("/storage/emulated/0/MyFolder");
FolderEncryptor.encryptFolder(folderToEncrypt, "MyPassword");
Decrypted:
File folderToDecrypt = new File("/storage/emulated/0/MyFolder");
FolderEncryptor.decryptFolder(folderToDecrypt, "MyPassword");
How to encrypt 1 file?
File fileToEncrypt = new File("/storage/emulated/0/MyFiles.txt");
String encryptionPassword = "your_password_here";
FolderEncryptor.encryptFile(fileToEncrypt, encryptionPassword);
Decrypted:
File decryptFile = new File("/storage/emulated/0/MyFiles.txt");
String decryptPassword = "your_password_here";
FolderEncryptor.decryptFile(decryptFile, decryptPassword);
note: You can custom extension
AES-128:
private static Key generateKey(String password) {
try {
byte[] passwordBytes = Arrays.copyOf(password.getBytes(StandardCharsets.UTF_8), 16);
return new SecretKeySpec(passwordBytes, ALGORITHM);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
AES-192:
private static Key generateKey(String password) {
try {
byte[] passwordBytes = Arrays.copyOf(password.getBytes(StandardCharsets.UTF_8), 24);
return new SecretKeySpec(passwordBytes, ALGORITHM);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
AES-256:
private static Key generateKey(String password) {
try {
byte[] passwordBytes = Arrays.copyOf(password.getBytes(StandardCharsets.UTF_8), 32);
return new SecretKeySpec(passwordBytes, ALGORITHM);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
While this code provides a basic example of file encryption and decryption, it is essential to consider additional security measures for real-world applications:
- Securely storing and managing encryption keys to protect against unauthorized access.
- Handling exceptions and errors gracefully to prevent potential vulnerabilities.
- Ensuring data integrity through methods like message authentication codes (MACs) to guard against tampering.
- Using strong password policies and implementing robust key derivation functions (KDFs) for secure key generation.
This code is intended for educational purposes and serves as a starting point for understanding file encryption. It may not provide the level of security required for production use. Always consult security best practices and consider using established encryption libraries and frameworks for security-critical applications.
This project is licensed under the MIT License - see the LICENSE file for details.