· 7 years ago · Jun 23, 2018, 12:48 AM
1package com.syntax.crypter;
2
3import javax.crypto.KeyGenerator;
4import javax.crypto.SecretKey;
5import javax.crypto.Cipher;
6import javax.crypto.spec.SecretKeySpec;
7import java.io.*;
8
9/**
10 * @author Synt[a]x
11 */
12public class Crypter {
13 public static void main(String[] argS) throws Exception {
14 File file = new File(argS[0]);
15 InputStream str = new FileInputStream(file);
16 OutputStream output = new FileOutputStream(new File(argS[1]));
17 long length = file.length();
18 if (length > Integer.MAX_VALUE) {
19 System.out.println("[Crypter] Filesize too large.");
20 }
21 byte[] fileBytes = new byte[(int) length]; //Array to load file into
22 int offset = 0;
23 int numRead = 0;
24 while (offset < fileBytes.length
25 && (numRead = str.read(fileBytes, offset, fileBytes.length - offset)) >= 0) {
26 offset += numRead;
27 str.close();
28 System.out.println("[Crypter] Read " + offset + " bytes.");
29 offset = 0;
30 System.out.println("[Crypter] Going to crypt using AES");
31 KeyGenerator keygen = KeyGenerator.getInstance("AES");
32 keygen.init(128);
33 SecretKey skey = keygen.generateKey();
34 byte[] raw = skey.getEncoded();
35 SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
36 Cipher cipher = Cipher.getInstance("AES");
37 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
38 byte[] aescrypted = cipher.doFinal(fileBytes);
39 output.write(aescrypted);
40 output.close();
41 System.out.println("[Crypter] Wrote " + aescrypted.length + " crypted bytes.");
42 }
43}