gpt4 book ai didi

php - 如何在 Objective C/iOS 中解密 PHP 脚本

转载 作者:可可西里 更新时间:2023-11-01 17:02:20 24 4
gpt4 key购买 nike

我已经检查了所有相关的 Stack Overflow 问题。还检查了该答案中的链接,但没有找到任何可用的解决方案。

这是我的 php 脚本,我与此脚本无关(因为我无法更改脚本)。

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

我无法在 Objective C 中解密它。我使用了许多类别,例如 Strong Encryption for Cocoa / Cocoa Touch等等,我也关注了这个问题How do I do base64 encoding on iOS?

这是我用于解密的 Objective-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;
}

最佳答案

从外观上看,该 php 函数做了两件事。

  1. mcrypt 使用 MCRYPT_RIJNDAEL_256
  2. base64对(1)的输出进行编码

这就是为什么简单地使用 base64 不起作用的原因。我将从名称中猜测 MCRYPT_RIJNDAEL_256 就是 AES 256。

希望对您有所帮助。

编辑:

您在上面添加的代码看起来没问题。您只需先对数据进行 base64 解码。

php 脚本执行此操作:

  1. aes加密
  2. base64编码

所以你想在你的 Cocoa 应用程序中这样做:

  1. base64解码
  2. aes解密

如果遇到问题,您可能想尝试一下,看看是否可以让 cocoa 执行与 php 脚本相同的操作:对数据进行加密和 base64 编码。如果您可以使加密函数的输出与 php 加密函数的输出相同,那么您就可以进行解密了。

关于php - 如何在 Objective C/iOS 中解密 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7618693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com