解码期间的javax.crypto.IllegalBlockSizeException

时间:2018-04-25 21:43:45

标签: java encryption cryptography keystore android-keystore

尝试在Android KeyStore中存储密码后,我得到<?PHP function getUserIP() { $client = @$_SERVER['HTTP_CLIENT_IP']; $forward = @$_SERVER['HTTP_X_FORWARDED_FOR']; $remote = $_SERVER['REMOTE_ADDR']; if(filter_var($client, FILTER_VALIDATE_IP)) { $ip = $client; } elseif(filter_var($forward, FILTER_VALIDATE_IP)) { $ip = $forward; } else { $ip = $remote; } return $ip; } $user_ip = getUserIP(); echo $user_ip; // Output IP address [Ex: 192.168.1.150] ?> 。这是我的代码:

javax.crypto.IllegalBlockSizeException

使用过的算法:

    public static String decode(String encodedString, Cipher cipher) {
    try {
        byte[] bytes = Base64.decode(encodedString, Base64.NO_WRAP); // bytes.length= 44, as I know it should be multiple of 16, am I right ? How to make it right ?
        return new String(cipher.doFinal(bytes)); // getting the error here
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }
    return null;
}

我收到以下错误:

    private static final String TRANSFORMATION = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";

另外,我发现一个有趣的事情,加密后byte []长度为256,但解码后它变为44:

    javax.crypto.IllegalBlockSizeException
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:513)
at javax.crypto.Cipher.doFinal(Cipher.java:1741)
at com.mayzusfs.android.moneypolo.app.utils.CryptoUtils.decode(CryptoUtils.java:82) // the line of the above method
at com.mayzusfs.android.moneypolo.app.FingerprintHandler.onAuthenticationSucceeded(FingerprintHandler.java:51)
at android.hardware.fingerprint.FingerprintManager$MyHandler.sendAuthenticatedSucceeded(FingerprintManager.java:975)
at android.hardware.fingerprint.FingerprintManager$MyHandler.handleMessage(FingerprintManager.java:896)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6501)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.security.KeyStoreException: Invalid input length
at android.security.KeyStore.getKeyStoreException(KeyStore.java:697)
at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:506)

我是加密新手,所以如果问题有点儿,请抱歉。我也尝试谷歌它并面对以下解决方案,但他们没有帮助我(也许我做错了): javax.crypto.IllegalBlockSizeException : Input length must be multiple of 16 when decrypting with padded cipher [duplicate]

javax.crypto.IllegalBlockSizeException

1 个答案:

答案 0 :(得分:0)

您指定在解码期间不想要换行,因此如果换行,则可能会得到不同的结果。但是通常情况下,这些行被包裹在76个字符之后,这些字符产生57个字符,而不是44个字节的数据。

这意味着 - 由于某种原因 - base 64解码器在到达字符串结尾之前停止了,或者字符串首先没有正确的大小。这可以是例如如果您尝试读取文件,并希望read方法始终返回文件的完整大小。

在编码之后和解码之前打印出基本64字符串以确定。预期输出大小为256字节,因为它等于密钥大小(以字节为单位)(这意味着您使用了2048位RSA密钥)。