Advertisement
Zeriab

RGSS(2) Encrypter source code

Oct 12th, 2011
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9.  
  10. import javax.swing.JOptionPane;
  11.  
  12. public class Encrypt {
  13.     private static int factor1 = 1;
  14.     private static int factor2 = 256;
  15.     private static int factor3 = 256*256;
  16.     private static int factor4 = 256*256*256;
  17.     private static int salt_multiplier = 7;
  18.     private static int salt_addition = 3;
  19.    
  20.     private static InputStream is;
  21.     private static OutputStream os;
  22.     private static long key;
  23.     private static byte[] header = "RGSSAD\000\001".getBytes();
  24.     private static final String[] dirs = {"Data", "Graphics"};
  25.  
  26.     private static byte[] writeFiledata(long length) throws IOException {
  27.         byte[] result = new byte[(int) length];
  28.         long x = 0;
  29.         int offset = 0;
  30.         while (offset < length - 3) {
  31.             x = readLong(is) ^ key;
  32.             os.write((byte) x);
  33.             os.write((byte) (x / factor2));
  34.             os.write((byte) (x / factor3));
  35.             os.write((byte) (x / factor4));
  36.             key = changeKey(key);
  37.             offset += 4;
  38.         }
  39.         if (offset < length) {
  40.             x = readLong(is, (int) (length-offset)) ^ key;
  41.             os.write((byte) x);
  42.             if (offset < length - 1) {
  43.                 os.write((byte) (x / factor2));
  44.             }
  45.             if (offset < length - 2) {
  46.                 os.write((byte) (x / factor3));
  47.             }
  48.             key = changeKey(key);
  49.         }
  50.         return result;
  51.     }
  52.    
  53.     private static void writeFilename(String filename, long length) throws IOException {
  54.         long x = 0;
  55.         for (int i = 0; i < length; i++) {
  56.             x = (filename.charAt(i) ^ (key & 0xFFL));
  57.             os.write((int)x);
  58.             key = changeKey(key);
  59.         }
  60.     }
  61.    
  62.     private static long changeKey(long key) {
  63.         return (key * salt_multiplier + salt_addition) & 0xFFFFFFFFL;
  64.     }
  65.  
  66.     private static long readLong(InputStream is) throws IOException {
  67.         long i1 = is.read();
  68.         long i2 = is.read();
  69.         long i3 = is.read();
  70.         long i4 = is.read();
  71.         return i1*factor1 + i2*factor2 + i3*factor3 + i4*factor4;
  72.     }
  73.    
  74.     private static long readLong(InputStream is, int length) throws IOException {
  75.         long i1 = is.read();
  76.         long i2 = 0;
  77.         long i3 = 0;
  78.         if (length > 1)
  79.             i2 = is.read();
  80.         if (length > 2)
  81.             i3 = is.read();
  82.         return i1*factor1 + i2*factor2 + i3*factor3;
  83.     }
  84.    
  85.     private static void writeLong(long l) throws IOException {
  86.         int b1 = (int) (l % factor2) / factor1;
  87.         int b2 = (int) (l % factor3) / factor2;
  88.         int b3 = (int) (l % factor4) / factor3;
  89.         int b4 = (int) (l / factor4);
  90.         os.write(b1);
  91.         os.write(b2);
  92.         os.write(b3);
  93.         os.write(b4);
  94.     }
  95.    
  96.     private static void encryptFiles(File directory) throws IOException {
  97.         for (File file :  directory.listFiles()) {
  98.             if (file.isDirectory())
  99.                 encryptFiles(file);
  100.             else
  101.             {
  102.                 encryptFile(file);
  103.             }
  104.         }
  105.     }
  106.    
  107.     public static void main(String[] args) {
  108.         doEncryption();
  109.     }
  110.    
  111.     public static void doEncryption() {
  112.         boolean success = false;
  113.         for (String dirString : dirs) {
  114.             if (new File(dirString).isDirectory()) {
  115.                 success = true;
  116.             }
  117.         }
  118.         if (!success) {
  119.             JOptionPane.showMessageDialog(null, "Nothing to encrypt");
  120.             return;
  121.         }
  122.        
  123.         Object[] options = { "RPG Maker XP", "RPG Maker VX", "Cancel" };
  124.         int i = JOptionPane.showOptionDialog(null, "Choose encryption type:",
  125.                 "RPG Maker XP/VX Encrypter", JOptionPane.DEFAULT_OPTION,
  126.                 JOptionPane.QUESTION_MESSAGE, null, options, options[0]);      
  127.        
  128.         String filename = "";
  129.         switch (i) {
  130.         case 0:
  131.             filename = "Game.rgssad";
  132.             break;
  133.         case 1:
  134.             filename = "Game.rgss2a";
  135.             break;
  136.         default:
  137.             return;
  138.         }
  139.        
  140.         try {
  141.             key = 0xDEADCAFEL;
  142.             os = new BufferedOutputStream(new FileOutputStream(filename));
  143.            
  144.             // Write the header
  145.             os.write(header);
  146.            
  147.             // Encrypt all the files in the relevant directories
  148.             for (String dirString : dirs) {
  149.                 File dir = new File(dirString);
  150.                 if (dir.isDirectory()) {
  151.                     encryptFiles(dir);
  152.                 }
  153.             }
  154.            
  155.             os.close();
  156.            
  157.             JOptionPane.showMessageDialog(null, "Encryption completed!");
  158.         } catch (Exception e) {
  159.             JOptionPane.showMessageDialog(null, "Encryption failed!", "An error has occurred",
  160.                     JOptionPane.ERROR_MESSAGE);
  161.         }
  162.     }
  163.    
  164.     public static void encryptFile(File file) throws IOException {
  165.         is = new BufferedInputStream(new FileInputStream(file));
  166.        
  167.         String path = file.getPath();
  168.         // Write the size of the filename
  169.         long size = path.length() ^ key;
  170.         writeLong(size);
  171.         // Change the key
  172.         key = changeKey(key);
  173.         // Write the filename
  174.         writeFilename(path, path.length());
  175.         // Write the size of the file data
  176.         size = file.length() ^ key;
  177.         writeLong(size);
  178.         // Change the key
  179.         key = changeKey(key);
  180.         // Remember the key
  181.         long rkey = key;
  182.         // Write the file data
  183.         writeFiledata(file.length());
  184.         // Restore key
  185.         key = rkey;
  186.        
  187.         is.close();
  188.     }
  189. }
  190.  
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement