php解密来自iOS的base64Encoding文本

时间:2012-09-21 15:17:57

标签: php ios encryption base64

我花了很多时间试图解密PHP服务器端的加密字符串("yIH7EYW0e4HuxkHApkG7xQ==")没有成功。该字符串在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];
}

0 个答案:

没有答案