Skip to content

Instantly share code, notes, and snippets.

@sontqq
Created December 24, 2019 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sontqq/9220150facd4035700c0f029869ae727 to your computer and use it in GitHub Desktop.
Save sontqq/9220150facd4035700c0f029869ae727 to your computer and use it in GitHub Desktop.
Direct Connect++ $Lock to $Key conversion class (Java)
class DCConvert {
private String retKey;
private int len;
private static boolean DEBUG = false;
public DCConvert(String s) {
char[] lock = s.toCharArray();
len = lock.length;
char[] key = new char[len];
for (int i = 0; i < s.length(); i++) {
if (i == 0)
key[i] = (char) ((int) lock[0] ^ (int) lock[len - 1] ^ (int) lock[len - 2] ^ 5);
else
key[i] = (char) ((int) lock[i] ^ (int) lock[i - 1]);
if (DEBUG)
System.out.println(lock[i] + " = " +
Integer.toBinaryString((int) lock[i] & 0xffffff) +
"\tkey[" + i + "] = " +
Integer.toBinaryString((int) key[i] & 0xffffff));
}
key = convertLocktoKey(lock, key);
retKey = makeRetKey(key);
}
private char[] convertLocktoKey(char[] lock, char[] key) {
// Next, every character in the key must be nibble-swapped:
// for (i = 0; i < len; i++)
// key[i] = ((key[i]<<4) & 240) | ((key[i]>>4) & 15)
if (DEBUG)
System.out.println("Ultima modificaçao...");
for (int i = 0; i < len; i++) {
key[i] = (char) ((((int) key[i] << 4) & 0xf0) | (((int) key[i] >> 4) & 0xf));
if (DEBUG)
System.out.println("key[" + i + "]: " + key[i] + "");
}
return key;
}
private String makeRetKey(char[] key) {
String charA = "\\x00"; // ASCII 0 -> Hex 00
String charB = "\\x05"; // ASCII 5 -> Hex 00
String charC = "\\x24"; // ASCII 36 -> Hex 00
String charD = "\\x60"; // ASCII 96 -> Hex 60 -> `
String charE = "\\x7C"; // ASCII 124 -> Hex 7C
String charF = "\\x7E"; // ASCII 126 -> Hex 7E -> ~
String retKey = new String(key);
// Finally, the characters with the decimal ASCII values of
// 0, 5, 36, 96, 124, and 126 cannot be sent to the server.
// Each character with this value must be substituted with the string
// /%DCN000%/, /%DCN005%/, /%DCN036%/, /%DCN096%/, /%DCN124%/,
// or /%DCN126%/, respectively.
// The resulting string is the key to be sent to the server.
if (DEBUG)
System.out.println(retKey + "\nSubstituir ACSCII 0 ('" + charA + "')");
retKey = retKey.replaceAll(charA, "/%DCN000%/");
if (DEBUG)
System.out.println(retKey + "\nSubstituir ACSCII 5 ('" + charB + "')");
retKey = retKey.replaceAll(charB, "/%DCN005%/");
if (DEBUG)
System.out.println(retKey + "\nSubstituir ACSCII 36 ('" + charC + "')");
retKey = retKey.replaceAll(charC, "/%DCN036%/");
if (DEBUG)
System.out.println(retKey + "\nSubstituir ACSCII 96 ('" + charD + "')");
retKey = retKey.replaceAll(charD, "/%DCN096%/");
if (DEBUG)
System.out.println(retKey + "\nSubstituir ACSCII 124 ('" + charE + "')");
retKey = retKey.replaceAll(charE, "/%DCN124%/");
if (DEBUG)
System.out.println(retKey + "\nSubstituir ACSCII 126 ('" + charF + "')");
retKey = retKey.replaceAll(charF, "/%DCN126%/");
return retKey;
}
/**
* returns the key
*/
public String getKey() {
return retKey;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment