首先,我要感谢大家阅读本网站的帮助。非常感谢你。
我面临的问题是我必须使用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];
}
ColdFusion代码。我是从here获得的:
<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>
答案 0 :(得分:6)
如果这是你使用的实际密钥,它看起来是错误的大小。它适用于AES 256,而不是128.此键在CF中产生相同的结果:
<cfset theKey = toBase64("1234567812345678")>
<强>结果:强>
BHmXSHXWOH6McXsttNTgpL5EQmfPCebjVShkZOeHBC8=