2 clcurie 11 clcurie_11 于 2013.09.11 11:08 提问

Android 不能解码 PHP base64 代码

我想解码Base64代码,再通过PHP服务器编码。程序能正常运行能使用PHP解码,但是当我通过Android手机解码时,有下面的错误:

Base64DecoderException: encoded value has invalid trailing byte

Base64 Code:

oLAwb6uSn2JXqAFTX+qJXaOawOYF3kDDK2HlCb7ItCeimVCsDE7OYH5OgsixKpIAM6KgkCktnB4HsLQtA5Ig1fQvDrRcct9dQi4m8wPpF7a3sFHSG29j2aItKeouflTtsSZgKWvSjg0gBBGM/7PlvkuK+8W4/GXS0QrqV1jcngWrspYmAdi0GiJbPm8b/zlscOIa1z1df11SuQH5+GiUzqZ4WDFOpoH0WWVW3KmbMQ2yifBmXnhn80qZct6KiN7aL8PHEczhNrRqAKfUuEwmsWOnEOyh7UOU6FcnW3VAo2BWd5dJRGgWb5Py09l0XmrdWdzin7klKtMqXOWQRcvEVT7PKtQxQotRpOa+2IQQirVfybyuMipY9YORuW1hqmc95Tdt1WHdIzVwEtq6NXx9AC5mSklbxrcOpINfS2RPFcK0UUMV2xQKAQ+u8PzTj/KBEmb04ObBbnX6y3uL1KT58lDecA9lIbNYuttlgRMzRdxFOvkk21wou2vtMBtIxk0XFJJGjazqqcxVeSxTvQ68wdNSkRmvteowkSq2Vi09CmOhToRHemFyZgKTxSBoNaFuVuYGVggEFIR9kHVrLxoK2Q==

Server Side

echo toAES("some stringe");

function pkcs5_pad($text, $blocksize) {
    $pad = $blocksize - (strlen($text) % $blocksize);

    return $text . str_repeat(chr($pad), $pad);
} 

function toAES($sStr) {
    error_reporting(E_ERROR | E_PARSE);

    $sKey = '1234567891234567';

    $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
    $sStr = pkcs5_pad($sStr, $size);

    $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);

    mcrypt_generic_init($td, $sKey, $iv);
    $data = mcrypt_generic($td, $sStr);

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

    $data = base64_encode($data);

    return $data;
}

function fromAES($sStr) {
    error_reporting(E_ERROR | E_PARSE);

    $sKey = '1234567891234567';

    $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $sKey, base64_decode($sStr), MCRYPT_MODE_ECB);

    $dec_s     = strlen($decrypted);
    $padding   = ord($decrypted[$dec_s - 1]);
    $decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
    }

My Java Code

 try
    {
        SecretKeySpec mSecretKeySpec = new SecretKeySpec('1234567891234567'.getBytes(), "AES");

        byte[] decrypted = Base64.decode(s.getBytes("UTF-8"), Base64.Deafult);

        Cipher mCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        mCipher.init(Cipher.DECRYPT_MODE, mSecretKeySpec);

        return new String(mCipher.doFinal(decrypted));
    }
    catch (Exception e)
    {
        e.printStackTrace();

        return null; // Always return null with the posted error
    }
Csdn user default icon
上传中...
上传图片
插入图片