i'm trying to convert a $Lock into a $Key(in java), based on this Appendix.
I've done the coding this way:
Code: Select all
public class Encriptar
{
private String retKey;
private int len;
private static boolean DEBUG=false;
public Encriptar(String l)
{
char[] lock=l.toCharArray();
len=lock.length;
char[] key = new char[len];
key=convertLocktoKey(lock, key);
retKey=makeRetKey(key);
}
private char[] convertLocktoKey(char[] lock,char[] key) {
/*The first key character is calculated from the first lock character and the last two lock characters:
key[0] = lock[0] xor lock[len-1] xor lock[len-2] xor 5*/
key[0] = (char)(lock[0] ^ lock[len-1] ^ lock[len-2] ^ (char)5);
if(DEBUG) {
System.out.println("key[0] :"+key[0]);
}
/*Except for the first, each key character is computed from the corresponding lock character and the one before it. If the first character has index 0 and the lock has a length of len then:
for (i = 1; i < len; i++)
key[i] = lock[i] xor lock[i-1];*/
for(int i=1;i<len;i++){
key[i]=(char)(lock[i] ^ lock[i-1]);
if(DEBUG) {
System.out.println("key["+i+"]: "+key[i]+" [criado dos caracteres: ("+lock[i]+" XOR "+lock[i-1]+" ) ]");
}
}
/*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)(((key[i]<<4) & 240) | ((key[i]>>4) & 15));
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;
}
public String getKey(){
return retKey;
}
}
Can anyone help me?
Best regards,
[PT]Devilishly