Advertisement
Guest User

JoaoSerra

a guest
Aug 29th, 2009
1,207
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.74 KB | None
  1. /*
  2.  *
  3.  * Copyright (c) 2009, Mulder3
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify it
  6.  * under the terms and conditions of the GNU General Public License,
  7.  * version 2, as published by the Free Software Foundation.
  8.  *
  9.  * This program is distributed in the hope it will be useful, but WITHOUT
  10.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12.  * more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License along with
  15.  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  16.  * Place - Suite 330, Boston, MA 02111-1307 USA.
  17.  *
  18.  * Authors:
  19.  *   Mulder3 <mulder3@mulder3.net>
  20.  *
  21.  */
  22.  
  23. import java.io.*;
  24. import java.security.spec.AlgorithmParameterSpec;
  25. import javax.crypto.spec.IvParameterSpec;
  26. import javax.crypto.spec.SecretKeySpec;
  27. import javax.crypto.Cipher;
  28.  
  29.  
  30. public class TV2xmlDecrypt {
  31.    
  32.     public static void main(String[] args) {
  33.  
  34.            System.out.println("Microsoft Mediaroom XML decrypt by Mulder3 <mulder3@mulder3.net>");
  35.            if (args.length!=3 || args[2].length()!=32){
  36.                System.out.println("Usage: TV2xmlDecrypt inFile outFile AESkey(16bytes hex string without spaces or colons)");
  37.                System.out.println("Example: TV2xmlDecrypt LoginEx_encrypted.bin LoginEx.xml 0123456789ABCDEF0123456789ABCDEF");
  38.                System.exit(0);
  39.            }
  40.            System.out.println("Trying to decrypt "+args[0]+"...");
  41.            try{
  42.                 FileInputStream inFile = new FileInputStream(new File(args[0]));
  43.                 FileOutputStream outFile = new FileOutputStream(new File(args[1]));
  44.                 decryptTV2xml(inFile, outFile, args[3]);
  45.                 inFile.close();
  46.                 outFile.close();
  47.            } catch(Exception e){
  48.                 e.printStackTrace();
  49.            }
  50.            System.out.println("File successfully decrypted... saved to "+args[1]+"...");
  51.     }  
  52.  
  53.     public static void decryptTV2xml(FileInputStream in, FileOutputStream out, String aeskey) throws Exception {
  54.        
  55.         boolean isFirstBock = true;
  56.         int offset = 0;
  57.         int padding = 0;
  58.         byte[] buffer;
  59.         byte[] buffer2;
  60.         byte[] decryptedXML = null;
  61.         byte[] iv = new byte[16];
  62.         byte[] OMAC1signature = new byte[16];
  63.         SecretKeySpec key = new SecretKeySpec(hexToByteArray(aeskey), "AES");
  64.  
  65.         while(true){
  66.             if (isFirstBock){
  67.               buffer = new byte[1+16+8+4096+16]; //padding/version byte + 16bytes iv + 8bytes ramdom data + 4096 bytes encrypted block + 16bytes OMAC1 signature
  68.               offset = in.read(buffer, 0, (1+16+8+4096+16));
  69.               if (offset==-1) break;
  70.               if (((buffer[0] >> 4) & 15) != 1){ System.out.println("ERROR: Stream version unknown"); System.exit(0);}
  71.               padding = buffer[0] & 15;
  72.               if (padding > 15){ System.out.println("ERROR: Invalid padding specified"); System.exit(0);}
  73.               System.arraycopy(buffer, 1, iv, 0, 16);
  74.               System.arraycopy(buffer, offset-16, OMAC1signature, 0, 16);
  75.               buffer2 = new byte[offset-(1+16+8+16)]; //padding/version byte + 16bytes iv + 8bytes ramdom data + 16bytes OMAC1 signature
  76.               System.arraycopy(buffer, (1+16+8), buffer2, 0, offset-(1+16+8+16));
  77.               //VerifyOMAC1signature(OMAC1signature,buffer2); //NOT IMPLEMENTED!!
  78.               decryptedXML=decryptAesBlock(buffer2,iv, key, padding);
  79.               out.write(decryptedXML, 0, decryptedXML.length);        
  80.               isFirstBock=false;
  81.  
  82.             } else {
  83.  
  84.               buffer = new byte[1+4096+16]; //padding/version byte + 4096 bytes encrypted block + 16bytes OMAC1 signature
  85.               offset = in.read(buffer, 0, (1+4096+16));
  86.               if (offset==-1) break;
  87.               if (((buffer[0] >> 4) & 15) != 1){ System.out.println("ERROR: Stream version unknown"); System.exit(0);}
  88.               padding = buffer[0] & 15;
  89.               if (padding > 15){ System.out.println("ERROR: Invalid padding specified"); System.exit(0);}
  90.               System.arraycopy(buffer, offset-16, OMAC1signature, 0, 16);
  91.               buffer2 = new byte[offset-(1+16)]; //padding/version byte + 16bytes OMAC1 signature
  92.               System.arraycopy(buffer, 1, buffer2, 0, offset-(1+16));
  93.               //VerifyOMAC1signature(OMAC1signature,buffer2); //NOT IMPLEMENTED!!!
  94.               decryptedXML=decryptAesBlock(buffer2,iv, key,padding);
  95.               out.write(decryptedXML, 0, decryptedXML.length);
  96.  
  97.             }
  98.         }
  99.     }
  100.  
  101.     private static byte[] decryptAesBlock(byte[] block, byte[] iv, SecretKeySpec key, int padding) throws Exception { // Java crypto subsystem throws a bunch of checked exceptions, so, i didn't bother to declare them all... it's a bad practice, i know...
  102.  
  103.         Cipher decipher;
  104.         AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
  105.         byte[] decryptedData = new byte[block.length];
  106.         byte[] result = new byte[block.length-padding];
  107.         decipher = Cipher.getInstance("AES/CBC/NoPadding");
  108.         decipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
  109.         decryptedData=decipher.doFinal(block, 0, block.length);
  110.         System.arraycopy(decryptedData, 0, result, 0, decryptedData.length-padding);
  111.  
  112.         return result;
  113.     }
  114.  
  115.     private static byte[] hexToByteArray(String hexString) {
  116.         String s = hexString.toLowerCase();
  117.         byte[] data = new byte[s.length() / 2];
  118.         for (int i = 0; i < s.length(); i += 2) {
  119.           data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
  120.         }
  121.         return data;
  122.     }
  123.    
  124. }
Advertisement
RAW Paste Data Copied
Advertisement