我从其他网络服务电话中收到一段加密的json。以下ruby代码正确地将响应解码回json。接收的数据首先被base64解码,然后前16个字节被视为iv,其余的被视为数据。关键是首先去除(缺乏更好的表达)。
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];
}
答案 0 :(得分:1)
最终解密部分没问题,但是显示错了。这完全是因为这一行:
NSString * json = [dataOut base64Encoding];
我没有查看解密数据,而是查看其Base64
编码字符串,显然它看起来不像JSON
。