Skip to content

Instantly share code, notes, and snippets.

@allanfreitas
Forked from hsanger/Encryptor.java
Created February 19, 2023 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allanfreitas/f7dc9070ca7c42d9214d458becd9628d to your computer and use it in GitHub Desktop.
Save allanfreitas/f7dc9070ca7c42d9214d458becd9628d to your computer and use it in GitHub Desktop.
Methods to encrypt and decrypt Strings and files (file encryption and decryption doesn't work for photos). Requires Apache Commons Lang.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class Encryptor {
public static void main(String[] args) throws Exception {
String original = "Hello World!";
System.out.println("Original: " + original);
String encrypted = encrypt("abc123ghidef9123", "RandomInitVector", original);
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypt("abc123ghidef9123", "RandomInitVector", encrypted));
}
public static void encryptFile(File f, String key, String iv) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(f));
int i = 0;
String line = null;
while ((line = br.readLine()) != null) {
i++;
}
String[] linesToEncrypt = new String[i];
BufferedReader br1 = new BufferedReader(new FileReader(f));
int i1 = 0;
String line1 = null;
while ((line1 = br1.readLine()) != null) {
linesToEncrypt[i1] = line1;
System.out.println("Processing line " + i1 + ": " + line1);
i1++;
}
String[] encryptedLines = new String[i];
Arrays.fill(encryptedLines, "");
int i2 = 0;
for(String line2 : linesToEncrypt) {
System.out.println(line2);
encryptedLines[i2] = encrypt(key, iv, line2);
i2++;
}
f.delete();
f.createNewFile();
for(String line3 : encryptedLines) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
String data = line3 + "\n";
fw = new FileWriter(f.getAbsoluteFile(), true);
bw = new BufferedWriter(fw);
bw.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public static void decryptFile(File f, String key, String iv) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(f));
int i = 0;
String line = null;
while ((line = br.readLine()) != null) {
i++;
}
String[] linesToDecrypt = new String[i];
BufferedReader br1 = new BufferedReader(new FileReader(f));
int i1 = 0;
String line1 = null;
while ((line1 = br1.readLine()) != null) {
linesToDecrypt[i1] = line1;
System.out.println("Processing line " + i1 + ": " + line1);
i1++;
}
String[] decryptedLines = new String[i];
Arrays.fill(decryptedLines, "");
int i2 = 0;
for(String line2 : linesToDecrypt) {
System.out.println(line2);
decryptedLines[i2] = decrypt(key, iv, line2);
i2++;
}
f.delete();
f.createNewFile();
for(String line3 : decryptedLines) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
String data = line3 + "\n";
fw = new FileWriter(f.getAbsoluteFile(), true);
bw = new BufferedWriter(fw);
bw.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public static String encrypt(String key, String initVector, String value) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
return Base64.encodeBase64String(encrypted);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static String decrypt(String key, String initVector, String encrypted) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
return new String(original);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment