Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Translate this code from C# to Java

Hello everybody,

I want to decrypt and read h hashlinks with Java.

There is a C# code available that does just this:
https://github.com/AresChat/cb0t/blob/ma...ashlink.cs

The process begins with the DecodeHashlink() method.

This includes the following methods and classes:
Class DecryptedHashlink (see link above)
Method d67() (see link above)
Method Decompress() (https://github.com/AresChat/cb0t/blob/ma...isc/Zip.cs)
Class HashlinkReader (https://github.com/AresChat/cb0t/blob/ma...kReader.cs)

How can I translate all of this to Java?


Best regards and thank you very much
Well simple really, learn Java and translate it. :D Best of luck
I have been doing a lot with Java lately. But you can not know everything or do everything alone. Sometimes you need help. That's why I created this post to ask for help.

Here's what I've done so far:

Class DecryptedHashlink in C#:
Code
class DecryptedHashlink
   {
       public String Name { get; set; }
       public IPAddress IP { get; set; }
       public ushort Port { get; set; }
   }
Translation to Java:
Code
class DecryptedHashlink
{
private String name;
private InetAddress ip;
private Byte port;

public String getName(){
return name;
}
public void setName(String newName){
this.name = newName;
}

public InetAddress getIP(){
return ip;
}
public void setIP(InetAddress newIP){
this.ip = newIP;
}

public Byte getPort(){
return port;
}
public void setPort(Byte newPort){
this.port = newPort;
}
}

Method DecodeHashlink in C#:
Code
public static DecryptedHashlink DecodeHashlink(String hashlink)
       {
           DecryptedHashlink room = new DecryptedHashlink();

           try
           {
               if (hashlink.ToUpper().StartsWith("CHATROOM:")) // not encrypted
               {
                   hashlink = hashlink.Substring(9);
                   int split = hashlink.IndexOf(":");
                   room.IP = IPAddress.Parse(hashlink.Substring(0, split));
                   hashlink = hashlink.Substring(split + 1);
                   split = hashlink.IndexOf("|");
                   room.Port = ushort.Parse(hashlink.Substring(0, split));
                   room.Name = hashlink.Substring(split + 1);
                   return room;
               }
               else // encrypted
               {
                   byte[] buf = Convert.FromBase64String(hashlink);
                   buf = d67(buf, 28435); //I could translate up to here
                   buf = Zip.Decompress(buf);

                   HashlinkReader reader = new HashlinkReader(buf);
                   reader.SkipBytes(32);
                   room.IP = reader;
                   room.Port = reader;
                   reader.SkipBytes(4);
                   room.Name = reader;

                   return room;
               }
           }
           catch // badly formed hashlink
           {
               return null;
           }
       }
I translate it to Java so:
Code
public static DecryptedHashlink DecodeHashlink(String hashlink){
DecryptedHashlink room = new DecryptedHashlink();

try
{
if (hashlink.toUpperCase().startsWith("CHATROOM:")) // not encrypted
{
hashlink = hashlink.substring(9);
int split = hashlink.indexOf(":");
try {
room.setIP(InetAddress.getByName((hashlink.substring(0, split))));
}
catch (Exception ex) {

}
hashlink = hashlink.substring(split + 1);
split = hashlink.indexOf("|");
room.setPort(convertStringToByte(hashlink.substring(0, split)));
room.setName(hashlink.substring(split + 1));
return room;
}
else // encrypted
{
byte[] buf = DatatypeConverter.parseBase64Binary(hashlink);
buf = d67(buf, 28435); //I could translate up to here
buf = Zip.Decompress(buf);

HashlinkReader reader = new HashlinkReader(buf);
reader.SkipBytes(32);
room.IP = reader;
room.Port = reader;
reader.SkipBytes(4);
room.Name = reader;

return room;
}
}
}

public static byte convertStringToByte(String str)

   {



       // Convert string to byte

       // using parseByte() method

       return Byte.parseByte(str);

   }

Method d67 in C#:
Code
private static byte[] d67(byte[] data, int b)
       {
           byte[] buffer = new byte[data.Length];
           Array.Copy(data, buffer, data.Length);

           for (int i = 0; i < data.Length; i++)
           {
               buffer[i] = (byte)(data[i] ^ b >> 8 & 255);
               b = (b + data[i]) * 23219 + 36126 & 65535;
           }
           return buffer;
       }
Translation to Java:
Code
private static byte[] d67(byte[] data, int b)
{
byte[] buffer = new byte[data.length];
System.arraycopy(data, 0, buffer, 0, data.length);

for (int i = 0; i < data.length; i++)
{
buffer[i] = (byte)(data[i] ^ b >> 8 & 255);
b = (b + data[i]) * 23219 + 36126 & 65535;
}
return buffer;
}

And now I have problems with the method Decompress from Class Zip:
Code
public static byte[] Decompress(byte[] data)
       {
           try
           {
               byte[] r = null;

               using (MemoryStream ms = new MemoryStream(data))
               using (Stream s = new InflaterInputStream(ms))
               {
                   List<byte> list = new List<byte>();
                   int count = 0;
                   byte[] b = new byte[8192];

                   while ((count = s.Read(b, 0, 8192)) > 0)
                       list.AddRange(b.Take(count));

                   r = list.ToArray();
                   list.Clear();
                   list = null;
               }

               return r;
           }
           catch { }

           return new byte[] { };
       }
Specially with this part from code:
Code
using (MemoryStream ms = new MemoryStream(data))
               using (Stream s = new InflaterInputStream(ms))
Having only JDK 7, how can I translate it to Java?
Does a try have to be written within another try?
And what is the equivalent of MemoryStream, Stream and InflaterInputStream?


Regards and thanks
https://www.programcreek.com/java-api-ex...nputStream
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by simmerFeb 12, 2021 12:07 AM 0 103 Feb 12, 2021 12:07 AM
Last Post: simmer
[Python] by Mr.7Feb 16, 2020 03:36 AM 10 280 Feb 16, 2020 03:36 AM
Last Post: lockhead
[.NET] by Cyb3rNuXOct 9, 2017 06:45 AM 4 287 Oct 9, 2017 06:45 AM
Last Post: Ninja