简体   繁体   中英

How to decrypt post data in PHP

I have some data encrypt in Objective-c, code below:

NSDictionary *jsonDic = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"key1", @"2", @"key2", nil]; 
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSData *data = [jsonWriter dataWithObject:jsonDic];
[jsonWriter release];

NSString *key = @"mykey";   
const int len = [data length]*2 + 2048;
char* buffer = (char*)malloc(len);
memset(buffer, 0, len);
const char *iv = "12345";
size_t dataOut = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, 
                                      kCCAlgorithmDES,
                                      kCCOptionPKCS7Padding,
                                      [key UTF8String], 
                                      kCCKeySizeDES,
                                      iv, 
                                      [data bytes],
                                      [data length],
                                      buffer, 
                                      len,
                                      &dataOut);

NSData* retData = nil;
if(cryptStatus == kCCSuccess) {
    retData = [NSData dataWithBytes:buffer length:dataOut];
}

free(buffer);
return destData;

Then call below code post to php api

NSString *urlString = @"http://domain/api.php";
NSMutableURLRequest *urlPost = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
[urlPost setHTTPMethod:@"POST"];
[urlPost setHTTPBody:data];
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:urlPost delegate:self];

I use below php code to read post and decrypt:

define("KEY","mykey");
define("IV","12345");

$post_body = file_get_contents('php://input');
$dec_str   = mcrypt_decrypt(MCRYPT_DES, KEY, $post_body, MCRYPT_MODE_BCB, IV);
error_log($dec_str);

But I can not see the right result. After one day and one night search and trying, still not find out what wrong. Thanks advance!

You have this call:

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, 
                                      kCCAlgorithmDES,
                                      kCCOptionPKCS7Padding,
                                      [keyString UTF8String], 
                                      kCCKeySizeDES,
                                      iv, 
                                      [data bytes],
                                      [data length],
                                      buffer, 
                                      len,
                                      &dataOut);

What's the keyString variable in there? I suppose you should write [key UTF8String] there ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM