首先,我要感谢大家对我阅读本网站的帮助。非常感谢你。
我面临的问题是我必须使用objective-c(Iphone)加密字符串并在Web服务器中使用ColdFusion对其进行解密。我想使用的算法是 AES-128。
目前,我设法在两个站点中分别加密/解密。我在objective-c 中加密的所有内容都可以在objective-c 中解密,但不能在ColdFusion 中解密。基本上,加密的结果是不一样的。
我有尽可能简单和干净的代码,可以在这里发布。
我的objective-c输出是这个:
加密数据:BHmXSHXWOH6McXsttNTgpL5EQmfPCebjVShkZOeHBC8=
我的 ColdFusion 输出是这个:
加密数据:G+tdEOfQTtVCQGxW3N5uzlu0mGabRKNxuIdAXArQE80=
如您所见,它们是不同的:(我认为问题可能出在Objective-C代码中,因为ColdFusion代码非常简单。但是老实说,我现在有点迷茫。非常感谢任何帮助。
这是我正在使用的代码的副本:
Objective-C 代码:
// Starting point of the application.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
NSString *strData = @"This is a plain string.";
NSString *strKey = @"12345678123456781234567812345678";
NSString *strIv = @"1234567812345678";
NSData *data = [NSData dataWithData:[strData dataUsingEncoding:NSUTF8StringEncoding]];
NSData *iv = [NSData dataWithData:[strIv dataUsingEncoding:NSUTF8StringEncoding]];
NSData *key = [NSData dataWithData:[strKey dataUsingEncoding:NSUTF8StringEncoding]];
NSData *encryptedData = [self doCipher:data iv:iv key:key context:kCCEncrypt];
NSLog(@"Encrypted data: %@",[self base64forData:encryptedData]);
}
// Method to encrypt/decrypt data
- (NSData *)doCipher:(NSData *)dataIn
iv:(NSData *)iv
key:(NSData *)symmetricKey
context:(CCOperation)encryptOrDecrypt
{
CCCryptorStatus ccStatus = kCCSuccess;
size_t cryptBytes = 0; // Number of bytes moved to buffer.
NSMutableData *dataOut = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];
ccStatus = CCCrypt( encryptOrDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
symmetricKey.bytes,
kCCKeySizeAES128,
iv.bytes,
dataIn.bytes,
dataIn.length,
dataOut.mutableBytes,
dataOut.length,
&cryptBytes);
if (ccStatus != kCCSuccess) {
NSLog(@"CCCrypt status: %d", ccStatus);
}
dataOut.length = cryptBytes;
return dataOut;
}
// Method to base64 encode data
- (NSString*)base64forData:(NSData*)theData {
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger theIndex = (i / 3) * 4;
output[theIndex + 0] = table[(value >> 18) & 0x3F];
output[theIndex + 1] = table[(value >> 12) & 0x3F];
output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
}
return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}
冷融合代码。我从这里得到它:
<cfcontent type="text/html; charset=utf-8">
<cfset thePlainData = "This is a plain string." />
<cfset theKey = toBase64("12345678123456781234567812345678") />
<cfset theAlgorithm = "AES/CBC/PKCS5Padding" />
<cfset theEncoding = "base64" />
<cfset theIV = "1234567812345678" />
<cfset encryptedString = encrypt(thePlainData, theKey, theAlgorithm, theEncoding, theIV) />
<cfoutput>Encrypted data: #encryptedString#</cfoutput>