Skip to content

Instantly share code, notes, and snippets.

@pjmagee
Created February 26, 2012 22:05
Show Gist options
  • Save pjmagee/1919280 to your computer and use it in GitHub Desktop.
Save pjmagee/1919280 to your computer and use it in GitHub Desktop.
Java Decryption Algorithm SWG
public static byte[] Decrypt(byte[] pData, int len, int nCrcSeed) {
nCrcSeed = Integer.reverseBytes(nCrcSeed);
// System.out.println("Decrypt. Original buffer: ");
// printPacketData(pData);
int offset = 0;
int[] Data;
byte[] byteData;
if (pData[0] == 0) {
len -= 4;
offset = 2;
} else {
len -= 3;
offset = 1;
}
int blockCount = (len) / 4;
int byteCount = (len) % 4;
if (byteCount < 0 || blockCount < 0) {
return pData;
}
Data = new int[blockCount];
byteData = new byte[byteCount];
ByteArrayInputStream bIn = new ByteArrayInputStream(pData);
DataInputStream dIn = new DataInputStream(bIn);
byte[] header = new byte[offset];
int iTemp = 0;
try {
dIn.read(header, 0, offset);
for (int i = 0; i < blockCount; i++) {
Data[i] = dIn.readInt();
iTemp = Data[i]; // iTemp == encrypted data.
Data[i] ^= nCrcSeed; // Decrypt the data.
nCrcSeed = iTemp; // CRC seed now equals the encrypted data.
}
nCrcSeed = Integer.reverseBytes(nCrcSeed); // WTF? This is why I
// prefer Java -- I SEE
// NOWHERE WHERE IT SAYS
// TO DO THIS IN THE
// ORIGINAL C++
// ALGORITHM.
for (int i = 0; i < byteCount; i++) {
byte originalByte = dIn.readByte();
iTemp = (originalByte ^ nCrcSeed);
byteData[i] = (byte) iTemp;
}
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
DataOutputStream dOut = new DataOutputStream(bOut);
dOut.write(header);
for (int i = 0; i < Data.length; i++) {
dOut.writeInt(Data[i]);
}
for (int i = 0; i < byteData.length; i++) {
dOut.writeByte(byteData[i]);
}
for (int i = pData.length - 2; i < pData.length; i++) {
dOut.writeByte(pData[i]);
}
// System.out.println("Finished decrypt. Packet data: " );
byte[] toReturn = bOut.toByteArray();
// printPacketData(toReturn);
return toReturn;
} catch (Exception e) {
System.out.println("Error decrypting packet: " + e.toString());
e.printStackTrace();
return pData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment