PDA

View Full Version : Java Functions to PHP



RefluxLtd
10-17-2011, 09:17 AM
Hello,

My friend wrote a keygen in Java, but I want to make it work in PHP.

If someone could convert this to PHP, that would be AWESOME!

Thanks alot :thumbsup:

EDIT: You don't have to convert this to PHP classes, just making all functions work would be awesome!

Here are the Java classes:

public final class LicenseKey
{
private final String USER;
private final String KEY;

public LicenseKey(String username)
{
this.USER = username;
this.KEY = makeKey();
}

public final String getKey() {
return this.KEY;
}

public final String makeKey() {
String a = Long.toHexString(LicenseUtil.getCrc32(this.USER + 3 + LicenseUtil.getSalt(true) + "000000003"));

String b = "3" + LicenseUtil.getSalt(false) + LicenseUtil.getHash("0000000000003", new StringBuilder().append(LicenseUtil.getSalt(false)).append(a).toString()) + a;

return b.toUpperCase();
}
}
and

public class LicenseUtil
{
private static final Random RAND = new Random();
private static String salt;

private static final void makeSalt()
{
salt = formatString(1, 0, 2, 'X', Integer.valueOf(RAND.nextInt(255) << 0));
}

public static final String getSalt(boolean clean)
{
if ((salt == null) || (clean)) {
makeSalt();
}
return salt;
}

public static final String getHash(String version, String saltedHash) {
StringBuilder buf = new StringBuilder();

for (int i = 0; i < version.length(); i += saltedHash.length()) {
for (int k = 0; (k < saltedHash.length()) && (i + k < version.length()); k++) {
buf.append(Integer.toHexString(Integer.parseInt(version.substring(i + k, i + k + 1), 16) ^ Integer.parseInt(saltedHash.substring(k, k + 1), 16)));
}

}

return buf.toString();
}

public static final String formatString(int idx, int flags, int width, char conversion, Object obj)
{
return String.format("%" + idx + "$" + flags + width + conversion, new Object[] { obj });
}

public static final long getCrc32(String toHash) {
CRC32 crc = new CRC32();
crc.update(toHash.getBytes());
return Long.valueOf(crc.getValue()).longValue();
}
}

-RefluxLtd ;)

tangoforce
10-17-2011, 11:04 AM
If this is a project request, it should be in that forum. Secondly, how much is on offer?

RefluxLtd
10-17-2011, 11:13 AM
If this is a project request, it should be in that forum. Secondly, how much is on offer?

1 It's not a project.
2 It's only 5 minutes of work if you know what to do, so there's no reward...

tangoforce
10-17-2011, 11:45 AM
So why aren't you doing it then? - It's only 5 minutes of work!

What you're asking is for someone to completely re-write it in php for you. That is a small project request.

DJCMBear
10-17-2011, 12:15 PM
Not 100% sure this is fully correct but does this do what you want?



<?php
class LicenseUtil {
private $RAND;
private $salt = '';
private function makeSalt() { $this->salt = rand(); }
public function getSalt($clean) {
if (($this->salt == null) || ($clean)) { $this->makeSalt(); }
return $this->salt;
}
public function getHash($version, $saltedHash) {
$buf = '';
for ($i = 0; $i < strlen($version); $i += strlen($saltedHash)) {
for ($k = 0; ($k < strlen($saltedHash) && (($i + $k) < strlen($version))); $k++) {
$buf .= $this->hexToStr((intVal(substr($version, $i + $k, ($i + $k) + 1), 16) ^ intVal(substr($saltedHash, $k, $k + 1), 16)));
}
}
return $buf;
}
public function hexToStr($hex) {
$string = '';
for ($i = 0; $i < strlen($hex) - 1; $i += 2) { $string .= chr(hexdec($hex[$i].$hex[$i+1])); }
return ($string);
}
}

class LicenseKey {
private $USER = '';
private $KEY = '';
public function LicenseKey($username) { $this->USER = $username; $this->KEY = $this->makeKey(); }
public function getKey() { return ($this->KEY); }
public function makeKey() {
$LicenseUtil = new LicenseUtil;
$a = $LicenseUtil->hexToStr(Crc32("{$this->USER}3{$LicenseUtil->getSalt(true)}000000003"));
$b = "3{$LicenseUtil->getSalt(false)}{$LicenseUtil->getHash("0000000000003", $LicenseUtil->getSalt(false).$a)}{$a}";
return strtoupper($b);
}
}

$License = new LicenseKey('root');
$License->makeKey();
print $License->getKey();
?>

RefluxLtd
10-17-2011, 12:59 PM
Not 100% sure this is fully correct but does this do what you want?

Thanks for trying, but the result should be a 24-length string, starting with "3"...

Also, makeSalt() is incorrect (I suppose):

salt = formatString(1, 0, 2, 'X', Integer.valueOf(RAND.nextInt(255) << 0));
Here's formatString:

public static final String formatString(int idx, int flags, int width, char conversion, Object obj) {
return String.format("%" + idx + "$" + flags + width + conversion, new Object[] { obj });
}

Here's an example result: "39696DAAE6B2A96EDAAE6B2A"
That's what it should be like... :(

DJCMBear
10-17-2011, 01:05 PM
Ahhh i missed that function lol sorry 1 sec

RefluxLtd
10-17-2011, 01:43 PM
Ahhh i missed that function lol sorry 1 sec

Ok :)

DJCMBear
10-18-2011, 10:00 AM
Right I have gotten this far but unable to make it a fixed length due to being at work so I will leave that to you, but you are set to use it as PHP now.



<?php
class LicenseUtil {
private $RAND;
private $salt = '';
private function makeSalt() { $this->salt = trim($this->formatString(1, 0, 2, 'X', rand())); }
public function getSalt($clean) {
if (($this->salt == null) || ($clean)) { $this->makeSalt(); }
return $this->salt;
}
public function getHash($version, $saltedHash) {
$buf = '';
for ($i = 0; $i < strlen($version); $i += strlen($saltedHash)) {
for ($k = 0; ($k < strlen($saltedHash) && (($i + $k) < strlen($version))); $k++) {
$buf .= $this->hexToStr((intVal(substr($version, $i + $k, ($i + $k) + 1), 16)^
intVal(substr($saltedHash, $k, $k + 1), 16)));
}
}
return str_replace(" ", "", $buf);
}
public function hexToStr($hex) {
$string = '';
for ($i = 0; $i < strlen($hex) - 1; $i += 2) { $string .= chr(hexdec($hex[$i].$hex[$i+1])); }
return ($string);
}
public function formatString($idx, $flags, $width, $conversion, $obj) {
return sprintf("%{$idx}{$flags}{$width}{$conversion}", $obj);
}
public function getCrc32($toHash) { return crc32($toHash); }
}

class LicenseKey {
private $USER = '';
private $KEY = '';
public function LicenseKey($username) { $this->USER = $username; $this->KEY = $this->makeKey(); }
public function getKey() { return ($this->KEY); }
public function makeKey() {
$LicenseUtil = new LicenseUtil;
$a = $LicenseUtil->hexToStr($LicenseUtil->getCrc32("{$this->USER}3{$LicenseUtil->getSalt(true)}0000000000003"));
$b = "3{$LicenseUtil->getSalt(false)}{$LicenseUtil->getHash("0000000000003", $LicenseUtil->getSalt(false).$a)}";
return strtoupper($b);
}
}

$License = new LicenseKey('root');
$License->makeKey();
print $License->getKey();
?>

tangoforce
10-18-2011, 11:54 AM
It's only 5 minutes of work if you know what to do

Hmm. How many hours did that take and its still not done?

RefluxLtd, you should of made that a project request as you've taken up hours of someones time.

DJCMBear
10-18-2011, 12:24 PM
It was only a few minuets, I had work, meetings and sleeping to do lol

RefluxLtd
10-21-2011, 03:59 PM
Hmm. How many hours did that take and its still not done?

RefluxLtd, you should of made that a project request as you've taken up hours of someones time.

Read the post above this one.

:mad:

tangoforce
10-21-2011, 09:40 PM
I did.

It's you who are not understanding because you're convinced that because its "only 5 minutes for someone who knows what they're doing" it doesn't count as a project.

You wanted someone to start and finish it. Thats a project.

DJCMBear
10-21-2011, 09:52 PM
I did.

It's you who are not understanding because you're convinced that because its "only 5 minutes for someone who knows what they're doing" it doesn't count as a project.

You wanted someone to start and finish it. Thats a project.

God reading your posts make me laugh :thumbsup: the way you put things is just done in a comical way, I don't know if you mean to but all I say is keep it up lol the forums need spicing up a little in some threads aha but yes I agree even though it isn't really a project type thing, it isn't a 5 min job like you said so I personally think yes it should be a paid project under the project section.

tangoforce
10-22-2011, 10:15 AM
:thumbsup: Glad you enjoy my posts ;)