Lee*_*Lee 3 php iphone objective-c ios
首先,我需要说我检查了所有相关的stackoverflow问题.还检查了答案中的链接,但没有任何可用的解决方案.所以请不要以为我在这里直接问你这个问题.
这是我的PHP脚本,我与这个脚本无关(因为我无法更改脚本).
function encrypt($message,$secretKey) {
return base64_encode(
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256,
$secretKey,
$message,
MCRYPT_MODE_ECB
)
);
}
Run Code Online (Sandbox Code Playgroud)
我无法在目标c中解密它.我已经使用了很多类别,如Cocoa/Cocoa Touch等强加密,我也遵循这个问题如何在iphone-sdk上进行base64编码?
请帮我.我非常迫切需要它.提前致谢.
这是我用于解密的目标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;
}
Run Code Online (Sandbox Code Playgroud)
我也试过这个解码器
- (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;
}
Run Code Online (Sandbox Code Playgroud)
从它的外观来看,那个php函数做了两件事.
MCRYPT_RIJNDAEL_256这就是为什么简单地使用base64不起作用.我将从名为MCRYPT_RIJNDAEL_256AES 256 的名字中猜出来.
希望有所帮助.
编辑:
您在上面添加的代码看起来不错.您只需要首先对数据进行base64解码.
php脚本执行此操作:
所以你想在你的cocoa应用程序中这样做:
如果你遇到麻烦,你可能想玩,看看你是否可以让cocoa做与php脚本相同的事情:加密和base64编码数据.如果你可以使你的加密函数的输出与php加密函数的输出相同,那么你就是一个让它解密的好地方.
| 归档时间: |
|
| 查看次数: |
2441 次 |
| 最近记录: |