Share Pastebin
Guest
Public paste!

Mejf

By: a guest | Apr 6th, 2009 | Syntax: Java | Size: 1.63 KB | Hits: 338 | Expires: Never
Copy text to clipboard
  1. import java.io.DataInputStream;
  2. import java.io.DataOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8.  
  9. public class CDeCrypt {
  10.  
  11.         public static void main(String[] args) {
  12.                 if (args.length < 2) {
  13.                         System.err
  14.                                         .println("Usage: java CDeCrypt <keyfile> <cryptfile> [<outdirectory>]\n"
  15.                                                         + "The decrypted file will get its original name and weill be placed in the optional <outdirectory>.");
  16.                         System.exit(-1);
  17.                 }
  18.  
  19.                 File key = new File(args[0]);
  20.                 File crypt = new File(args[1]);
  21.                 File outDir = new File(args.length > 2 ? args[2] : ".");
  22.  
  23.                 decrypt(key, crypt, outDir);
  24.         }
  25.  
  26.         private static void decrypt(File key, File crypt, File outDir) {
  27.                 try {
  28.                         DataInputStream k = new DataInputStream(new FileInputStream(key));
  29.                         DataInputStream c = new DataInputStream(new FileInputStream(crypt));
  30.  
  31.                         int mNameLen = k.readInt();
  32.                         StringBuilder sb = new StringBuilder();
  33.                         while (mNameLen > 0) {
  34.                                 sb.append(k.readChar());
  35.                                 mNameLen--;
  36.                         }
  37.                         String mName = sb.toString();
  38.                         File mFile = new File(outDir, mName);
  39.  
  40.                         DataOutputStream m = new DataOutputStream(new FileOutputStream(
  41.                                         mFile));
  42.  
  43.                         System.out.print("Decrypting " + crypt + " using key " + key
  44.                                         + " to file " + mFile + " ... ");
  45.  
  46.                         int b;
  47.                         while ((b = k.read()) >= 0) {
  48.                                 m.write(b ^ c.read());
  49.                         }
  50.  
  51.                         k.close();
  52.                         c.close();
  53.                         m.close();
  54.  
  55.                         System.out.println("done!");
  56.                 } catch (FileNotFoundException e) {
  57.                         e.printStackTrace();
  58.                 } catch (IOException e) {
  59.                         e.printStackTrace();
  60.                 }
  61.         }
  62.  
  63. }