Last active
May 30, 2020 16:27
-
-
Save Terkea/2611756f1bfbec6c5f44e36c608f6732 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.crypto.*; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Base64; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import static javax.crypto.Cipher.DECRYPT_MODE; | |
import static javax.crypto.Cipher.ENCRYPT_MODE; | |
public class Main { | |
private static String ALGORITHM = "AES"; | |
private static SecretKey secretKey; | |
private static Cipher encrypter; | |
private static KeyGenerator keyGenerator; | |
private static SecretKey generateKey() throws NoSuchAlgorithmException { | |
return KeyGenerator.getInstance(ALGORITHM).generateKey(); | |
} | |
private static String encodeKey(SecretKey key){ | |
return Base64.getEncoder().encodeToString(key.getEncoded()); | |
} | |
private static byte[] decodeKey(String encodeKey){ | |
return Base64.getDecoder().decode(encodeKey); | |
} | |
private static SecretKey reconstructKey(byte[] decodedKey){ | |
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, ALGORITHM); | |
return originalKey; | |
} | |
public static String encrypt(Path mainFolder) throws NoSuchAlgorithmException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException{ | |
SecretKey generatedKey = generateKey(); | |
String encodedKey = encodeKey(generatedKey); | |
System.out.println("ENCODED KEY: " + encodedKey); | |
keyGenerator = KeyGenerator.getInstance(ALGORITHM); | |
try { | |
encrypter = Cipher.getInstance(ALGORITHM); | |
} catch (NoSuchPaddingException e) { | |
e.printStackTrace(); | |
} | |
encrypter.init(ENCRYPT_MODE, generatedKey); | |
List<File> filesInFolder = Files.walk(mainFolder) | |
.filter(Files::isRegularFile) | |
.map(Path::toFile) | |
.collect(Collectors.toList()); | |
for (int i = 0; i<filesInFolder.size(); i++){ | |
FileInputStream inputStream = new FileInputStream(filesInFolder.get(i)); | |
byte[] inputBytes = new byte[(int) filesInFolder.get(i).length()]; | |
inputStream.read(inputBytes); | |
byte[] outputBytes = encrypter.doFinal(inputBytes); | |
FileOutputStream outputStream = new FileOutputStream(filesInFolder.get(i)); | |
outputStream.write(outputBytes); | |
} | |
return encodedKey; | |
} | |
public static void decrypt(Path mainFolder, String key) throws NoSuchAlgorithmException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException{ | |
byte[] decodedKey = decodeKey(key); | |
SecretKey reconstructKey = reconstructKey(decodedKey); | |
keyGenerator = KeyGenerator.getInstance(ALGORITHM); | |
try { | |
encrypter = Cipher.getInstance(ALGORITHM); | |
} catch (NoSuchPaddingException e) { | |
e.printStackTrace(); | |
} | |
; | |
encrypter.init(DECRYPT_MODE, reconstructKey); | |
List<File> filesInFolder = Files.walk(mainFolder) | |
.filter(Files::isRegularFile) | |
.map(Path::toFile) | |
.collect(Collectors.toList()); | |
for (int i = 0; i<filesInFolder.size(); i++){ | |
FileInputStream inputStream = new FileInputStream(filesInFolder.get(i)); | |
byte[] inputBytes = new byte[(int) filesInFolder.get(i).length()]; | |
inputStream.read(inputBytes); | |
byte[] outputBytes = encrypter.doFinal(inputBytes); | |
FileOutputStream outputStream = new FileOutputStream(filesInFolder.get(i)); | |
outputStream.write(outputBytes); | |
} | |
} | |
public static void main(String args[]){ | |
try { | |
decrypt(Paths.get(System.getProperty("user.home") + "\\Documents\\test"), "iU87ZezdF0IZla+beBNpzg=="); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} catch (InvalidKeyException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (BadPaddingException e) { | |
e.printStackTrace(); | |
} catch (IllegalBlockSizeException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment