开发者

AES Encrypting, in objective-c, using init vector, decrypt on php

开发者 https://www.devze.com 2023-04-01 17:50 出处:网络
Iam working on a single sign on solution where users can login without providing username/password solely by using their device id.

Iam working on a single sign on solution where users can login without providing username/password solely by using their device id.

Iam using CCCrypt to encrypt the device id, along with an i-vector. I use the following script:

http://code.google.com/p/iphone-lib/source/browse/trunk/categories/NSData%2BExtension.m

It works fine, encrypting and decrypting on the device itself, but when I want PHP to decrypt the encrypted device id I only get about half of the data back, rest is gibberish.

Example: (note, for testing ive just copy/pasted the encrypted string and the iv directly into my php script)

/**
 * Gets and encrypts the device id. Returns an array where element 0 is the id, and element 1 is the initialization vector used
 * @return NSSArray
 */
- (NSArray *)getEncryptedDeviceId
{    
    NSMutableArray *data = [[NSMutableArray alloc] init];

    NSString *deviceId = [DeviceUtil UDID];
    NSString *initVector = [[[NSString alloc] getRandomString:16] autorelease];
    NSData* encrypted = [[deviceId dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO] aesEncryptWithKey:SINGLE_SIGNON_KEY initialVector:initVector];

    NSLog(@"%@开发者_StackOverflow => %@", initVector, [encrypted base64EncodedString]);

    [data addObject:[[encrypted base64EncodedString] urlencodedValue]];
    [data addObject:initVector];

    return data;
}

I then try to decrypt the data with this php function

public static function decryptData($data, $iv)
{   
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, self::_MCRYPT_SECRET, base64_decode($data), MCRYPT_MODE_CBC, $iv));
}

What i get back is something like this: 5gV?eAw?Vla.5A-A756-ACE482DD53A2 (about half the ID).

Any clues?


You can do the following... From XCode, define an initialization vector, in hex format, of 16 values, preferably random... For this example I am using the same (copy/paste)...

const char iv[16] = { 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,   0xcd, 0xcd, 0xcd, 0xcd };

Then place a breakpoint at a line in your code, after this iv declaration, and view the console. You will notice that each 0xcd hex value has a corresponding '\315'

Take all 16 \315's or whatever values appear in the console and create a string in php as followed

$iv = "\315\315\315\315\315\315\315\315\315\315\315\315\315\315\315";

Use this $iv string in your mcrypt calls

0

精彩评论

暂无评论...
验证码 换一张
取 消