18910140161

有人会把下面的 java 获取签名字符串 代码用 php 在重写一下吗?

顺晟科技

2022-09-15 20:59:44

49

public class SignatureUtil{

  /**
  * 获取签名字符串
  *
  * @param uuid       客户唯一标识
  * @param appKey     应用key
  * @param appSecret  应用密钥
  * @param timeMillis 时间戳
  * @param movedCard  移动取模基数
  * @return
  * @throws Exception
  */
  public static String getEncryptStr(String uuid, String appKey, String appSecret, String timeMillis, int movedCard) throws Exception {
      String encryptStr = uuid + appKey + appSecret + timeMillis;
      byte[] encryptByte = encryptStr.getBytes("iso-8859-1");
      byte[] changeByte = change(encryptStr, movedCard);
      byte[] mergeByte = mergeByte(encryptByte, changeByte);
      return DigestUtils.md5Hex(mergeByte);
  }

  /**
  * 简单移位
  */
  private static byte[] change(String encryptStr, int moveCard) throws UnsupportedEncodingException {
      byte[] encryptByte = encryptStr.getBytes("iso-8859-1");
      int encryptLength = encryptByte.length;
      byte temp;
      for (int i = 0; i < encryptLength; i++) {
          temp = ((i % moveCard) > ((encryptLength - i) % moveCard)) ? encryptByte[i] : encryptByte[encryptLength - (i + 1)];
          encryptByte[i] = encryptByte[encryptLength - (i + 1)];
          encryptByte[encryptLength - (i + 1)] = temp;
      }
      return encryptByte;
  }

  /**
  * 合并
  *
  * @param encryptByte
  * @param changeByte
  * @return
  */
  private static byte[] mergeByte(byte[] encryptByte, byte[] changeByte) {
      int encryptLength = encryptByte.length;
      int encryptLength2 = encryptLength * 2;
      byte[] temp = new byte[encryptLength2];
      for (int i = 0; i < encryptByte.length; i++) {
          temp[i] = encryptByte[i];
          temp[encryptLength2 - 1 - i] = changeByte[i];
      }
      return temp;
  }

}

大概逻辑如下:

public function getEncryptStr(string $uuid , string $app_key, string $app_secret, string $time_millis, int $move_card)
    {
        $encrypt_str = $uuid . $app_key . $app_secret . $time_millis;

        $encrypt_byte = utf8_decode($encrypt_str);  //把 UTF-8 字符串解码为 ISO-8859-1。

        $change_str = $this->change($encrypt_str, $move_card);

        $merge_str = $this->merge_byte($encrypt_byte, $change_str);

        return md5($merge_str);
    }

    public function change(string $encrypt_str, int $move_card)
    {
        $encrypt_str = utf8_decode($encrypt_str);

        $len = strlen($encrypt_str);

        for ($i = 0; $i < $len; $i++){
            $temp = ( ($i % $move_card) > (($len - $i) % $move_card) ) ? $encrypt_str[$i] : $encrypt_str[$len - ($i + 1)];
            $encrypt_str[$i] = $encrypt_str[$len - ( $i + 1)];
            $encrypt_str[$len - ($i+1)] = $temp;
        }

        return $encrypt_str;
    }

    public function merge_byte($encrypt_byte, $change_str)
    {
        $len = strlen($encrypt_byte);
        $len2 = $len * 2;

        $temp = '';
        for ($i = 0; $i < $len; $i++){
            $temp[$i] = $encrypt_byte[$i];
            $temp[$len2 - 1 -$i] = $change_str[$i];
        }

        return $temp;
    }
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航