Skip to content

Instantly share code, notes, and snippets.

@punchouty
Last active March 5, 2024 16:35
Show Gist options
  • Save punchouty/9071ae87313817369560 to your computer and use it in GitHub Desktop.
Save punchouty/9071ae87313817369560 to your computer and use it in GitHub Desktop.
Cryptography with AES Algorrithm
package com.punchouty.ed;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
public class Main {
public static final String FOLDER = "/Users/rajan.punchouty/tmp/misc";
public static final File folder = new File(FOLDER);
public static final String ENCRYPTION_DECRYPTION_ALGORITHM = "PBEWithMD5AndDES";
private static final String password = "anomamrahs"; // Password to encrypt or decrypt - Folder Name
private static SecretKeyFactory keyFac;
private static SecretKey pbeKey;
// Salt
private static byte[] salt = "17111973".getBytes(); //random string of eight charachters - Real DOB
// Iteration count
private static int count = 20;
private static PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt,count);
private static PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
public static void main(String[] args) throws Exception {
keyFac = SecretKeyFactory.getInstance(ENCRYPTION_DECRYPTION_ALGORITHM);
pbeKey = keyFac.generateSecret(pbeKeySpec);
decrypt();
//encrypt();
}
public static void encrypt() throws NoSuchAlgorithmException, InvalidKeySpecException, Exception {
File [] files = folder.listFiles((File dir, String name) -> {
if(name.endsWith(".jpg") || name.endsWith(".m4v")|| name.endsWith(".mp4")) return true;
return false;
});
int index = 1;
for (File file : files) {
System.out.println("Processing - " + file.getAbsolutePath());
if(file.getName().endsWith(".jpg")) {
encrypt(pbeKey, ENCRYPTION_DECRYPTION_ALGORITHM, file.getAbsolutePath(), FOLDER + "/" + index + "_jpg.aes");
}
else if(file.getName().endsWith(".m4v")) {
encrypt(pbeKey, ENCRYPTION_DECRYPTION_ALGORITHM, file.getAbsolutePath(), FOLDER + "/" + index + "_m4v.aes");
}
else if(file.getName().endsWith(".mp4")) {
encrypt(pbeKey, ENCRYPTION_DECRYPTION_ALGORITHM, file.getAbsolutePath(), FOLDER + "/" + index + "_mp4.aes");
}
else {
System.err.println("Unidentified Format");
}
index++;
}
}
public static void decrypt() throws NoSuchAlgorithmException, InvalidKeySpecException, Exception {
File [] files = folder.listFiles((File dir, String name) -> {
if(name.endsWith(".aes")) return true;
return false;
});
int index = 1;
for (File file : files) {
System.out.println("Processing - " + file.getAbsolutePath());
if(file.getName().contains("jpg")) {
decrypt(pbeKey, ENCRYPTION_DECRYPTION_ALGORITHM, file.getAbsolutePath(), FOLDER + "/" + index + ".jpg");
}
else if(file.getName().contains("m4v")) {
decrypt(pbeKey, ENCRYPTION_DECRYPTION_ALGORITHM, file.getAbsolutePath(), FOLDER + "/" + index + ".m4v");
}
else if(file.getName().contains("mp4")) {
decrypt(pbeKey, ENCRYPTION_DECRYPTION_ALGORITHM, file.getAbsolutePath(), FOLDER + "/" + index + ".mp4");
}
else {
System.err.println("Unidentified Format");
}
index++;
}
}
private static void encrypt(Key key, String algorithm, String fileToEncrypt, String encryptedFile) throws Exception {
File fileInput = new File(fileToEncrypt);
File fileOutput = new File(encryptedFile);
FileInputStream fis = new FileInputStream(fileInput);
FileOutputStream fos = new FileOutputStream(fileOutput);
Cipher encrypt = Cipher.getInstance(algorithm);
encrypt.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec);
CipherOutputStream cout = new CipherOutputStream(fos, encrypt);
byte[] buf = new byte[1024];
int read;
while ((read = fis.read(buf)) != -1) {
// reading data
cout.write(buf, 0, read); // writing encrypted data
}
// closing streams
cout.flush();
cout.close();
fis.close();
}
private static void decrypt(Key key, String algorithm, String fileToDecrypt, String decryptedFile) throws Exception {
File fileInput = new File(fileToDecrypt);
File fileOutput = new File(decryptedFile);
FileInputStream fis = new FileInputStream(fileInput);
FileOutputStream fos = new FileOutputStream(fileOutput);
Cipher decrypt = Cipher.getInstance(algorithm);
decrypt.init(Cipher.DECRYPT_MODE, key, pbeParamSpec);
CipherInputStream cin = new CipherInputStream(fis, decrypt);
byte[] buf = new byte[1024];
int read = 0;
while ((read = cin.read(buf)) != -1) { // reading encrypted data
fos.write(buf, 0, read); // writing decrypted data
}
cin.close();
fos.flush();
fos.close();
System.out.println("Successfully Decrypted");
}
public static final String getHexString(byte[] b) {
String result = "";
for (int i = 0; i < b.length; i++) {
result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment