Jump to content

Help with adapting VB.NET code to PHP


frankienrg

Recommended Posts

Hi everyone!

I'm trying to convert this VB.NET function:

Dim fs_read As New FileStream(“C:\MCC05.rcs”, FileMode.Open, FileAccess.Read)
Dim fs_write As New StreamWriter(File.Create(fs_read.Name.Replace(“.rcs”, “.txt”)))

Dim lBytes As Long = fs_read.Length
Dim fileData(lBytes) As Byte
Dim i As Integer
Dim lbytes_27 As Long = lBytes Mod 27 ‘ sono i byte che restano fuori dividendo il file a gruppi di 27 byte

Dim chiave As Array
chiave = Split(“31;32;33;34;35;36;61;62;63;64;65;66;71;71;77;65;72;74;79;75;61;73;64;66;67;68;6A”, “;”)

While Not fs_read.Position = lBytes – lbytes_27
fs_read.Read(fileData, 0, 27)

For i = 0 To 26
fs_write.Write(Chr(fileData(i) Xor “&H” & chiave(i)))

Next

End While
i = 0
fs_read.Read(fileData, 0, lbytes_27)

For i = 0 To (lbytes_27 – 1)
fs_write.Write(Chr(fileData(i) Xor “&H” & chiave(i)))

Next
fs_read.Close()
fs_write.Close()

 

into PHP code. That's what i made:

<?php
$fs_read = fopen("mcc05.rcs", "r+");
$fs_write = fopen("mcc05.txt", "w+");
$lBytes = $fs_read;
$lBytes_28 = $lBytes % 28;

$chiave = array('2A', '68', '6C', '34', '35', '6A', '6E', '31', '32', '64', '66', '67', '46', '46', '44', '52', '38', '73', '78', '63', '33', '33', '64', '65', '72', '66', '76', '2A');

while (ftell($fs_read) != ($lBytes – $lBytes_28)) {

$fileData = fread($fs_read, 28);

for ($i=0; $i<27; $i++ {
	fwrite($fs_write, (chr($fileData($i) xor “&H” & $chiave($i))));
}

}
$i = 0

for ($i=0; $i<(lBytes_28 – 1); $i++ {
fwrite($fs_write, (chr($fileData($i) xor “&H” & $chiave($i))));
}

$fclose($fs_read);
$fclose($fs_write);
?>

Obviously it doesn't work (give me an error at line 9), but I think it's still a BAD CODE, and even if I fixed the erros, it wouldn't work. I don't understand well VB code, so I think I lost something in the conversion! Will rep+ if some of you could translate the original VB.NET code to PHP, thanks!

Link to comment
Share on other sites

Turn error reporting on via php.ini/httpd.conf or place this at the top of your script:

ini_set('display_errors',1);
error_reporting(E_ALL);

 

What is the error you are recieving? It would point to if it cannot relocate the pointer or the stream cannot be iterated.

Link to comment
Share on other sites

well, thanks for the fast reply, but what I'm really looking for, is not the fix for my php adaption (because I already know that it wouldn't work).

 

It is really a bad adaption. The purpose of me posting my semi-conversion is to give who wants to help me out, something to start with the conversion of the VB.NET code snippet i posted

 

EDIT: btw, that's the error:

[06-Apr-2010 10:35:45] PHP Parse error:  syntax error, unexpected T_STRING in /home/horseden/public_html/meeee/mcc.php on line 9

 

Link to comment
Share on other sites

ok, I think i got most out of it...

<?php
$fs_read = fopen("mcc05.rcs", "r+");
$fs_write = fopen("mcc05.txt", "w+");
$lBytes = filesize("mcc05.rcs");
$lBytes_28 = $lBytes % 28;
$lDiff = ($lBytes)-($lBytes_28);

$chiave = array('2A', '68', '6C', '34', '35', '6A', '6E', '31', '32', '64', '66', '67', '46', '46', '44', '52', '38', '73', '78', '63', '33', '33', '64', '65', '72', '66', '76', '2A');

while (ftell($fs_read) != $lDiff) {

$fileData = fread($fs_read, 28);

for ($i=0; $i<28; $i++) {
	fwrite($fs_write, (chr($fileData($i) xor “&H” & $chiave($i))));
}

}
$i = 0;

for ($i=0; $i<(lBytes_28–1); $i++) {
fwrite($fs_write, (chr($fileData($i) xor “&H” & $chiave($i))));
}

$fclose($fs_read);
$fclose($fs_write);
?>

but i'm getting an error on this: fwrite($fs_write, (chr($fileData($i) xor “&H” & $chiave($i))));

 

I don't know how to convert this damn string:

fs_write.Write(Chr(fileData(i) Xor “&H” & chiave(i)))

from VB.NET to PHP. I think it's the only last step I have to do :S

Link to comment
Share on other sites

&HFF = 0xFF same as FF in hexadecimal..

 

&H is only used in VB6/VB.NET

 

replace &H with 0x

 

but thats a dirty fix.. a cleaner fix would be replacing the array with hexadecimal php representation

 

 

$chiave = array('2A', '68', '6C', '34', '35', '6A', '6E', '31', '32', '64', '66', '67', '46', '46', '44', '52', '38', '73', '78', '63', '33', '33', '64', '65', '72', '66', '76', '2A');

 

to

 

$chiave = array('0x2A', '0x68', '0x6C', '0x34', '0x35', '0x6A', '6E', '31', '32', '64', '66', '67', '46', '46', '44', '52', '38', '73', '78', '63', '33', '33', '64', '65', '72', '66', '76', '2A');

 

etc.. I won't do them all but add 0x in front of each

 

then fix

		fwrite($fs_write, (chr($fileData($i) xor “&H” & $chiave($i))));

to

		fwrite($fs_write, (chr($fileData($i) xor $chiave($i))));

 

 

$lBytes = $fs_read;

is also wrong.. $lBytes is the length of filesize..

 

meaning you have to use

 

$lBytes = filesize("mcc05.rcs");

Link to comment
Share on other sites

Fatal error: Call to undefined function X]H() in mcc.php on line 15

 

last version...

<?php
$fs_read = fopen("mcc05.rcs", "r+");
$fs_write = fopen("mcc05.txt", "w+");
$lBytes = filesize("mcc05.rcs");
$lBytes_28 = $lBytes % 28;
$lDiff = ($lBytes)-($lBytes_28);

$chiave = array('2A', '68', '6C', '34', '35', '6A', '6E', '31', '32', '64', '66', '67', '46', '46', '44', '52', '38', '73', '78', '63', '33', '33', '64', '65', '72', '66', '76', '2A');

while (ftell($fs_read) != $lDiff) {

$fileData = fread($fs_read, 28);
    $prefix = "0x";
for ($i=0; $i<28; $i++) {
        fwrite($fs_write, (chr($fileData($i) xor $prefix.$chiave($i))));
}

}

for ($i=0; $i<lBytes_28; $i++) {
fwrite($fs_write, (chr($fileData($i) xor $prefix.$chiave($i))));
}

fclose($fs_read);
fclose($fs_write);
?>

 

Link to comment
Share on other sites

$chiave = array(0x2A, 0x68, 0x6C, 0x34, 0x35, 0x6A, 0x6E, 0x31, 0x32, 0x64, 0x66, 0x67, 0x46, 0x46, 0x44, 0x52, 0x38, 0x73, 0x78, 0x63, 0x33, 0x33, 0x64, 0x65, 0x72, 0x66, 0x76, 0x2A);

 

keep adding 0x in front of each one and removed all single quotes they are causing problems!

 

also replace both fwrites with yes you have to [] in php not ().

 

fwrite($fs_write, (chr($fileData[$i] xor $chiave[$i])));

 

Problem solved.. you can hit resolved, I've tested it. =]

Link to comment
Share on other sites

  • 7 months later...

Sorry to be reviving this topic, but I actually solved the problem, here's the script:

 

$giornata = $_GET['mcc'];

##AREA CONFIGURABILE##
$nome_file = "mcc".$giornata;
$input = $nome_file.".rcs";
$output = $nome_file.".txt";
$ultima_riga = 2; // 0=SI 2=NO
$chiave = array('72', '2A', '67', '66', '64', '34', '56', '42', '48', '34', '34', '46', '46', '35', '52', '38', '73', '78', '2A', '63', '33', '33', '66', '34', '66', '45', '45', '32');
##FINE CONFIGURAZIONE##

if (file_exists($input)) {
$fs_read = fopen($input, "r+");
$fs_write = fopen($output, "w+");
$lBytes = filesize($input);
$lBytes_28 = $lBytes % 28;
$lDiff = ($lBytes)-($lBytes_28);

while (ftell($fs_read) != $lDiff) {
	$fileData = fread($fs_read, 28);
	for ($i=0; $i<28; $i++) {
		$data = hexdec(bin2hex($fileData[$i]));
		$key = hexdec($chiave[$i]);
		$result = $data ^ $key;
		fwrite($fs_write, chr($result));
	}
}

$fileData = fread($fs_read, $lBytes_28);
for ($i=0; $i<($lBytes_28-$ultima_riga); $i++) {
	$data = hexdec(bin2hex($fileData[$i]));
	$key = hexdec($chiave[$i]);
	$result = $data ^ $key;
	fwrite($fs_write, chr($result));
}

fclose($fs_read);
fclose($fs_write);

echo "File: <a href='$output'><b>$output</b></a>";
echo "<br /><b>Stats:</b><br />Lunghezza file: $lBytes<br/>Primo passaggio: $lDiff<br/>Secondo passaggio: $lBytes_28";
}
else {
echo "File: <a href='$input'><b>$input</b></a> non trovato<br />Istruzioni es.: convertitore.php?mcc=7";
}

 

use it through GETs:

 

decrypter.php?mcc=10 for the file named mcc10.rcs, it will get you a mcc10.txt.

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.