Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import javax.swing.JOptionPane;
- public class Encrypt {
- private static int factor1 = 1;
- private static int factor2 = 256;
- private static int factor3 = 256*256;
- private static int factor4 = 256*256*256;
- private static int salt_multiplier = 7;
- private static int salt_addition = 3;
- private static InputStream is;
- private static OutputStream os;
- private static long key;
- private static byte[] header = "RGSSAD\000\001".getBytes();
- private static final String[] dirs = {"Data", "Graphics"};
- private static byte[] writeFiledata(long length) throws IOException {
- byte[] result = new byte[(int) length];
- long x = 0;
- int offset = 0;
- while (offset < length - 3) {
- x = readLong(is) ^ key;
- os.write((byte) x);
- os.write((byte) (x / factor2));
- os.write((byte) (x / factor3));
- os.write((byte) (x / factor4));
- key = changeKey(key);
- offset += 4;
- }
- if (offset < length) {
- x = readLong(is, (int) (length-offset)) ^ key;
- os.write((byte) x);
- if (offset < length - 1) {
- os.write((byte) (x / factor2));
- }
- if (offset < length - 2) {
- os.write((byte) (x / factor3));
- }
- key = changeKey(key);
- }
- return result;
- }
- private static void writeFilename(String filename, long length) throws IOException {
- long x = 0;
- for (int i = 0; i < length; i++) {
- x = (filename.charAt(i) ^ (key & 0xFFL));
- os.write((int)x);
- key = changeKey(key);
- }
- }
- private static long changeKey(long key) {
- return (key * salt_multiplier + salt_addition) & 0xFFFFFFFFL;
- }
- private static long readLong(InputStream is) throws IOException {
- long i1 = is.read();
- long i2 = is.read();
- long i3 = is.read();
- long i4 = is.read();
- return i1*factor1 + i2*factor2 + i3*factor3 + i4*factor4;
- }
- private static long readLong(InputStream is, int length) throws IOException {
- long i1 = is.read();
- long i2 = 0;
- long i3 = 0;
- if (length > 1)
- i2 = is.read();
- if (length > 2)
- i3 = is.read();
- return i1*factor1 + i2*factor2 + i3*factor3;
- }
- private static void writeLong(long l) throws IOException {
- int b1 = (int) (l % factor2) / factor1;
- int b2 = (int) (l % factor3) / factor2;
- int b3 = (int) (l % factor4) / factor3;
- int b4 = (int) (l / factor4);
- os.write(b1);
- os.write(b2);
- os.write(b3);
- os.write(b4);
- }
- private static void encryptFiles(File directory) throws IOException {
- for (File file : directory.listFiles()) {
- if (file.isDirectory())
- encryptFiles(file);
- else
- {
- encryptFile(file);
- }
- }
- }
- public static void main(String[] args) {
- doEncryption();
- }
- public static void doEncryption() {
- boolean success = false;
- for (String dirString : dirs) {
- if (new File(dirString).isDirectory()) {
- success = true;
- }
- }
- if (!success) {
- JOptionPane.showMessageDialog(null, "Nothing to encrypt");
- return;
- }
- Object[] options = { "RPG Maker XP", "RPG Maker VX", "Cancel" };
- int i = JOptionPane.showOptionDialog(null, "Choose encryption type:",
- "RPG Maker XP/VX Encrypter", JOptionPane.DEFAULT_OPTION,
- JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
- String filename = "";
- switch (i) {
- case 0:
- filename = "Game.rgssad";
- break;
- case 1:
- filename = "Game.rgss2a";
- break;
- default:
- return;
- }
- try {
- key = 0xDEADCAFEL;
- os = new BufferedOutputStream(new FileOutputStream(filename));
- // Write the header
- os.write(header);
- // Encrypt all the files in the relevant directories
- for (String dirString : dirs) {
- File dir = new File(dirString);
- if (dir.isDirectory()) {
- encryptFiles(dir);
- }
- }
- os.close();
- JOptionPane.showMessageDialog(null, "Encryption completed!");
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Encryption failed!", "An error has occurred",
- JOptionPane.ERROR_MESSAGE);
- }
- }
- public static void encryptFile(File file) throws IOException {
- is = new BufferedInputStream(new FileInputStream(file));
- String path = file.getPath();
- // Write the size of the filename
- long size = path.length() ^ key;
- writeLong(size);
- // Change the key
- key = changeKey(key);
- // Write the filename
- writeFilename(path, path.length());
- // Write the size of the file data
- size = file.length() ^ key;
- writeLong(size);
- // Change the key
- key = changeKey(key);
- // Remember the key
- long rkey = key;
- // Write the file data
- writeFiledata(file.length());
- // Restore key
- key = rkey;
- is.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement