PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Pythonsnippsel zu PHP



hoschi111
30.06.2015, 17:07
Hallo Froinde,

ich arbeite gerade an einem Projekt, dass es leider so nur in dieser Form nur als Python-Programm gibt.
Ich möchte daraus gerne eine Webanwendung basteln.
Leider komme ich an einer Stelle bei PHP nicht weiter.

Dies hier:




def encrypted_id(id):



byte1 = bytearray('3go8&$8*3*3h0k(2)2')



byte2 = bytearray(id)



byte1_len = len(byte1)



for i in xrange(len(byte2)):



byte2[i] = byte2[i]^byte1[i%byte1_len]



m = md5.new()



m.update(byte2)



result = m.digest().encode('base64')[:-1]



result = result.replace('/', '_')



result = result.replace('+', '-')



return result




Soll zu PHP geportet werden.

Wenn ID = "2890616069428992" ist,
so muss am Ende "_ZqukolAn64Kv2joMyYZcw==" ausgegeben werden.


Mein Ansatz:


$byte1 = unpack('C*', '3go8&$8*3*3h0k(2)2');
$byte2 = unpack('C*', '2890616069428992');
$byte3 = array();
$byte1_len = count($byte1);
for($i=0; $i < $byte1_len; $i++) {
$byte3[i] = $byte2[i]^$byte1[i%$byte1_len];
}

$mystring = implode(array_map("chr", unpack('C*', pack('H*', md5(serialize($byte2)) ))));
$mystring = str_replace("/","_", $mystring);
$mystring = str_replace("+","-", $mystring);
echo base64_encode($mystring);

Ich habe das Gefühl, dass $byte2 gar nicht beeinflusst wird. Die Pythonfunktion nach VB.Net zu portieren hat allerdings problemlos funktioniert.

VB.Net:



Imports System.Text
Imports System.Security.Cryptography


Public Class Form1
Public Function TextStringToByteArray(ByRef str As String) As Byte()
Dim enc As System.Text.Encoding = System.Text.Encoding.Default
Return enc.GetBytes(str)
End Function


Public Function ByteArrayToString(ByRef Barr() As Byte) As String
Return Convert.ToBase64String(Barr)
End Function


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim byte1 As Byte() = TextStringToByteArray("3go8&$8*3*3h0k(2)2")
Dim byte2 As Byte() = TextStringToByteArray(TextBox2.Text)
Dim byte1_len As Integer = byte1.Length
For i As Integer = 0 To byte2.Length - 1
byte2(i) = byte2(i) Xor byte1(i Mod byte1_len)
Next
Dim md5Hash As MD5 = MD5.Create


Dim result As String = ByteArrayToString(md5Hash.ComputeHash(byte2))
result = result.Replace("/", "_")
result = result.Replace("+", "-")
TextBox1.Text = result
' MsgBox(result)
End Sub
End Class



Hoffe ihr könnt mir da helfen. ;)

---------- Post added at 18:07 ---------- Previous post was at 17:47 ----------

Hab die Lösung gefunden:


function encrypted_id($id)
{
$byte1 = '3go8&$8*3*3h0k(2)2';
for($i=0;$i<strlen($id);$i++)
{
$byte2[$i] = $id[$i];
}
$byte1_len = strlen($byte1);
for($i=0;$i<strlen($id);$i++)
{
$byte2[$i] = $byte2[$i] ^ $byte1[$i % $byte1_len];
}
$id_ = implode('',$byte2);
$id_ = md5($id_,1);
$m = base64_encode($id_);
$m = str_replace(array('/','+'),array('_','-'),$m);
return $m;
}


// kann geclosed werden hier :*

IRET
30.06.2015, 17:10
Webserver können auch Pythoncode ausführen. Stichwort CGI.

hoschi111
30.06.2015, 17:17
Danke dir, hab oben die Lösung reineditiert.
Wollte eine PHP-only Variante :)