如何在Objective C/iOS中解密PHP脚本 [英] How to decrypt a PHP script in Objective C / iOS

查看:34
本文介绍了如何在Objective C/iOS中解密PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了所有相关的堆栈溢出问题.还检查了答案中的链接,但没有任何可用的解决方案.

I've checked all the related Stack Overflow questions. Also checked the links in that answers but didn't got any usable solution.

这是我的PHP脚本,与该脚本无关(因为我无法更改脚本).

Here is my php script and I've nothing to do with this script (as I can't change the script).

function encrypt($message,$secretKey) {
return base64_encode(
    mcrypt_encrypt(
        MCRYPT_RIJNDAEL_256,
        $secretKey,
        $message,
        MCRYPT_MODE_ECB
    )
 );
}

我无法在目标C中对其进行解密.我使用了许多类别,例如如何在iOS上进行base64编码?

I'm unable to decrypt it in Objective C. I've used a number of Categories like Strong Encryption for Cocoa / Cocoa Touch etc, also I followed this question How do I do base64 encoding on iOS?

这是我用于解密的目标C代码(可可豆类别NSData + AES.h中找到)

Here is the objective C codes that I used for decryption (found in cocoa-aes Category 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;
}

推荐答案

从它的外观来看,该php函数可以完成两件事.

From the looks of it, that php function does two things.

  1. 使用MCRYPT_RIJNDAEL_256
  2. 进行mcrypt
  3. base64对(1)的输出进行编码
  1. mcrypt using MCRYPT_RIJNDAEL_256
  2. base64 encodes the output of (1)

这就是为什么仅使用base64无法起作用的原因.我会从名称中猜测MCRYPT_RIJNDAEL_256只是AES 256.

That would by why simply using base64 doesn't work. I'm going to guess from the name that MCRYPT_RIJNDAEL_256 is just AES 256.

希望有帮助.

您在上面添加的代码看起来不错.您只需要首先对数据进行base64解码即可.

The code you added above looks ok. You just have to base64 decode the data first.

php脚本执行此操作:

The php script does this:

  1. aes加密
  2. base64编码

所以您想在可可应用程序中做到这一点:

So you want to do this in your cocoa app:

  1. base64解码
  2. aes解密

如果遇到麻烦,您可能需要玩转,看看是否可以使可可粉做与php脚本相同的事情:对数据进行加密和base64编码.如果您可以使加密函数的输出与php加密函数的输出相同,那么您就可以解密它了.

If you're having trouble, you might want to play around and see if you can get cocoa to do the same thing as the php script: encrypt and base64 encode the data. If you can get the output of your encryption function to be the same as the output of the php encryption function, you're in a good place to get it decrypting.

这篇关于如何在Objective C/iOS中解密PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆