pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Java pastebin - collaborative debugging tool View Help


Posted by Mejf on Mon 6 Apr 21:03
report abuse | download | new post

  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. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post