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:04
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. import java.util.Random;
  9.  
  10. public class CCrypt {
  11.  
  12.         public static void main(String[] args) {
  13.                 if (args.length < 2) {
  14.                         System.err
  15.                                         .println("Usage: java CCrypt <file1> <file2>\n"
  16.                                                         + "You will get one file called C, containing the cryptotext, "
  17.                                                         + "one file called K which decrypts C into M and "
  18.                                                         + "one file called K2 which decrypts C into M2.");
  19.                         System.exit(-1);
  20.                 }
  21.                 File f1 = new File(args[0]);
  22.                 File f2 = new File(args[1]);
  23.  
  24.                 if (f1.length() > f2.length()) {
  25.                         File temp = f1;
  26.                         f1 = f2;
  27.                         f2 = temp;
  28.                 }
  29.  
  30.                 doFirst(f1);
  31.  
  32.                 doSecond(f2);
  33.  
  34.         }
  35.  
  36.         private static void doSecond(File f2) {
  37.                 try {
  38.                         /**
  39.                          * From M2 and C we create K2 by K2 = M2 + C. Then M2 = C + K2 and M
  40.                          * = C + K
  41.                          */
  42.                         DataInputStream crypt = new DataInputStream(
  43.                                         new FileInputStream("C"));
  44.                         DataInputStream in = new DataInputStream(new FileInputStream(f2));
  45.                         DataOutputStream key = new DataOutputStream(new FileOutputStream(
  46.                                         "K2"));
  47.  
  48.                         key.writeInt(f2.getName().length());
  49.                         for (int l = 0; l < f2.getName().length(); l++)
  50.                                 key.writeChar(f2.getName().charAt(l));
  51.  
  52.                         int b;
  53.                         while ((b = in.read()) >= 0) {
  54.                                 key.write(b ^ crypt.read());
  55.                         }
  56.  
  57.                         crypt.close();
  58.                         in.close();
  59.                         key.close();
  60.  
  61.                 } catch (FileNotFoundException e) {
  62.                         System.err.println("File not found: " + f2);
  63.                         System.exit(-1);
  64.                 } catch (IOException e) {
  65.                         e.printStackTrace();
  66.                 }
  67.         }
  68.  
  69.         private static void doFirst(File f1) {
  70.                 try {
  71.                         /**
  72.                          * From M and a random K we create C, by C = M ^ K.
  73.                          */
  74.                         Random rand = new Random();
  75.                         DataOutputStream crypt = new DataOutputStream(new FileOutputStream(
  76.                                         "C"));
  77.                         DataInputStream in = new DataInputStream(new FileInputStream(f1));
  78.                         DataOutputStream key = new DataOutputStream(new FileOutputStream(
  79.                                         "K"));
  80.  
  81.                         key.writeInt(f1.getName().length());
  82.                         for (int l = 0; l < f1.getName().length(); l++)
  83.                                 key.writeChar(f1.getName().charAt(l));
  84.  
  85.                         int b;
  86.                         while ((b = in.read()) >= 0) {
  87.                                 int k = rand.nextInt();
  88.                                 key.write(k);
  89.                                 crypt.write(b ^ k);
  90.                         }
  91.  
  92.                         crypt.close();
  93.                         in.close();
  94.                         key.close();
  95.  
  96.                 } catch (FileNotFoundException e) {
  97.                         System.err.println("File not found: " + f1);
  98.                         System.exit(-1);
  99.                 } catch (IOException e) {
  100.                         e.printStackTrace();
  101.                 }
  102.         }
  103.  
  104. }

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