Guest User

Untitled

a guest
Jun 13th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.50 KB | None | 0 0
  1. package tests;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.nio.ByteBuffer;
  10. import java.nio.ByteOrder;
  11.  
  12. import corvidae.corvus.corax.tools.PrintData;
  13.  
  14. /**
  15.  * @author PsychoJr
  16.  */
  17. public class Research
  18. {
  19.  
  20.     private static int m_encryptionKeys[] = new int[4];
  21.     private static int m_encryptionXORKeys[] = new int[4];
  22.     private static int m_encryptionModulus[] = new int[4];
  23.  
  24.     private static int m_decryptionKeys[] = new int[4];
  25.     private static int m_decryptionXORKeys[] = new int[4];
  26.     private static int m_decryptionModulus[] = new int[4];
  27.  
  28.     private static int[] fileXOR = new int[] { 0x3F08A79B, 0xE25CC287,
  29.             0x93D27AB9, 0x20DEA7BF };
  30.  
  31.     public static void main(String[] args) throws IOException
  32.     {
  33.         loadKeys("Dec1.dat", true);
  34.         loadKeys("Enc2.dat", false);
  35.  
  36.         for (int i : m_encryptionModulus)
  37.             System.out.print(Integer.toHexString(i) + " ");
  38.  
  39.         System.out.println();
  40.         for (int i : m_encryptionKeys)
  41.             System.out.print(Integer.toHexString(i) + " ");
  42.  
  43.         System.out.println();
  44.         for (int i : m_encryptionXORKeys)
  45.             System.out.print(Integer.toHexString(i) + " ");
  46.  
  47.         System.out.println();
  48.         for (int i : m_decryptionModulus)
  49.             System.out.print(Integer.toHexString(i) + " ");
  50.  
  51.         System.out.println();
  52.         for (int i : m_decryptionKeys)
  53.             System.out.print(Integer.toHexString(i) + " ");
  54.  
  55.         System.out.println();
  56.         for (int i : m_decryptionXORKeys)
  57.             System.out.print(Integer.toHexString(i) + " ");
  58.        
  59.         //new Research().encryptBlock(buff, src, blockSize)
  60.     }
  61.  
  62.     private static void loadKeys(String fileName, boolean dec)
  63.             throws IOException
  64.     {
  65.         FileInputStream st = new FileInputStream(fileName);
  66.  
  67.         ByteBuffer buffer = ByteBuffer.allocate(65535);
  68.         int size = st.read(buffer.array());
  69.  
  70.         buffer.limit(size);
  71.         buffer.order(ByteOrder.LITTLE_ENDIAN); // Correct order >.>
  72.  
  73.         short header = buffer.getShort();
  74.         int keySize = buffer.get();
  75.         int[] temp = new int[4];
  76.  
  77.         reset(buffer, temp);
  78.         for (int i = 0; i < 4; ++i)
  79.         {
  80.             if (dec)
  81.                 m_decryptionModulus[i] = fileXOR[i] ^ temp[i];
  82.             else
  83.                 m_encryptionModulus[i] = fileXOR[i] ^ temp[i];
  84.         }
  85.  
  86.         reset(buffer, temp);
  87.         for (int i = 0; i < 4; ++i)
  88.         {
  89.             if (dec)
  90.                 m_decryptionKeys[i] = fileXOR[i] ^ temp[i];
  91.             else
  92.                 m_encryptionKeys[i] = fileXOR[i] ^ temp[i];
  93.         }
  94.  
  95.         reset(buffer, temp);
  96.         for (int i = 0; i < 4; ++i)
  97.         {
  98.             if (dec)
  99.                 m_decryptionXORKeys[i] = fileXOR[i] ^ temp[i];
  100.             else
  101.                 m_encryptionXORKeys[i] = fileXOR[i] ^ temp[i];
  102.         }
  103.  
  104.         System.out.println("Header: " + Integer.toHexString(header)
  105.                 + " Valid: " + (header == 0x1112) + " size: " + keySize
  106.                 + " reader size: " + size);
  107.         System.out.println("Remaining:\n" + PrintData.printData(buffer));
  108.     }
  109.  
  110.     private static void reset(ByteBuffer buff, int[] temp)
  111.     {
  112.         int loc = 0;
  113.  
  114.         while (loc != 4)
  115.         {
  116.             temp[loc] = buff.getInt();
  117.             loc++;
  118.         }
  119.     }
  120.  
  121.     public void encryptBlock(byte[] buff, byte[] src, int blockSize)
  122.     {
  123.         byte encBuff[] = new byte[4];
  124.         int encValue[] = new int[4];
  125.  
  126.         ZeroMemory(buff, 0, 11);
  127.  
  128.         for (int i = 0; i < 4; ++i)
  129.         {
  130.             encBuff[i] = (byte) (((m_encryptionXORKeys[i] ^ (src)[i] ^ encValue[0]) * m_encryptionKeys[i])
  131.                     % m_encryptionModulus[i]);
  132.             encValue[0] = encBuff[i] & 0xFFFF;
  133.         }
  134.  
  135.         for (int i = 0; i < 3; ++i)
  136.         {
  137.             encBuff[i] = (byte) (encBuff[i] ^ m_encryptionXORKeys[i]
  138.                     ^ (encBuff[i + 1] & 0xFFFF));
  139.         }
  140.  
  141.         int bitPos = 0;
  142.  
  143.         for (int i = 0; i < 4; ++i)
  144.         {
  145.             bitPos = addBits(buff, bitPos, (int[]) toObjectFromByteArray(encBuff), 0, 16);
  146.             bitPos = addBits(buff, bitPos, (int[]) toObjectFromByteArray(encBuff), 22, 2);
  147.         }
  148.  
  149.         char checksum = 0xF8;
  150.  
  151.         for (int i = 0; i < 8; ++i)
  152.         {
  153.             checksum ^= src[i];
  154.         }
  155.  
  156.         encValue[1] = checksum;
  157.         encValue[0] = checksum ^ blockSize ^ 0x3D;
  158.  
  159.         addBits(buff, bitPos, encValue, 0, 16);
  160.     }
  161.  
  162.    
  163.     public int addBits(byte[] buff, int addBitPos, int[] src, int srcBitPos, int size)
  164.     {
  165.         int srcBitsCount = size + srcBitPos;
  166.         int tempBuffSize = ((srcBitsCount - 1) >> 3) + (1 - (srcBitPos >> 3));
  167.  
  168.  
  169.         byte[] tempBuff = new byte[tempBuffSize + 1];
  170.         ZeroMemory(tempBuff, 0, tempBuffSize + 1);
  171.         System.arraycopy(src,  src.length + (srcBitPos >> 3), tempBuff, 0, tempBuffSize);
  172.  
  173.  
  174.         if((srcBitsCount % 8) != 0) {
  175.                 tempBuff[tempBuffSize - 1] &= 0xFF << (8 - (srcBitsCount % 8));
  176.         }
  177.  
  178.  
  179.         int shiftLeft = srcBitPos % 8;
  180.         shift(tempBuff, tempBuffSize, -shiftLeft);
  181.  
  182.  
  183.         int shiftRight = addBitPos % 8;
  184.         shift(tempBuff, tempBuffSize + 1, shiftRight);
  185.  
  186.  
  187.         int newTempBuffSize = ((shiftRight <= shiftLeft) ? 0 : 1) + tempBuffSize;
  188.         int[] tempBuffDist = new int[buff.length + (addBitPos >> 3)];
  189.  
  190.  
  191.         for(int i = 0; i < newTempBuffSize; ++i) {
  192.                 tempBuffDist[i] |= tempBuff[i];
  193.         }
  194.  
  195.         return addBitPos + size;
  196. }
  197.    
  198.    
  199.     public void shift(byte[] buff, int size, int shiftVal)
  200.     {
  201.         if (shiftVal != 0)
  202.         {
  203.             if (shiftVal > 0)
  204.             {
  205.                 if ((size - 1) > 0)
  206.                 {
  207.                     for (int i = (size - 1); i > 0; --i)
  208.                     {
  209.                         buff[i] = (byte) ((buff[i - 1] << (8 - shiftVal)) | (buff[i] >> shiftVal));
  210.                     }
  211.                 }
  212.  
  213.                 buff[0] >>= shiftVal;
  214.             }
  215.             else
  216.             {
  217.                 shiftVal = -shiftVal;
  218.  
  219.                 if ((size - 1) > 0)
  220.                 {
  221.                     for (int i = 0; i < (size - 1); ++i)
  222.                     {
  223.                         buff[i] = (byte) ((buff[i + 1] >> (8 - shiftVal)) | (buff[i] << shiftVal));
  224.                     }
  225.                 }
  226.  
  227.                 buff[size - 1] <<= shiftVal;
  228.             }
  229.         }
  230.     }
  231.  
  232.     private void ZeroMemory(byte[] Dest, int arr, int i)
  233.     {
  234.         for (int a = 0; a <= i; a++)
  235.         {
  236.             Dest[a + arr] = 0;
  237.         }
  238.     }
  239.    
  240.    
  241.     private static Object toObjectFromByteArray(byte[] byteArr) {
  242.         if (byteArr == null) {
  243.             return null;
  244.         }
  245.  
  246.         Object resultObj = null;
  247.         ByteArrayInputStream bin = null;
  248.         ObjectInputStream ooin = null;
  249.         try {
  250.             bin = new ByteArrayInputStream(byteArr);
  251.             ooin = new ObjectInputStream(bin);
  252.             resultObj = ooin.readObject();
  253.         }
  254.         catch (Exception ex) {
  255.             throw new RuntimeException(ex);
  256.         }
  257.         finally {
  258.             try {
  259.                 if (ooin != null) {
  260.                     ooin.close();
  261.                 }
  262.                 if (bin != null) {
  263.                     bin.close();
  264.                 }
  265.             }
  266.             catch (IOException ex1) {
  267.                 ex1.printStackTrace();
  268.             }
  269.  
  270.         }
  271.         return resultObj;
  272.     }
  273.  
  274.     private static byte[] toByteArray(Object obj) {
  275.         ByteArrayOutputStream barr = null;
  276.         ObjectOutputStream oout = null;
  277.         byte[] bytearr = null;
  278.         try {
  279.             byte[] b2 = null;
  280.             barr = new ByteArrayOutputStream(10000);
  281.             oout = new ObjectOutputStream(barr);
  282.             oout.writeObject(obj);
  283.             oout.flush();
  284.             oout.close();
  285.             bytearr = barr.toByteArray();
  286.  
  287.         }
  288.         catch (Exception ex) {
  289.             throw new RuntimeException(ex);
  290.         }
  291.         finally {
  292.             try {
  293.                 if (oout != null) {
  294.                     oout.close();
  295.                 }
  296.                 if (barr != null) {
  297.                     barr.close();
  298.                 }
  299.             }
  300.             catch (IOException ex1) {
  301.                 ex1.printStackTrace();
  302.             }
  303.         }
  304.         return bytearr;
  305.     }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment