首先,我需要说我已经检查了所有相关的stackoverflow问题。还检查了答案中的链接,但没有任何可用的解决方案。所以请不要以为我在这里直接问你这个问题。
这是我的php脚本,我与这个脚本无关(因为我无法更改脚本)。
function encrypt($message,$secretKey) {
return base64_encode(
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256,
$secretKey,
$message,
MCRYPT_MODE_ECB
)
);
}
我无法在目标c中解密它。我使用了许多类别,例如Strong Encryption for Cocoa / Cocoa Touch等,我也遵循了这个问题How do I do base64 encoding on iphone-sdk?
请帮帮我。我非常迫切需要它。在此先感谢。
以下是我用于解密的目标C代码(在cocoa-aes类别NSData + AES.h中找到)
- (NSData *)AESDecryptWithPassphrase:(NSString *)pass
{
NSMutableData *ret = [NSMutableData dataWithCapacity:[self length]];
unsigned long rk[RKLENGTH(KEYBITS)];
unsigned char key[KEYLENGTH(KEYBITS)];
const char *password = [pass UTF8String];
for (int i = 0; i < sizeof(key); i++)
key[i] = password != 0 ? *password++ : 0;
int nrounds = rijndaelSetupDecrypt(rk, key, KEYBITS);
unsigned char *srcBytes = (unsigned char *)[self bytes];
int index = 0;
while (index < [self length])
{
unsigned char plaintext[16];
unsigned char ciphertext[16];
int j;
for (j = 0; j < sizeof(ciphertext); j++)
{
if (index >= [self length])
break;
ciphertext[j] = srcBytes[index++];
}
rijndaelDecrypt(rk, nrounds, ciphertext, plaintext);
[ret appendBytes:plaintext length:sizeof(plaintext)];
NSString* s = [[NSString alloc] initWithBytes:plaintext length:sizeof(plaintext) encoding:NSASCIIStringEncoding];
NSLog(@"%@",s);
}
return ret;
}
我也试过这个解码器
- (NSData*) aesDecryptWithKey:(NSString *)key initialVector:(NSString*)iv
{
int keyLength = [key length];
if(keyLength != kCCKeySizeAES128)
{
DebugLog(@"key length is not 128/192/256-bits long");
///return nil;
}
char keyBytes[keyLength+1];
bzero(keyBytes, sizeof(keyBytes));
[key getCString:keyBytes maxLength:sizeof(keyBytes) encoding:NSUTF8StringEncoding];
size_t numBytesDecrypted = 0;
size_t decryptedLength = [self length] + kCCBlockSizeAES128;
char* decryptedBytes = malloc(decryptedLength);
CCCryptorStatus result = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128 ,
(iv == nil ? kCCOptionECBMode | kCCOptionPKCS7Padding : kCCOptionPKCS7Padding),
keyBytes,
keyLength,
iv,
[self bytes],
[self length],
decryptedBytes,
decryptedLength,
&numBytesDecrypted);
if(result == kCCSuccess){
NSData* d=[NSData dataWithBytesNoCopy:decryptedBytes length:numBytesDecrypted];
NSLog(@"%@",[NSString stringWithUTF8String:[d bytes]]);
return d;
}
free(decryptedBytes);
return nil;
}
答案 0 :(得分:8)
从它的外观来看,php函数做了两件事。
MCRYPT_RIJNDAEL_256
那就是为什么简单地使用base64不起作用。我将从MCRYPT_RIJNDAEL_256
只是AES 256的名称猜测。
希望有所帮助。
编辑:
您在上面添加的代码看起来不错。您只需要首先对数据进行base64解码。
php脚本执行此操作:
所以你想在你的cocoa app中做到这一点:
如果你遇到麻烦,你可能想玩,看看你是否可以让cocoa做与php脚本相同的事情:加密和base64编码数据。如果您可以将加密函数的输出与php加密函数的输出相同,那么您就可以将其解密了。