View Full Version : password encrypt algorithm.
predatorx
08.04.2007, 22:32
Hi ppl..
the password encrypt is a public algorithm??
not is MD5...
somebody knows that algorithm is ...?
this question came to my mind, when i change asp to php ;)
thx
function encrypt($str) {
$key = array();
$dst = array();
$i = 0;
$nBytes = strlen($str);
while ($i < $nBytes){
$i++;
$key[$i] = ord(substr($str, $i - 1, 1));
$dst[$i] = $key[$i];
}
$rslt = $key[1] + $key[2]*256 + $key[3]*65536 + $key[4]*16777216;
$one = $rslt * 213119 + 2529077;
$one = $one - intval($one/ 4294967296) * 4294967296;
$rslt = $key[5] + $key[6]*256 + $key[7]*65536 + $key[8]*16777216;
$two = $rslt * 213247 + 2529089;
$two = $two - intval($two/ 4294967296) * 4294967296;
$rslt = $key[9] + $key[10]*256 + $key[11]*65536 + $key[12]*16777216;
$three = $rslt * 213203 + 2529589;
$three = $three - intval($three/ 4294967296) * 4294967296;
$rslt = $key[13] + $key[14]*256 + $key[15]*65536 + $key[16]*16777216;
$four = $rslt * 213821 + 2529997;
$four = $four - intval($four/ 4294967296) * 4294967296;
$key[4] = intval($one/16777216);
$key[3] = intval(($one - $key[4] * 16777216) / 65535);
$key[2] = intval(($one - $key[4] * 16777216 - $key[3] * 65536) / 256);
$key[1] = intval(($one - $key[4] * 16777216 - $key[3] * 65536 - $key[2] * 256));
$key[8] = intval($two/16777216);
$key[7] = intval(($two - $key[8] * 16777216) / 65535);
$key[6] = intval(($two - $key[8] * 16777216 - $key[7] * 65536) / 256);
$key[5] = intval(($two - $key[8] * 16777216 - $key[7] * 65536 - $key[6] * 256));
$key[12] = intval($three/16777216);
$key[11] = intval(($three - $key[12] * 16777216) / 65535);
$key[10] = intval(($three - $key[12] * 16777216 - $key[11] * 65536) / 256);
$key[9] = intval(($three - $key[12] * 16777216 - $key[11] * 65536 - $key[10] * 256));
$key[16] = intval($four/16777216);
$key[15] = intval(($four - $key[16] * 16777216) / 65535);
$key[14] = intval(($four - $key[16] * 16777216 - $key[15] * 65536) / 256);
$key[13] = intval(($four - $key[16] * 16777216 - $key[15] * 65536 - $key[14] * 256));
$dst[1] = $dst[1] ^ $key[1];
$i=1;
while ($i<16){
$i++;
$dst[$i] = $dst[$i] ^ $dst[$i-1] ^ $key[$i];
}
$i=0;
while ($i<16){
$i++;
if ($dst[$i] == 0) {
$dst[$i] = 102;
}
}
$encrypt = "0x";
$i=0;
while ($i<16){
$i++;
if ($dst[$i] < 16) {
$encrypt = $encrypt . "0" . dechex($dst[$i]);
} else {
$encrypt = $encrypt . dechex($dst[$i]);
}
}
return $encrypt;
}
Actually it's wrong, and as I see noone ever noticed that... :P As some of you know this function was sometimes returning wrong hash (too long, and with wrong data).
Problem is that someone put /65535 instead of /65536 and after that all people were using it without checking.
Those are correct lines:
$key[3] = intval(($one - $key[4] * 16777216) / 65536);
$key[7] = intval(($two - $key[8] * 16777216) / 65536);
$key[11] = intval(($three - $key[12] * 16777216) / 65536);
$key[15] = intval(($four - $key[16] * 16777216) / 65536);
Now it all finally works correctly ;P
Wrong code is for example used in:
https://postpacific.loc-online.com/forums/showthread.php?t=4870&highlight=encrypt
https://postpacific.loc-online.com/forums/showthread.php?t=6148&highlight=encrypt
https://postpacific.loc-online.com/forums/showthread.php?t=5999&highlight=encrypt
L2Tools 1.6 L2Control Panel.
Thanks, but one question.
I changed this string's and still have problem.
Look, I have made hash of password ROOT and ROOD.
And they was same!
0xb1be70e9a83f19192cb593935ec4e2e2
Im think that's abnormal, any idea?
Same hash for simmilar words is an other thing, it's dependant on algorith, which is just not perfect itself, so it's 'normal'. Blame NCsoft programmers for that. At least implementation is correct now :P
Has understood, good job.
thanks.
supermamie
30.07.2007, 14:53
Even with ateh's correction, I can't log on my server with L2CP.
Accounts had been made by L2 account creator.
If anyone have an idea, because I need to make something like
UPDATE user_auth SET password = blabla WHERE password=blibli AND account = bla.
thanks :)
My slightly changed (simplified) version:
function password_encrypt ( $plain ) {
$array_mul = array ( 0 => 213119, 1 => 213247, 2 => 213203, 3 => 213821 );
$array_add = array ( 0 => 2529077, 1 => 2529089, 2 => 2529589, 3 => 2529997 );
$dst = $key = array ( 0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0, 13 => 0, 14 => 0, 15 => 0 );
for ( $i = 0; $i < strlen ( $plain ); $i++ ) {
$dst [ $i ] = $key [ $i ] = ord ( substr ( $plain, $i, 1 ) );
}
for ( $i = 0; $i <= 3; $i++ ) {
$val [ $i ] = fmod ( ( $key [ $i * 4 + 0 ] + $key [ $i * 4 + 1 ] * 0x100 + $key [ $i * 4 + 2 ] * 0x10000 + $key [ $i * 4 + 3 ] * 0x1000000 ) * $array_mul [ $i ] + $array_add [ $i ], 4294967296 );
}
for ( $i = 0; $i <= 3; $i++ ) {
$key [ $i * 4 + 0 ] = $val [ $i ] & 0xff;
$key [ $i * 4 + 1 ] = $val [ $i ] / 0x100 & 0xff;
$key [ $i * 4 + 2 ] = $val [ $i ] / 0x10000 & 0xff;
$key [ $i * 4 + 3 ] = $val [ $i ] / 0x1000000 & 0xff;
}
$dst [ 0 ] = $dst [ 0 ] ^ $key [ 0 ];
for ( $i = 1; $i <= 15; $i++ ) {
$dst [ $i ] = $dst [ $i ] ^ $dst [ $i - 1 ] ^ $key [ $i ];
}
for ( $i = 0; $i <= 15; $i++ ) {
if ( $dst [ $i ] == 0 ) {
$dst [ $i ] = 0x66;
}
}
$encrypted = "0x";
for ( $i = 0; $i <= 15; $i++ ) {
if ( $dst [ $i ] < 16 ) {
$encrypted .= "0";
}
$encrypted .= strtoupper ( dechex ( $dst [ $i ] ) );
}
return ( $encrypted );
}
And then you use:
$query = "update user_auth set password=" . password_encrypt ( $new_password ) . " where password=" . password_encrypt ( $old_password ) . " and account='" . $account . "'";
$res = mssql_query ( $query );
Kenjiro, that's cool.
Big thanks, +1.
I will test.
supermamie
30.07.2007, 20:38
Just tested, and it works great!
thank you a lot!
supermamie
05.08.2007, 13:59
In fact... it works great but only with passwords without numbers :/
The encryption of number is wrong so people can't play if they enter a password with numbers in it.
function auth_encrypt($str)
{
$key = array();
$dst = array();
$i = 0;
$nBytes = strlen($str);
while ($i < $nBytes)
{
$i++;
$key[$i] = ord(substr($str, $i - 1, 1));
$dst[$i] = $key[$i];
}
$rslt = @$key[1] + @$key[2]*256 + @$key[3]*65536 + @$key[4]*16777216;
$one = $rslt * 213119 + 2529077; $one = $one - intval($one/ 4294967296) * 4294967296;
$rslt = @$key[5] + @$key[6]*256 + @$key[7]*65536 + @$key[8]*16777216;
$two = $rslt * 213247 + 2529089; $two = $two - intval($two/ 4294967296) * 4294967296;
$rslt = @$key[9] + @$key[10]*256 + @$key[11]*65536 + @$key[12]*16777216;
$three = $rslt * 213203 + 2529589; $three = $three - intval($three/ 4294967296) * 4294967296;
$rslt = @$key[13] + @$key[14]*256 + @$key[15]*65536 + @$key[16]*16777216;
$four = $rslt * 213821 + 2529997; $four = $four - intval($four/ 4294967296) * 4294967296;
$key[4] = intval($one/16777216);
$key[3] = intval(($one - @$key[4] * 16777216) / 65535);
$key[2] = intval(($one - @$key[4] * 16777216 - @$key[3] * 65536) / 256);
$key[1] = intval(($one - @$key[4] * 16777216 - @$key[3] * 65536 - @$key[2] * 256));
$key[8] = intval($two/16777216);
$key[7] = intval(($two - @$key[8] * 16777216) / 65535);
$key[6] = intval(($two - @$key[8] * 16777216 - @$key[7] * 65536) / 256);
$key[5] = intval(($two - @$key[8] * 16777216 - @$key[7] * 65536 - @$key[6] * 256));
$key[12] = intval($three/16777216);
$key[11] = intval(($three - @$key[12] * 16777216) / 65535);
$key[10] = intval(($three - @$key[12] * 16777216 - @$key[11] * 65536) / 256);
$key[9] = intval(($three - @$key[12] * 16777216 - @$key[11] * 65536 - @$key[10] * 256));
$key[16] = intval($four/16777216);
$key[15] = intval(($four - @$key[16] * 16777216) / 65535);
$key[14] = intval(($four - @$key[16] * 16777216 - @$key[15] * 65536) / 256);
$key[13] = intval(($four - @$key[16] * 16777216 - @$key[15] * 65536 - @$key[14] * 256));
$dst[1] = $dst[1] ^ @$key[1]; $i=1;
while ($i<16)
{
$i++;
$dst[$i] = @$dst[$i] ^ @$dst[$i-1] ^ @$key[$i];
}
$i=0;
while ($i<16)
{
$i++;
if($dst[$i]==0)
{
$dst[$i] = 102;
}
}
$encrypt = "0x";
$i=0;
while ($i<16)
{
$i++;
if($dst[$i] < 16)
{
$encrypt = $encrypt . "0" . dechex($dst[$i]);
}
else
{
$encrypt = $encrypt . dechex($dst[$i]);
}
}
return $encrypt;
}
What is the difference between function on L2CP and this 2 functions ? :happy4:
my function is resist for errors (max chars 16).
supermamie
05.08.2007, 20:47
Thank's a lot :)
It seems this works with numbers too :)
Hmm, mine is tested with a-zA-Z0-9!@#$%^&*()-_=+[]{}\|;':",./<>? and working, and... why shouldn't? It is mathematically the same as l2asp version (but ASP was lowercasing letters which was a bit annoying).
supermamie
07.08.2007, 09:15
I really don't know, but every people on my server with numbers in their password had problem :(
Which version are U using before? Mine and adriahu versions is essentially the same (minor coding styles and array offset).
supermamie
11.08.2007, 15:35
First I used some version found on this forum and the code in L2CP, then your's but with problems with numbers, and now, adriahu's one, without problems.
Phantom2005
11.08.2007, 17:10
I'd personally exchange:
$nBytes = strlen($str);
while ($i < $nBytes)
{
$i++;
$key[$i] = ord(substr($str, $i - 1, 1));
$dst[$i] = $key[$i];
}
for
while ($i < 16) {
$i++;
$key[$i] = '';
$dst[$i] = '';
$key[$i] = ord(substr($str, $i - 1, 1));
$dst[$i] = $key[$i];
}
A password can not be bigger then 16 chars. ( quote l2cp: if(strlen($password) < 4 || strlen($password) > 16) { )
If you don't do this there will be $key's that will remain "undefined".
If you have error_reporting on E_ALL you will have a few warnings.
If you exchange that part with my code those warnings will be gone + will work fine.
Yes, you're right, all variables should be initialized before use, thus in my version is:
$dst = $key = array ( 0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0, 7 => 0, 8 => 0, 9 => 0, 10 => 0, 11 => 0, 12 => 0, 13 => 0, 14 => 0, 15 => 0 );
for ( $i = 0; $i < strlen ( $plain ); $i++ ) {
$dst [ $i ] = $key [ $i ] = ord ( substr ( $plain, $i, 1 ) );
}
Btw, $dst and $key variables is integers, not characters, thus this should be (for sake of correctness):
$key[$i] = $dst[$i] = 0;
Of course it'll work with $dst[$i] = '' because of dynamic typing in PHP, but you should have in mind that future version could have some another type management (maybe explicit type conversions?).