前提
OpenSSLを使ってURLの暗号化し、別ページで復号化の処理をしたいのですが、うまく表示されません。
実現したいこと
暗号化したIDを復号することで表示させたい。
発生している問題・エラーメッセージ
該当のソースコード
php
1class.cryptor.php 2 3<?php 4 5class Cryptor{ 6 7 private $encrypt_method = "AES-256-CBC"; 8 9 private $secret_key = "XDT-YUGHH-GYGF-YUTY-GHRGFR"; 10 11 private $iv = "DFYTYUITYUIUYUGYIYT"; 12 13 public function __construct() { 14 15 } 16 17 function decryptId($id){ 18 $id = base64_decode($id); 19 $key = hash('sha256', $this->secret_key); 20 $iv = substr(hash('sha256', $this->iv), 0, 16); 21 $id = openssl_decrypt($id, $this->encrypt_method, $key, 0, $iv); 22 return $id; 23 } 24 25 function encryptId($id){ 26 $key = hash('sha256', $this->secret_key); 27 $iv = substr(hash('sha256', $this->iv), 0, 16); 28 $id = openssl_encrypt($this->id, $this->encrypt_method, $key, 0, $iv); 29 $id = base64_encode($id); 30 return $id; 31 } 32 33} 34?>
php
1sourcepage.php 2 3<?php 4require_once("class.cryptor.php"); 5$userid = 1; 6$crypt = new Cryptor(); 7$newid = $crypt->encryptId($userid); 8echo "<a href='/destinationpage.php?id=".$newid."'>Go To Page</a>"; 9?>
php
1destinationpage.php 2 3<?php 4require_once("class.cryptor.php"); 5$crypt = new Cryptor(); 6 7if(isset($_GET["id"])){ 8 $userid = $_GET["id"]; 9 $decryptedid = $crypt->decryptId($userid); 10echo "This is the user id:".$decryptedid; 11} 12?>
試したこと
問題点を教えていただければ助かります。
宜しくお願いいたします。