Skip to content

Instantly share code, notes, and snippets.

@httpspace
Created August 10, 2020 06:03
Show Gist options
  • Save httpspace/477a9b0c0fd3e236c418535c34741daf to your computer and use it in GitHub Desktop.
Save httpspace/477a9b0c0fd3e236c418535c34741daf to your computer and use it in GitHub Desktop.
<?php
$key = pack('H*', '5E8B6E1998F421204C6576544FE1A26B44FC775982D8CE2E'); //驗證參數
$iv = 'FOCASAPIFOCASAPI'; //iv=FOCASAPIFOCASAPI
$qrcodedata = 'TWQRP%3A%2F%2F%E6%98%9F%E4%B9%9D%E5%AE%A2%E5%92%96%E5%95%A1%2F158%2F01%2FV1%3FD1%3D12500%26D3%3DAVnVbcN9xxRv%26D10%3D901%26D11%3D00%2C00600611122233344400000001%3B01%2C00600611122233344400000001%3B04%2C00800899887766554400000001%26D12%3D20170630130000%26OprodNumber%3D%E6%8B%BF%E9%90%B5'; //url encode過的qrcode內容
$qrcodedatahex = bin2hex($qrcodedata);
$datalen = strlen($qrcodedata);
$datalenhex = str_pad(dechex($datalen), 4, '0', STR_PAD_LEFT); //qrcode加密資料長度2 bytes,所以 表示要補到4碼
//echo $datalenhex."\n";
$inputdatahex = $datalenhex . $qrcodedatahex;
$inputdata = pack('H*', $inputdatahex);
$encresult = strtoupper(bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $inputdata, MCRYPT_MODE_CBC, $iv))); //加密後的資料
$keyver = '02'; //加密基碼版本02,需依實際資料指定
$enchex = $keyver . $encresult; //加密版本||加密後的資料
$encbase64 = base64_encode(pack('H*', $enchex)); //轉base64
echo $encbase64 . "\n";
$encurlencode = urlencode($encbase64); //進行url encode
echo $encurlencode;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment