Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tests;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import corvidae.corvus.corax.tools.PrintData;
- /**
- * @author PsychoJr
- */
- public class Research
- {
- private static int m_encryptionKeys[] = new int[4];
- private static int m_encryptionXORKeys[] = new int[4];
- private static int m_encryptionModulus[] = new int[4];
- private static int m_decryptionKeys[] = new int[4];
- private static int m_decryptionXORKeys[] = new int[4];
- private static int m_decryptionModulus[] = new int[4];
- private static int[] fileXOR = new int[] { 0x3F08A79B, 0xE25CC287,
- 0x93D27AB9, 0x20DEA7BF };
- public static void main(String[] args) throws IOException
- {
- loadKeys("Dec1.dat", true);
- loadKeys("Enc2.dat", false);
- for (int i : m_encryptionModulus)
- System.out.print(Integer.toHexString(i) + " ");
- System.out.println();
- for (int i : m_encryptionKeys)
- System.out.print(Integer.toHexString(i) + " ");
- System.out.println();
- for (int i : m_encryptionXORKeys)
- System.out.print(Integer.toHexString(i) + " ");
- System.out.println();
- for (int i : m_decryptionModulus)
- System.out.print(Integer.toHexString(i) + " ");
- System.out.println();
- for (int i : m_decryptionKeys)
- System.out.print(Integer.toHexString(i) + " ");
- System.out.println();
- for (int i : m_decryptionXORKeys)
- System.out.print(Integer.toHexString(i) + " ");
- //new Research().encryptBlock(buff, src, blockSize)
- }
- private static void loadKeys(String fileName, boolean dec)
- throws IOException
- {
- FileInputStream st = new FileInputStream(fileName);
- ByteBuffer buffer = ByteBuffer.allocate(65535);
- int size = st.read(buffer.array());
- buffer.limit(size);
- buffer.order(ByteOrder.LITTLE_ENDIAN); // Correct order >.>
- short header = buffer.getShort();
- int keySize = buffer.get();
- int[] temp = new int[4];
- reset(buffer, temp);
- for (int i = 0; i < 4; ++i)
- {
- if (dec)
- m_decryptionModulus[i] = fileXOR[i] ^ temp[i];
- else
- m_encryptionModulus[i] = fileXOR[i] ^ temp[i];
- }
- reset(buffer, temp);
- for (int i = 0; i < 4; ++i)
- {
- if (dec)
- m_decryptionKeys[i] = fileXOR[i] ^ temp[i];
- else
- m_encryptionKeys[i] = fileXOR[i] ^ temp[i];
- }
- reset(buffer, temp);
- for (int i = 0; i < 4; ++i)
- {
- if (dec)
- m_decryptionXORKeys[i] = fileXOR[i] ^ temp[i];
- else
- m_encryptionXORKeys[i] = fileXOR[i] ^ temp[i];
- }
- System.out.println("Header: " + Integer.toHexString(header)
- + " Valid: " + (header == 0x1112) + " size: " + keySize
- + " reader size: " + size);
- System.out.println("Remaining:\n" + PrintData.printData(buffer));
- }
- private static void reset(ByteBuffer buff, int[] temp)
- {
- int loc = 0;
- while (loc != 4)
- {
- temp[loc] = buff.getInt();
- loc++;
- }
- }
- public void encryptBlock(byte[] buff, byte[] src, int blockSize)
- {
- byte encBuff[] = new byte[4];
- int encValue[] = new int[4];
- ZeroMemory(buff, 0, 11);
- for (int i = 0; i < 4; ++i)
- {
- encBuff[i] = (byte) (((m_encryptionXORKeys[i] ^ (src)[i] ^ encValue[0]) * m_encryptionKeys[i])
- % m_encryptionModulus[i]);
- encValue[0] = encBuff[i] & 0xFFFF;
- }
- for (int i = 0; i < 3; ++i)
- {
- encBuff[i] = (byte) (encBuff[i] ^ m_encryptionXORKeys[i]
- ^ (encBuff[i + 1] & 0xFFFF));
- }
- int bitPos = 0;
- for (int i = 0; i < 4; ++i)
- {
- bitPos = addBits(buff, bitPos, (int[]) toObjectFromByteArray(encBuff), 0, 16);
- bitPos = addBits(buff, bitPos, (int[]) toObjectFromByteArray(encBuff), 22, 2);
- }
- char checksum = 0xF8;
- for (int i = 0; i < 8; ++i)
- {
- checksum ^= src[i];
- }
- encValue[1] = checksum;
- encValue[0] = checksum ^ blockSize ^ 0x3D;
- addBits(buff, bitPos, encValue, 0, 16);
- }
- public int addBits(byte[] buff, int addBitPos, int[] src, int srcBitPos, int size)
- {
- int srcBitsCount = size + srcBitPos;
- int tempBuffSize = ((srcBitsCount - 1) >> 3) + (1 - (srcBitPos >> 3));
- byte[] tempBuff = new byte[tempBuffSize + 1];
- ZeroMemory(tempBuff, 0, tempBuffSize + 1);
- System.arraycopy(src, src.length + (srcBitPos >> 3), tempBuff, 0, tempBuffSize);
- if((srcBitsCount % 8) != 0) {
- tempBuff[tempBuffSize - 1] &= 0xFF << (8 - (srcBitsCount % 8));
- }
- int shiftLeft = srcBitPos % 8;
- shift(tempBuff, tempBuffSize, -shiftLeft);
- int shiftRight = addBitPos % 8;
- shift(tempBuff, tempBuffSize + 1, shiftRight);
- int newTempBuffSize = ((shiftRight <= shiftLeft) ? 0 : 1) + tempBuffSize;
- int[] tempBuffDist = new int[buff.length + (addBitPos >> 3)];
- for(int i = 0; i < newTempBuffSize; ++i) {
- tempBuffDist[i] |= tempBuff[i];
- }
- return addBitPos + size;
- }
- public void shift(byte[] buff, int size, int shiftVal)
- {
- if (shiftVal != 0)
- {
- if (shiftVal > 0)
- {
- if ((size - 1) > 0)
- {
- for (int i = (size - 1); i > 0; --i)
- {
- buff[i] = (byte) ((buff[i - 1] << (8 - shiftVal)) | (buff[i] >> shiftVal));
- }
- }
- buff[0] >>= shiftVal;
- }
- else
- {
- shiftVal = -shiftVal;
- if ((size - 1) > 0)
- {
- for (int i = 0; i < (size - 1); ++i)
- {
- buff[i] = (byte) ((buff[i + 1] >> (8 - shiftVal)) | (buff[i] << shiftVal));
- }
- }
- buff[size - 1] <<= shiftVal;
- }
- }
- }
- private void ZeroMemory(byte[] Dest, int arr, int i)
- {
- for (int a = 0; a <= i; a++)
- {
- Dest[a + arr] = 0;
- }
- }
- private static Object toObjectFromByteArray(byte[] byteArr) {
- if (byteArr == null) {
- return null;
- }
- Object resultObj = null;
- ByteArrayInputStream bin = null;
- ObjectInputStream ooin = null;
- try {
- bin = new ByteArrayInputStream(byteArr);
- ooin = new ObjectInputStream(bin);
- resultObj = ooin.readObject();
- }
- catch (Exception ex) {
- throw new RuntimeException(ex);
- }
- finally {
- try {
- if (ooin != null) {
- ooin.close();
- }
- if (bin != null) {
- bin.close();
- }
- }
- catch (IOException ex1) {
- ex1.printStackTrace();
- }
- }
- return resultObj;
- }
- private static byte[] toByteArray(Object obj) {
- ByteArrayOutputStream barr = null;
- ObjectOutputStream oout = null;
- byte[] bytearr = null;
- try {
- byte[] b2 = null;
- barr = new ByteArrayOutputStream(10000);
- oout = new ObjectOutputStream(barr);
- oout.writeObject(obj);
- oout.flush();
- oout.close();
- bytearr = barr.toByteArray();
- }
- catch (Exception ex) {
- throw new RuntimeException(ex);
- }
- finally {
- try {
- if (oout != null) {
- oout.close();
- }
- if (barr != null) {
- barr.close();
- }
- }
- catch (IOException ex1) {
- ex1.printStackTrace();
- }
- }
- return bytearr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment