[Help] Converting a Lock into a Key (Java)

Problems compiling? Don't understand the source code? Don't know how to code your feature? Post here.

Moderator: Moderators

Locked
[PT]Devilishly
Posts: 96
Joined: 2003-04-18 05:57
Location: Oporto, Portugal
Contact:

[Help] Converting a Lock into a Key (Java)

Post by [PT]Devilishly » 2004-06-13 08:00

Hi,

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;
    }
}
I suppose this is not right, since I'm trying to use this code and I allways receive reset from server :(
Can anyone help me?

Best regards,
[PT]Devilishly

GargoyleMT
DC++ Contributor
Posts: 3212
Joined: 2003-01-07 21:46
Location: .pa.us

Post by GargoyleMT » 2004-06-13 09:11

There are non-Java examples of Lock to Key here:

http://dcplusplus.sourceforge.net/wiki/ ... /LockToKey

TheParanoidOne
Forum Moderator
Posts: 1420
Joined: 2003-04-22 14:37

Post by TheParanoidOne » 2004-06-13 12:12

The world is coming to an end. Please log off.

DC++ Guide | Words

[PT]Devilishly
Posts: 96
Joined: 2003-04-18 05:57
Location: Oporto, Portugal
Contact:

Post by [PT]Devilishly » 2004-06-13 18:39

Nice an open Source java DC client :)

Thanks for your help :)

Best regards,
[PT]Devilishly

TheParanoidOne
Forum Moderator
Posts: 1420
Joined: 2003-04-22 14:37

Post by TheParanoidOne » 2004-06-14 01:23

[PT]Devilishly wrote:Nice an open Source java DC client :)
Indeed, but note that it hasn't had any updates in several years.
The world is coming to an end. Please log off.

DC++ Guide | Words

[PT]Devilishly
Posts: 96
Joined: 2003-04-18 05:57
Location: Oporto, Portugal
Contact:

Post by [PT]Devilishly » 2004-06-14 03:30

Yes, I notice that... :shock:
Why was it abandoned, does anyone knows?

Anyway, with crwood's help I could finally get it to work :D
Here it is:

Code: Select all

public class Encripter
{
    private String retKey;
    private int len;
    private static boolean DEBUG = false;
 
    public Encripter(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; }
}
Best regards,
[PT]Devilishly

psf8500
Posts: 23
Joined: 2003-03-04 18:51
Contact:

Post by psf8500 » 2004-06-14 09:24

There is a more recent Java DC client here.

I've not tried it though.

ivulfusbar
Posts: 506
Joined: 2003-01-03 07:33

Post by ivulfusbar » 2004-06-14 11:36

Run jython instead, and i can supply you with an algo in python. *grins*... a short comment.. an awful lot of code todo almost nothing.. ;))
Everyone is supposed to download from the hubs, - I don´t know why, but I never do anymore.

GargoyleMT
DC++ Contributor
Posts: 3212
Joined: 2003-01-07 21:46
Location: .pa.us

Post by GargoyleMT » 2004-06-14 18:43

ivulfusbar wrote:an awful lot of code todo almost nothing.. ;))
You mean:
Server: Hello I'm dumb.
Client: Hello, I'm also dumb.

Don't you?

I can't find the origin of that quip. ;)

arnetheduck
The Creator Himself
Posts: 296
Joined: 2003-01-02 17:15

Post by arnetheduck » 2004-06-15 04:19

GargoyleMT wrote:
ivulfusbar wrote:an awful lot of code todo almost nothing.. ;))
You mean:
Server: Hello I'm dumb.
Client: Hello, I'm also dumb.

Don't you?

I can't find the origin of that quip. ;)
I think it was:

Server: Hello, I'm stupid.
Client: Hello, I'm also stupid.

from the old forums =)

[PT]Devilishly
Posts: 96
Joined: 2003-04-18 05:57
Location: Oporto, Portugal
Contact:

Post by [PT]Devilishly » 2004-06-18 15:28

ivulfusbar wrote:Run jython instead, and i can supply you with an algo in python. *grins*... a short comment.. an awful lot of code todo almost nothing.. ;))
Thanks :)
But there's no need... The code is runnig fine :)
Just one more question:
When I was trying to send a message in multihub I've received this Exception:

Code: Select all

java.net.SocketException: Broken pipe
What does this message means and what can I do to solve this problem?

Best regards,
[PT]Devilishly

[PT]Devilishly
Posts: 96
Joined: 2003-04-18 05:57
Location: Oporto, Portugal
Contact:

Post by [PT]Devilishly » 2005-01-09 08:37

Hi,

I’m having some troubles to connect to ptokax hubs, because of Lock into key convertion.
I’ve look at http://wza.digitalbrains.com/DC/doc/Appendix_A.html, and had followed all the steps, but it seams that it isn’t working correctly... :(

I’ve wrote this code:

Code: Select all

/*
 * NewEncyptionHandler.java
 *
 * Created on 9 de Janeiro de 2005, 12:20
 */

package src.app;

/**
 *
 * @author  botelhodaniel
 */
public class NewEncyptionHandler {
    
    public static String calculateValidationKey(String lock) {
        int len = lock.length();
        int[] key = new int[len];
        key = computeKey(key,lock.toCharArray(),len);
        key = nibbleSwap(key,len);
        return dcnEncoding(key,len);
    }
    
    /**
     *  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;
     *  The first key character is calculated from the first lock character and the last 
     *  two lock characters
     */
    private static int[] computeKey(int[] key,char[] lock, int len){
        key[0] = lock[0] ^ lock[len-1] ^ lock[len-2] ^ 0x05;
        for(int i=1; i<len; i++)
            key[i] = lock[i] ^ lock[i-1];
        return key;
    }
    
    /**
     * Next, every character in the key must be nibble-swapped
     */
    private static int[] nibbleSwap(int[] key, int len){
        for(int i = 0; i < len; i++)
            key[i]=((((int)key[i] << 0x4) & 0xf0) | (((int)key[i] >> 0x4) & 0xf));
        return 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.
     */
    private static String dcnEncoding(int[] key, int len){
        String pattern = "/%DCN{0}%/";
        String retKey = "";
        for(int i=0; i<len; i++){
            switch(key[i]){
                case 0 : case 5 : case 36 : case 96 : case 124 :
                case 126 :
                    retKey += java.text.MessageFormat.format(pattern,new Object[]{expandZeros(key[i])});
                    break;
                default:
                    retKey += (char) key[i]; break;
            }
        }
        return retKey;
    }
    private static String expandZeros(int val) {
        String s = ""+val;
        while(s.length()<"000".length())
            s = "0"+s;
        return s;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String lock = "tHn[0U1d1lewXCi:b=D^DlKR_L3jM6d+bSf;^BX7\\9cZ?dt";
        System.out.println(calculateValidationKey(lock));
        System.out.println(DCppEncryptionHandler.breakLock(lock));
    }
    
}
I’ve tested it with the following lock:
"tHn[0U1d1lewXCi:b=D^DlKR_L3jM6d+bSf;^BX7\\9cZ?dt"
The generated key was:
“ ÃbS¶VFUUÕ?!ò±¢5?õ?¡¡?r?�1÷?r•%ô? SÕV�¡ö¶V¥?Vµ �

Generating the key, from the same lock in DC++, I received the following key:
“ ÃbS¶VFUUÕ?!ò±¢5…õâ€â€

joakim_tosteberg
Forum Moderator
Posts: 587
Joined: 2003-05-07 02:38
Location: Sweden, Linkoping

Post by joakim_tosteberg » 2005-01-09 11:25

Are you using any unicode/utf8 that could mess it up?

[PT]Devilishly
Posts: 96
Joined: 2003-04-18 05:57
Location: Oporto, Portugal
Contact:

Post by [PT]Devilishly » 2005-01-09 17:07

joakim_tosteberg wrote:Are you using any unicode/utf8 that could mess it up?
Where, in the lock string? I don't think so, but the code is the above...
Anyway, DC++ understand it, and sends a $Key for it.... :?

Best regards,
[PT]Devilishly

[PT]Devilishly
Posts: 96
Joined: 2003-04-18 05:57
Location: Oporto, Portugal
Contact:

Post by [PT]Devilishly » 2005-01-11 06:37

Hi,

Since I don't understand why this is happening, I'll try to demonstrate manually by the appendix:

First it says:
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:

Code: Select all

for (i = 1; i < len; i++)
	key[i] = lock[i] xor lock[i-1];
The first key character is calculated from the first lock character and the last two lock characters:

Code: Select all

key[0] = lock[0] xor lock[len-1] xor lock[len-2] xor 5
suppose we have this lock: "zba"

Code: Select all

lock[0] = z (0x7A)
lock[1] = b (0x62)
lock[2] = a (0x61)

So len= 3;
for i=0:
key[0] = lock[0] xor lock[2] xor lock[1] xor 5
key[0] = 0x7A xor 0x61 xor 0x62 xor 0x5 = 0x7C

for i=1;
key[1] = lock[1] xor lock[0]
key[1] = 0x62 xor 0x7A = 0x18

for i=2;
key[2] = lock[2] xor lock[1]
key[2] = 0x61 xor 0x62 = 0x3
Then,
Next, every character in the key must be nibble-swapped:

Code: Select all

for (i = 0; i < len; i++)
	key[i] = ((key[i]<<4) & 240) | ((key[i]>>4) & 15)

Code: Select all

for i=0:
key[0] = ((0x7C<<4) & 0xF0) | ((0x7C>>4) & 0xF)
key[0] = 0xC7

for i=1;
key[1] = ((0x18<<4) & 0xF0) | ((0x18>>4) & 0xF)
key[1] = 0x81

for i=2;
key[2] = ((0x3<<4) & 0xF0) | ((0x3>>4) & 0xF)
key[2] = 0x30
So the result key form the lock "zba", should be:
Ç?0
Where Ç = 0xC7 ; ? = 0x81 ; 0 = 0x30
But, what's happening is that when I try to ask the key to DC++ with the lock "zba" it sends me an key, where the ? = 0xFFFD instead of ? = 0x81
Now what is wrong?!

Best regards,
[PT]Devilishly

[PT]Devilishly
Posts: 96
Joined: 2003-04-18 05:57
Location: Oporto, Portugal
Contact:

Post by [PT]Devilishly » 2005-01-11 19:33

Hi,

I've found the error... It has nothing to do with the lockToKey convertion... :oops:

Best regards,
[PT]Devilishly

Guitarm
Forum Moderator
Posts: 385
Joined: 2004-01-18 15:38

Post by Guitarm » 2005-01-12 03:46

Good, and the error is? :wink:
"Nothing really happens fast. Everything happens at such a rate that by the time it happens, it all seems normal."

[PT]Devilishly
Posts: 96
Joined: 2003-04-18 05:57
Location: Oporto, Portugal
Contact:

Post by [PT]Devilishly » 2005-01-12 04:37

Hi,

It was due to my charset encode. Once I've changed to "ISO-8859-1", every thing started working fine :roll:

Best regards,
[PT]Devilishly

Guitarm
Forum Moderator
Posts: 385
Joined: 2004-01-18 15:38

Post by Guitarm » 2005-01-12 05:35

Ok, nice to know, thanks :)
"Nothing really happens fast. Everything happens at such a rate that by the time it happens, it all seems normal."

Locked