AES到PHP的AESCrypt加密/解密

时间:2017-08-01 17:51:09

标签: php ios objective-c encryption

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

-------------------------------------------------------------------------------------------------------------------------------------
$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;}

OriginalText:iphone

密文:SXNepKfh0IrlDDdkq4EdmQ ==

这里有什么问题,请给出一些解密来自PHP的加密文本的建议。如何准确地获得&#34; iphone&#34;基数为64的字符串编码字符串SXNepKfh0IrlDDdkq4EdmQ ==

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

谢谢!

1 个答案:

答案 0 :(得分:1)

问题:

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

修复,有三个选择:

  1. 使用PKCS#7填充,对于mcrypt,在加密之前添加PKCS#7填充,并在解密后将其删除。 (推荐)
    1. 使用空填充,对于Common Crypto,不指定kCCOptionPKCS7Padding,在加密前添加空填充,在解密后删除。 (不推荐)
      1. 最好不要使用mcrypt,它是abandonware,多年没有更新,不支持标准PKCS#7(néePKCS#5)填充,只能使用甚至无法使用的非标准空填充二进制数据。 mcrypt有许多可以追溯到2003年的杰出bugs。不推荐使用的mcrypt-extension将在PHP 7.2中删除。而是考虑使用defuseRNCryptor,它们提供了完整的解决方案,并且正在维护并且是正确的。