2014-12-08 37 views
2

我一直試圖使用RNCryptor加密iOS中的字符串,並讓應用程序將加密的字符串發佈到將解密PHP中的字符串的服務器。RNCryptor在iOS中加密並在PHP中解密

直到PHP腳本返回空字符串結束時,一切似乎都正常工作(無錯誤消息)。

我認爲問題出現在iOS代碼中,因爲當我試圖解密示例decrypt.php中的字符串時,它工作正常。

的iOS:

NSString *key = @"myPassword"; 
NSString *string = @"Secret String"; 
NSData *plain = [string dataUsingEncoding:NSUTF8StringEncoding]; 
NSData *cipherData = [RNEncryptor encryptData:plain withSettings:kRNCryptorAES256Settings password:key error:&error]; 
NSString *cipherString = [[NSString alloc] initWithData:[cipherData base64EncodedDataWithOptions:0] encoding:NSUTF8StringEncoding]; 

然後我發佈cipherString以下PHP腳本

PHP:

require 'autoload.php'; 
$password = "myPassword"; 
$base64Encrypted = $_POST['data']; 
$cryptor = new \RNCryptor\Decryptor(); 
$plaintext = $cryptor->decrypt($base64Encrypted, $password); 
echo $plaintext; 

所有幫助表示讚賞。謝謝。

編輯:我從this discussion瞭解到,當我從cipherString直接進入PHP的Base64時,沒有POST,它完美運行。有什麼想法嗎?

+0

PHP有非標準填充替換

$cryptor = new \RNCryptor\Decryptor(); 

,它不是PKCS#7。 – zaph 2014-12-08 23:54:41

+1

將最後一條語句分解爲兩個組件,以便您可以瞭解是否爲失敗的加密或Base5編碼。 – zaph 2014-12-08 23:58:09

+0

@Zaph RNCryptor!= mcrypt,所以PKCS#7評論可能不適用。請注意,有多個版本的RNCrypt,請確保版本匹配! – 2014-12-09 15:50:12

回答

0

通過

$cryptor = new \RNCryptor\RNCryptor\Decryptor;