dongyue4964 2012-09-08 10:55
浏览 19
已采纳

在PHP中解压Android应用程序中的字符串

I'm facing a problem when it comes to decrypting a string that is sent from an Android application. It works fine as long as non-special characters such as æ, ø, å is written. If I add one of those they are shown as '?'.

Here is how the string is encrypted (Java):

/*Constructor*/
public DataCrypt()
{
   ivspec = new IvParameterSpec(iv.getBytes());

   keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");

   try
   {
      cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
   }
   catch (NoSuchAlgorithmException e)
   {
                // TODO Auto-generated catch block
                e.printStackTrace();
    }
   catch (NoSuchPaddingException e)
   {
                // TODO Auto-generated catch block
                e.printStackTrace();
   }
}

public byte[] encrypt(String text) throws Exception
{
    if(text == null || text.length() == 0)
       throw new Exception("Empty string");

    byte[] encrypted = null;

    try
    {
      cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);

      encrypted = cipher.doFinal(text.getBytes("UTF-8"));
    }
    catch (Exception e)
    {                       
      throw new Exception("[encrypt] " + e.getMessage());
    }      
    return encrypted;
}

And here is how the string is decrypted:

function decrypt($code)
{
  $code = $this->hex2bin($code);
  $iv = $this->iv;

  $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
  mcrypt_generic_init($td, $this->key, $iv);

  $decrypted = mdecrypt_generic($td, $code);

  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);

  return utf8_decode(trim($decrypted));
}

protected function hex2bin($hexdata)
{
  $bindata = '';

  for ($i = 0; $i < strlen($hexdata); $i += 2)
  {
    $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
  }
  return $bindata;
}

function pkcs5_unpad($text)
{
    $pad = ord($text{strlen($text)-1});
    if ($pad > strlen($text))
        return false;

    if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
        return false;

    return substr($text, 0, -1 * $pad);
}


$Username = pkcs5_unpad($crypto->decrypt($Username)); //$crypto is an instance of the cryptography class which holds the methods.
echo $Username.'<br/>'; /*Print ??? for special characters like æ, ø, å*/

Hope someone has an idea on how I can fix the problem so it is possible to write ø æ å as well.

Thanks for any help!

  • 写回答

1条回答 默认 最新

  • doushen1026 2012-09-08 11:16
    关注

    The problem is the use of utf8_decode() which the php docs define as Converts a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1. This implies, that non-iso characters are mutilated. Try to simply remove the utf8_decode()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名