将二进制打包c#代码转换为PHP

时间:2017-02-12 16:45:40

标签: c# php

我正在尝试将以下c#代码转换为PHP,但我不知道要使用哪个pack()参数。

var xorkey = BitConverter.ToUInt32(filedata, 0) >> 8;
xorkey *= 0x8083;
for (var i = 8; i < filedata.Length; i += 0x4)
{
    BitConverter.GetBytes(BitConverter.ToUInt32(filedata, i) ^ xorkey).CopyTo(filedata, i);
    xorkey ^= BitConverter.ToUInt32(filedata, i);
}
filedata = filedata.Skip(4).ToArray();

编辑 - 一直在努力,到目前为止我的工作似乎正在发挥作用。

$xorkey = unpack("L", $filedata)[1] >> 8;
$xorkey *= 32899; // 32899 == 0x8083

for ($i = 8; $i < strlen($filedata); $i += 4) {
    $bytes = unpack("L", substr($filedata, $i))[1] ^ $xorkey;

    // left to do: BitConverter.GetBytes($bytes).CopyTo(filedata, i);

    // this doesn't work: $xorkey ^= unpack("L", substr($filedata, $i))[1];
}

1 个答案:

答案 0 :(得分:8)

我不确定你在那里尝试做什么,但是如果你试图加密任何数据,你应该摆脱它(至少弱)xor循环并使用一些强cipher encryption algorithms。除此之外,如果数据的长度不是乘数4,那么for循环可能会导致数组索引错误。

如果您仍想将这段代码转换为PHP,那么请按照以下方式进行操作:

C#代码和结果

org.postgresql.util.PSQLException: ERROR: could not extend file "base/12407/18990.223": No space left on device
  Hint: Check free disk space.
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:168)
    at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:116)
    at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:83)
    at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:83)
    at dbAnalysis.config.NamedParamStatement.executeQuery(NamedParamStatement.java:31)
    at dbAnalysis.dao.DbAccess.profile(DbAccess.java:61)
    at dbAnalysis.Benchmark.perform(Benchmark.java:63)
    at dbAnalysis.controller.ConsoleApplication.main(ConsoleApplication.java:95)

PHP代码和结果

byte[] filedata = new byte[] { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88 };

var xorkey = BitConverter.ToUInt32(filedata, 0) >> 8;

//xorkey *= 0x8083;
/* It's important to convert the result to UInt32,
 * because the multiplication of a 32bit integer with an other big integer,
 * may result in a 64bit integer
 */
xorkey = Convert.ToUInt32(xorkey * 0x8083);

//Console.WriteLine("xorkey == {0}", xorkey); // xorkey == 4473666

for (var i = 8; i < filedata.Length; i += 0x4)
{
    BitConverter.GetBytes(BitConverter.ToUInt32(filedata, i) ^ xorkey).CopyTo(filedata, i);
    xorkey ^= BitConverter.ToUInt32(filedata, i);
}
filedata = filedata.Skip(4).ToArray();

// Result filedata will contain the following data
// 45 46 47 48 8f 20 c4 8 4 4 4 1c 1c 1c 1c 4 4 4 4 c