iOS AES encryption and PHP decryption
I am trying to encrypt a string in iOS and decrypt it with PHP.
In iOS I have the following code, based on the information in this link AES-128 encryption in iOS and Decryption in PHP
- (void)testCrypto {
NSString *password = @"1234567891234567";
NSString *plaintext = @"This is a test";
NSString *encrypted = [plaintext AES128EncryptWithKey:password];
dispatch_queue_t jsonParsingQueue = dispatch_queue_create("jsonParsingQueue", NULL);
dispatch_async(jsonParsingQueue, ^{
NSString *webResult = [EPWebServices cryptoData:[encrypted dataUsingEncoding:NSUTF8StringEncoding] password:password];
[self performSelectorOnMainThread:@selector(testing:) withObject:webResult waitUntilDone:YES];
});
}
- (void)testing:(id)test {
test = [[NSString alloc] initWithData:test encoding:NSUTF8StringEncoding];
NSLog(@"Result:%@", test);
}
The category on NSData used above to encrypt is in the link. EPWebServices
is a class that converts the cipher text to base64 and sends a URLRequest to the server with the password and cipher text.
In PHP I have (also from the link above):
$Pass = $_GET['pw'];
$crypted = urldecode($_GET['encrypted']);
$newClear = decrypt_password($crypted, $Pass);
echo "Dec: ".$newClear . "Input: ".$crypted;
function decrypt_password($pass,$key)
{
$base64encoded_ciphertext = $pass;
$res_non = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($base64encoded_ciphertext), ‘ecb’);
$decrypted = $res_non;
$dec_s2 = strlen($decrypted);
$padding = ord($decrypted[$dec_s2-1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
The problem is that the decrypted message is just an empty string. The result I get in NSLog is just Dec:Input: ejhCNHo0Z2tNZ0paMGdJOTJLdEJjZz09
. I have confirmed that both the password and cipher text is received properly in PHP, however, the decryption only prints out an empty string. I am new to encryption, so there might be some basic errors. Please let me know if particular output/NSLog etc is needed.
上一篇: o链接器错误(加密应用程序)
下一篇: iOS AES加密和PHP解密