1

我花了很多时间尝试"yIH7EYW0e4HuxkHApkG7xQ=="在 PHP 服务器端解密一个加密字符串 (),但没有成功。该字符串在 iOS 平台上进行了加密,并使用 base64 格式编码为文本。

无论如何,有人可以纠正我在 PHP 方面的错误(我在这方面有点弱)吗?我知道这种方法不是很安全。我只需要一个临时解决方案,直到我可以使用 openssl 研究更好的方法。


PHP:

$str_encrypted = "yIH7EYW0e4HuxkHApkG7xQ=="; // base64encoded
$mykey = "my password";
$decrypted = decrypt_data($str_encrypted =, $mykey);

function decrypt_data($str_encrypted =, $mykey)
{
  $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  $decrypted_text = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $encryptionkey, $text, MCRYPT_MODE_ECB, $iv);
  return $decrypted_text;
}

IOS:

- (NSString *)AES128EncryptWithKey:(NSString *)key
{

    NSData *plainData = [self dataUsingEncoding:NSUTF8StringEncoding];
    NSData *encryptedData = [plainData AES128EncryptWithKey:key];

    NSString *encryptedString = [encryptedData base64Encoding];

    return encryptedString;

}

IOS:

- (NSString *)base64Encoding
{
   const unsigned char *bytes = [self bytes];
   NSMutableString *result = [NSMutableString stringWithCapacity:self.length];
   unsigned long ixtext = 0;
   unsigned long lentext = self.length;
   long ctremaining = 0;
   unsigned char inbuf[3], outbuf[4];
   unsigned short i = 0;
   unsigned short charsonline = 0, ctcopy = 0;
   unsigned long ix = 0;

   while( YES )
   {
      ctremaining = lentext - ixtext;
      if( ctremaining <= 0 ) break;

      for( i = 0; i < 3; i++ )
      {
         ix = ixtext + i;
         if( ix < lentext ) inbuf[i] = bytes[ix];
         else inbuf [i] = 0;
      }

      outbuf [0] = (inbuf [0] & 0xFC) >> 2;
      outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
      outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
      outbuf [3] = inbuf [2] & 0x3F;
      ctcopy = 4;

      switch( ctremaining )
      {
         case 1:
            ctcopy = 2;
            break;
         case 2:
            ctcopy = 3;
            break;
      }

      for( i = 0; i < ctcopy; i++ )
         [result appendFormat:@"%c", encodingTable[outbuf[i]]];

      for( i = ctcopy; i < 4; i++ )
         [result appendString:@"="];

      ixtext += 3;
      charsonline += 4;

   }

   return [NSString stringWithString:result];
}
4

0 回答 0