Advertisement
Guest User

Mejf

a guest
Apr 6th, 2009
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  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. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement