需要移植 XTEA C++ 的帮助代码为 PHP

发布于 2024-12-12 18:19:17 字数 4932 浏览 3 评论 0 原文

我在将 C++ 代码转换为 PHP 时遇到一些问题。目的是我需要使用 XTEA 密码 将我的应用程序(使用 C++ 构建)与 Web 服务器(PHP)进行通信,以通过 XMLRPC 加密/解密请求的数据包。

我从某个项目 Lode 的编程接口 中获取的原始代码以及我指的代码是这个

一些片段:

inline void xtea_encipher(unsigned int num_rounds, unsigned long* v, unsigned long* k)
{
  unsigned long v0 = v[0], v1 = v[1];
  unsigned long sum = 0, delta = 0x9E3779B9;
  for(unsigned int i = 0; i < num_rounds; i++)
  {
    v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
    sum += delta;
    v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
  }
  v[0] = v0;
  v[1] = v1;
}

inline void xtea_decipher(unsigned int num_rounds, unsigned long* v, unsigned long* k)
{
  unsigned long v0 = v[0], v1 = v[1];
  unsigned long delta = 0x9E3779B9, sum = delta * num_rounds;
  for(unsigned int i = 0; i < num_rounds; i++)
  {
    v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
    sum -= delta;
    v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  }
  v[0] = v0;
  v[1] = v1;
}

template<typename T>
void xtea_encipher(T& out, const T& in, const std::string& password)
{
  out = in;
  size_t extra_zeroes = (8 - (out.size() % 8)) % 8;
  out.resize(out.size() + extra_zeroes, 0); //add values until the size is a multiple of 8

  unsigned long k[4]; //128 bit key
  std::string pw16 = password; //password with 16 chars (=128 bit)
  pw16.resize(16, 0);
  typedef unsigned char uc;

  k[0] = ((uc)pw16[ 0]) | ((uc)pw16[ 1] << 8) | ((uc)pw16[ 2] << 16) | ((uc)pw16[ 3] << 24);
  k[1] = ((uc)pw16[ 4]) | ((uc)pw16[ 5] << 8) | ((uc)pw16[ 6] << 16) | ((uc)pw16[ 7] << 24);
  k[2] = ((uc)pw16[ 8]) | ((uc)pw16[ 9] << 8) | ((uc)pw16[10] << 16) | ((uc)pw16[11] << 24);
  k[3] = ((uc)pw16[12]) | ((uc)pw16[13] << 8) | ((uc)pw16[14] << 16) | ((uc)pw16[15] << 24);

  unsigned long v[2];

  for(size_t i = 0; i + 7 < out.size(); i += 8)
  {
    v[0] = ((uc)out[i + 0]) | ((uc)out[i + 1] << 8) | ((uc)out[i + 2] << 16) | ((uc)out[i + 3] << 24);
    v[1] = ((uc)out[i + 4]) | ((uc)out[i + 5] << 8) | ((uc)out[i + 6] << 16) | ((uc)out[i + 7] << 24);

    xtea_encipher(64, v, k);

    out[i + 0] = (v[0]) & 255; out[i + 1] = (v[0] >> 8) & 255; out[i + 2] = (v[0] >> 16) & 255; out[i + 3] = (v[0] >> 24) & 255;
    out[i + 4] = (v[1]) & 255; out[i + 5] = (v[1] >> 8) & 255; out[i + 6] = (v[1] >> 16) & 255; out[i + 7] = (v[1] >> 24) & 255;
  }

  out.push_back(extra_zeroes); //at the end, specify to the data how many zeroes can be removed after decoding
}

//returns false if everything went ok, returns true and does nothing if the amount of zeros is > 7 which means it wasn't an enciphered file OR the filesize modulo 8 of the input file isn't 1
template<typename T>
bool xtea_decipher(T& out, const T& in, const std::string& password)
{
  if((in.size() % 8) != 1) return true; //error, incorrect size

  size_t extra_zeroes = in[in.size() - 1];

  if(extra_zeroes >= 8) return true; //error, incorrect amount of extra zeroes indicated

  out = in;

  out.resize(out.size() - 1);

  unsigned long k[4]; //128 bit key
  std::string pw16 = password; //password with 16 chars (=128 bit)
  pw16.resize(16, 0);
  typedef unsigned char uc;
  k[0] = ((uc)pw16[ 0]) | ((uc)pw16[ 1] << 8) | ((uc)pw16[ 2] << 16) | ((uc)pw16[ 3] << 24);
  k[1] = ((uc)pw16[ 4]) | ((uc)pw16[ 5] << 8) | ((uc)pw16[ 6] << 16) | ((uc)pw16[ 7] << 24);
  k[2] = ((uc)pw16[ 8]) | ((uc)pw16[ 9] << 8) | ((uc)pw16[10] << 16) | ((uc)pw16[11] << 24);
  k[3] = ((uc)pw16[12]) | ((uc)pw16[13] << 8) | ((uc)pw16[14] << 16) | ((uc)pw16[15] << 24);

  unsigned long v[2];

  for(size_t i = 0; i + 7 < in.size(); i += 8)
  {
    v[0] = ((uc)out[i + 0]) | ((uc)out[i + 1] << 8) | ((uc)out[i + 2] << 16) | ((uc)out[i + 3] << 24);
    v[1] = ((uc)out[i + 4]) | ((uc)out[i + 5] << 8) | ((uc)out[i + 6] << 16) | ((uc)out[i + 7] << 24);

    xtea_decipher(64, v, k);

    out[i + 0] = (v[0]) & 255; out[i + 1] = (v[0] >> 8) & 255; out[i + 2] = (v[0] >> 16) & 255; out[i + 3] = (v[0] >> 24) & 255;
    out[i + 4] = (v[1]) & 255; out[i + 5] = (v[1] >> 8) & 255; out[i + 6] = (v[1] >> 16) & 255; out[i + 7] = (v[1] >> 24) & 255;
  }

  out.resize(out.size() - extra_zeroes);

  return false;
}

也许有人可以帮助将上面的代码转换为 PHP 代码?非常感谢所提供的所有帮助。

提前致谢。

I have some problem convert C++ code to PHP. The purpose is I need to communicate my application (build with C++) with web server (PHP ) using XTEA cipher to encrypt/decrypt the requested packet via XMLRPC.

The original code I taken from some project Lode's Programming Interface and the code that I mean is this.

some snippets:

inline void xtea_encipher(unsigned int num_rounds, unsigned long* v, unsigned long* k)
{
  unsigned long v0 = v[0], v1 = v[1];
  unsigned long sum = 0, delta = 0x9E3779B9;
  for(unsigned int i = 0; i < num_rounds; i++)
  {
    v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
    sum += delta;
    v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
  }
  v[0] = v0;
  v[1] = v1;
}

inline void xtea_decipher(unsigned int num_rounds, unsigned long* v, unsigned long* k)
{
  unsigned long v0 = v[0], v1 = v[1];
  unsigned long delta = 0x9E3779B9, sum = delta * num_rounds;
  for(unsigned int i = 0; i < num_rounds; i++)
  {
    v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
    sum -= delta;
    v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  }
  v[0] = v0;
  v[1] = v1;
}

template<typename T>
void xtea_encipher(T& out, const T& in, const std::string& password)
{
  out = in;
  size_t extra_zeroes = (8 - (out.size() % 8)) % 8;
  out.resize(out.size() + extra_zeroes, 0); //add values until the size is a multiple of 8

  unsigned long k[4]; //128 bit key
  std::string pw16 = password; //password with 16 chars (=128 bit)
  pw16.resize(16, 0);
  typedef unsigned char uc;

  k[0] = ((uc)pw16[ 0]) | ((uc)pw16[ 1] << 8) | ((uc)pw16[ 2] << 16) | ((uc)pw16[ 3] << 24);
  k[1] = ((uc)pw16[ 4]) | ((uc)pw16[ 5] << 8) | ((uc)pw16[ 6] << 16) | ((uc)pw16[ 7] << 24);
  k[2] = ((uc)pw16[ 8]) | ((uc)pw16[ 9] << 8) | ((uc)pw16[10] << 16) | ((uc)pw16[11] << 24);
  k[3] = ((uc)pw16[12]) | ((uc)pw16[13] << 8) | ((uc)pw16[14] << 16) | ((uc)pw16[15] << 24);

  unsigned long v[2];

  for(size_t i = 0; i + 7 < out.size(); i += 8)
  {
    v[0] = ((uc)out[i + 0]) | ((uc)out[i + 1] << 8) | ((uc)out[i + 2] << 16) | ((uc)out[i + 3] << 24);
    v[1] = ((uc)out[i + 4]) | ((uc)out[i + 5] << 8) | ((uc)out[i + 6] << 16) | ((uc)out[i + 7] << 24);

    xtea_encipher(64, v, k);

    out[i + 0] = (v[0]) & 255; out[i + 1] = (v[0] >> 8) & 255; out[i + 2] = (v[0] >> 16) & 255; out[i + 3] = (v[0] >> 24) & 255;
    out[i + 4] = (v[1]) & 255; out[i + 5] = (v[1] >> 8) & 255; out[i + 6] = (v[1] >> 16) & 255; out[i + 7] = (v[1] >> 24) & 255;
  }

  out.push_back(extra_zeroes); //at the end, specify to the data how many zeroes can be removed after decoding
}

//returns false if everything went ok, returns true and does nothing if the amount of zeros is > 7 which means it wasn't an enciphered file OR the filesize modulo 8 of the input file isn't 1
template<typename T>
bool xtea_decipher(T& out, const T& in, const std::string& password)
{
  if((in.size() % 8) != 1) return true; //error, incorrect size

  size_t extra_zeroes = in[in.size() - 1];

  if(extra_zeroes >= 8) return true; //error, incorrect amount of extra zeroes indicated

  out = in;

  out.resize(out.size() - 1);

  unsigned long k[4]; //128 bit key
  std::string pw16 = password; //password with 16 chars (=128 bit)
  pw16.resize(16, 0);
  typedef unsigned char uc;
  k[0] = ((uc)pw16[ 0]) | ((uc)pw16[ 1] << 8) | ((uc)pw16[ 2] << 16) | ((uc)pw16[ 3] << 24);
  k[1] = ((uc)pw16[ 4]) | ((uc)pw16[ 5] << 8) | ((uc)pw16[ 6] << 16) | ((uc)pw16[ 7] << 24);
  k[2] = ((uc)pw16[ 8]) | ((uc)pw16[ 9] << 8) | ((uc)pw16[10] << 16) | ((uc)pw16[11] << 24);
  k[3] = ((uc)pw16[12]) | ((uc)pw16[13] << 8) | ((uc)pw16[14] << 16) | ((uc)pw16[15] << 24);

  unsigned long v[2];

  for(size_t i = 0; i + 7 < in.size(); i += 8)
  {
    v[0] = ((uc)out[i + 0]) | ((uc)out[i + 1] << 8) | ((uc)out[i + 2] << 16) | ((uc)out[i + 3] << 24);
    v[1] = ((uc)out[i + 4]) | ((uc)out[i + 5] << 8) | ((uc)out[i + 6] << 16) | ((uc)out[i + 7] << 24);

    xtea_decipher(64, v, k);

    out[i + 0] = (v[0]) & 255; out[i + 1] = (v[0] >> 8) & 255; out[i + 2] = (v[0] >> 16) & 255; out[i + 3] = (v[0] >> 24) & 255;
    out[i + 4] = (v[1]) & 255; out[i + 5] = (v[1] >> 8) & 255; out[i + 6] = (v[1] >> 16) & 255; out[i + 7] = (v[1] >> 24) & 255;
  }

  out.resize(out.size() - extra_zeroes);

  return false;
}

There maybe someone who could help to convert above code to PHP code? All help given is greatly appreciated.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記柔刀 2024-12-19 18:19:17

Wikipedia 上的 XTEA 页面有一个指向 PHP 实现。 (请参阅底部的下载链接)。这应该是一个好的开始。

The XTEA page on Wikipedia has a links to a PHP implementation. (See the download links at the bottom). That should be a good start.

蓝海 2024-12-19 18:19:17

另一种可能性可能是保留您的 C++ 代码,并学习如何将其粘合(即嵌入)到 PHP 中。我认为 PHP 能够在动态加载的共享库中调用外部代码。

Another possibility might be to keep your C++ code, and learn how to glue it (that is to embed it) into PHP. I think that PHP is able to call external code in dynamically loaded shared libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文