PHPプログラムに関する各種メモ書き

WordpressのプラグインSmash Balloon Social Photo Feedのデータをデコードする

<?php
class SBI_Data_Encryption
{
  private $key;
  private $salt;

  public function __construct()
  {
    $this->key = 'put your unique phrase here';
    $this->salt = 'put your unique phrase here';
  }

  public function decrypt($raw_value)
  {
    $decoded = base64_decode($raw_value);
    $method = 'aes-256-ctr';
    $ivlen = openssl_cipher_iv_length($method);
    $iv = substr($decoded, 0, $ivlen);
    $cipher_raw = substr($decoded, $ivlen);

    $decrypted = openssl_decrypt(
      $cipher_raw,
      $method,
      $this->key,
      0,
      $iv
    );

    // 末尾の暗号化キーを削除
    $decrypted = str_replace($this->key, '', $decrypted);

    // JSONとしてデコード
    $json_decoded = json_decode($decrypted, true);
    if (json_last_error() === JSON_ERROR_NONE) {
      return $json_decoded;
    }

    return $decrypted;
  }
}

$encrypted_data = "<ここに暗号化されたデータ(テーブルwp_sbi_instagram_posts のカラムjson_data の値)を入れる>";

$decryptor = new SBI_Data_Encryption();
$result = $decryptor->decrypt($encrypted_data);

if ($result !== false) {
  echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
No.2573
12/05 22:18

edit