2015-10-14 84 views
1

我有2個PHP頁面,在該頁面1加密字符串onLoad,加密字符串將通過URL傳遞到第2頁。偶爾openssl_decrypt錯誤:「未能爲base64解碼輸入」

偶爾(大約8選1)第2頁無法解密字符串,並給了我此錯誤消息:failed to base64 decode the input ...

這裏是我使用的是什麼:

  • 加密方法: AES-128-CBC
  • 四:openssl_random_pseudo_bytes(16);

這裏有一些廣告ditional信息:

  • PHP 5.4.16
  • 的nginx 1.8.0
  • CentOS的7

有什麼建議嗎?

+0

聽起來像一個URL編碼問題給我。你可以通過提供一個失敗的例子來幫助我們確認。它可能包含一個'+'或'='符號或一些這樣的,而其他工作正常的例子不。如果您提供了用於將字符串放入URL的代碼,那麼它也會有所幫助,因此我們可以看到它是否正確編碼。 – Simba

+0

你是對的,那就是問題所在。我將包括我在下面解決它的方式。 – ioojimooi

回答

3

爲了在一個URL字符串,則需要URL encode它。對於base 64,您需要用%3D替換=,但不要這樣做。如果在URL中包含字符串,請使用proper URL encoding function

+0

感謝指着我,我以前有沒有這樣的想法rawurlencode/rawurldecode功能(...「_」;)〜 – ioojimooi

+0

我相信他還可以使用Base64編碼與[RFC 4648](HTTPS網頁安全字母: //tools.ietf.org/html/rfc4648#page-7)。像Jason Web Keys(JWK)這樣的標準使用字母表。 OpenSSL使用原始的Base64字母表,而不是網頁安全的字母表。所以如果他想繼續使用OpenSSL,他需要一個編碼功能。 – jww

1

感謝來自@Simba & @大衛·施瓦茨評論,這裏就是要做到:

第1頁:

- take the var out from $_GET; 
- decode the var via rawurldecode(); 
- decrypt the decoded var via openssl_decrypt(); 

工作正常:第2頁

- encrypt the string via openssl_encrypt(); 
- encode the encrypted string for sending via url by rawurlencode(); 
- concatenate those "encrypted+encoded" strings and send via url; 

現在〜thx再次〜