2
我從其他Web服務調用接收到一段加密的json。以下ruby代碼正確地將響應解碼回json。接收到的數據首先被base64解碼,然後前16個字節被視爲iv,其餘爲數據。首先關鍵是解除限制(缺少更好的表達)。目標c解碼加密數據
encrypted = Base64.decode64(res) #base 64 decode
de_cipher = OpenSSL::Cipher::Cipher.new("AES-128-CBC")
de_cipher.decrypt
de_cipher.key = [key].pack('H*') #de-hex the key
de_cipher.iv = encrypted[0..15] # the first 16 bytes is the IV
descrypted = de_cipher.update(encrypted) << de_cipher.final;
json_string = descrypted[16 .. (descrypted.size - 1)] #taking away the first 16, rest is data
ruby代碼只是爲了理解數據而做的準備。我真正需要的是在iPhone上調用這個Web服務並在目標c中解碼。但到目前爲止沒有運氣,我無法將收到的字符串解密爲正確的json。下面是我有:
//self.responseData is received through NSURLConnection, pretty sure it is piece together correctly. But there is \r\n at the end, which made it not correct length for base64, so I took the last two bytes away.
NSString *str = [[[NSString alloc] initWithData:[self.responseData subdataWithRange:(NSRange){0, self.responseData.length - 2}] encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"tvm get token response = [%@]",str);
//CreateDataWithHexString is something I found on stack overflow, supposed to reverse hex string to binary
NSString * key =[[MyProfile sharedInstance] getOneProperty:TVM_KEY];
//NSData *keyData = [[NSData alloc] initWithBase64EncodedString:key options:0];
NSData *keyData = [self CreateDataWithHexString:key];
//base64 decode the received string
NSData * whole = [[NSData alloc] initWithBase64EncodedString:str options:0];
NSData * iv = [whole subdataWithRange:(NSRange){0, 16}];
NSData * data = [whole subdataWithRange:(NSRange){16, whole.length - 16}];
CCCryptorStatus ccStatus = kCCSuccess;
size_t cryptBytes = 0; // Number of bytes moved to buffer.
NSMutableData *dataOut = [NSMutableData dataWithLength:data.length + kCCBlockSizeAES128];
ccStatus = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
keyData.bytes,
kCCKeySizeAES128,
iv.bytes,
data.bytes,
data.length,
dataOut.mutableBytes,
dataOut.length,
&cryptBytes);
if (ccStatus == kCCSuccess) {
dataOut.length = cryptBytes;
NSString * json = [dataOut base64Encoding];
NSLog(@"json = [%@]", dataOut);
NSLog(@"json = [%@]", json);
}
else {
關鍵最初如下產生的,希望這是有道理的去六角如上:
CFUUIDRef theKeyUUID = CFUUIDCreate(NULL);
CFStringRef keyuuid = CFUUIDCreateString(NULL, theKeyUUID);
CFRelease(theKeyUUID);
//server side expect a uuid without those -'s.
NSString * key = [(__bridge NSString *)keyuuid stringByReplacingOccurrencesOfString:@"-" withString:@""];
CFRelease(keyuuid);
下面是CreateDataWithHexString我在堆棧溢出發現,希望它這是正確的目的:
-(NSData *)CreateDataWithHexString:(NSString *)inputString
{
NSUInteger inLength = [inputString length];
unichar *inCharacters = alloca(sizeof(unichar) * inLength);
[inputString getCharacters:inCharacters range:NSMakeRange(0, inLength)];
UInt8 *outBytes = malloc(sizeof(UInt8) * ((inLength/2) + 1));
NSInteger i, o = 0;
UInt8 outByte = 0;
for (i = 0; i < inLength; i++) {
UInt8 c = inCharacters[i];
SInt8 value = -1;
if (c >= '0' && c <= '9') value = (c - '0');
else if (c >= 'A' && c <= 'F') value = 10 + (c - 'A');
else if (c >= 'a' && c <= 'f') value = 10 + (c - 'a');
if (value >= 0) {
if (i % 2 == 1) {
outBytes[o++] = (outByte << 4) | value;
outByte = 0;
} else {
outByte = value;
}
} else {
if (o != 0) break;
}
}
return [[NSData alloc] initWithBytesNoCopy:outBytes length:o freeWhenDone:YES];
}