【问题标题】:AESCrypt encryption/decryption from PHP to iOS从 PHP 到 iOS 的 AESCrypt 加密/解密
【发布时间】:2018-01-08 17:19:25
【问题描述】:

我需要在我们的服务器端转换加密/解密方法,请检查以下代码

-------------------------------------------------------------------------------------------------------------------------------------
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = "a16byteslongkey!";
$plaintext = "iphone";
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv);


$ciphertext = base64_encode($ciphertext);
echo "ciphertext: ".$ciphertext."<br/>";

$ciphertextinput = base64_decode($ciphertext);
$cipherdetext = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertextinput, MCRYPT_MODE_CBC, $iv);
echo  "decryptText:".$cipherdetext."<br/>";

我尝试使用 AES 256 加密,但结果不同,

加密

- (NSData *)AES256EncryptWithKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

NSUInteger dataLength = [self length];

//See the doc: For block ciphers, the output size will always be less than or 
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);

size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES256,
                                      NULL /* initialization vector (optional) */,
                                      [self bytes], dataLength, /* input */
                                      buffer, bufferSize, /* output */
                                      &numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
    //the returned NSData takes ownership of the buffer and will free it on deallocation
    return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}

free(buffer); //free the buffer;
return nil;}

解密

- (NSData *)AES256DecryptWithKey:(NSString *)key  {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)

// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

char ivPtr = "";

NSUInteger dataLength = [self length];


//See the doc: For block ciphers, the output size will always be less than or 
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);

size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES256,
                                      NULL /* initialization vector (optional) */,
                                      [self bytes], dataLength, /* input */
                                      buffer, bufferSize, /* output */
                                      &numBytesDecrypted);

if (cryptStatus == kCCSuccess) {
    //the returned NSData takes ownership of the buffer and will free it on deallocation
    return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}

free(buffer); //free the buffer;
return nil;}

原文:iphone

密文:SXNepKfh0IrlDDdkq4EdmQ==

这里有什么问题,请给一些从 PHP 解密加密文本的建议。如何从 base 64 编码字符串 SXNepKfh0IrlDDdkq4EdmQ== 中获取准确的“iphone”字

另外,请告诉我如何在 AES 加密中使用 ECB 模式。

谢谢!

【问题讨论】:

  • 作为 iOS 中的 test 解密 without kCCOptionPKCS7Padding,以十六进制记录结果(将其添加到问题中),查看最后字节,如果解密正确,您应该会看到填充。注意:Base64 非常适合计算机,但不适用于人,因为它使用十六进制,它是逐字节表示的。
  • 该问题需要一个minimal reproducible example,并在适当的情况下以十六进制输入和输出测试值。

标签: php ios objective-c encryption


【解决方案1】:

问题:

`mcrypt 使用非标准空数据填充,iOS 代码使用 PKCS#7 填充。

修复,三个选择:

  1. 使用 PKCS#7 填充,对于 mcrypt,在加密之前添加 PKCS#7 填充,并在解密后将其删除。 (推荐)

或

  1. 使用空填充,对于 Common Crypto,不要指定 kCCOptionPKCS7Padding,在加密之前添加空填充并在解密后删除。 (不推荐)

或

  1. 最好不要使用 mcrypt,它是废弃软件,多年未更新,不支持标准 PKCS#7(née PKCS#5)填充,只有甚至不能使用的非标准空填充与二进制数据。 mcrypt 有许多出色的 bugs 可以追溯到 2003 年。不推荐使用的 mcrypt-extension 将在 PHP 7.2 中删除。而是考虑使用defuse 或RNCryptor,它们提供了完整的解决方案并且正在维护中并且是正确的。

【讨论】:

  • 好的,谢谢!将尝试所有三个选项。我相信,RNCryptor 现在是最好的。
  • 使用空填充,对于 Common Crypto 不指定 kCCOptionPKCS7Padding,在加密前添加空填充,解密后删除。 (不推荐) - 所以我们需要在 CCCrypt 中传递 nil 而不是 kCCOptionPKCS7Padding 选项?它使应用程序崩溃,似乎输入密钥长度错误。 *** 由于未捕获的异常“InputAlignmentException”而终止应用程序,原因:“输入大小未正确对齐。”
  • 密钥必须是 AES 大小,128、192 或 256 位,对于 Common Crypto,必须指定该大小。在您的代码中,您有 kCCKeySizeAES256 是的,密钥是“a16byteslongkey!”,它是 16 字节/128 位,两者不匹配。 添加测试值:以十六进制输入和输出,其中二进制而不是 Base64,您必须尽自己的一份力量。
  • 好的 :) 谢谢@zaph
猜你喜欢
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多