javax.crypto.IllegalBlockSizeException引起:android.security.KeyStoreException:输入长度无效

时间:2018-01-19 19:53:53

标签: java android android-studio encryption rsa

我目前正在制作一个存储加密文件的小型Android程序,以后可以加载+解密。我在这里遇到了我的代码问题,似乎无法找到合适的答案。 我的代码:

private void prepKeys() throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException, KeyStoreException, IOException, CertificateException, UnrecoverableEntryException, InvalidAlgorithmParameterException, NoSuchProviderException {
    String alias = "TESTINGKEY10";

    KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
    ks.load(null);
    kpg = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");

    if(ks.containsAlias(alias)) {
        KeyStore.Entry entry = ks.getEntry(alias, null);

        privateKey = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();
        publicKey = ks.getCertificate(alias).getPublicKey();
    }
    else {
        kpg.initialize(new KeyGenParameterSpec.Builder(
                alias,
                KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
                .setKeySize(1024)
                .build());

        kp = kpg.generateKeyPair();
        publicKey = kp.getPublic();
        privateKey = kp.getPrivate();

    }
}

public byte[] RSAEncrypt(final String plain) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    encryptedBytes = cipher.doFinal(plain.getBytes());
    return encryptedBytes;
}

@RequiresApi(api = Build.VERSION_CODES.O)
public String RSADecrypt(final byte[] encryptedBytes) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    cipher1 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher1.init(Cipher.DECRYPT_MODE, privateKey);
    decryptedBytes = cipher1.doFinal(encryptedBytes);
    decrypted = new String(decryptedBytes);
    return decrypted;
}

因此,它会在app app上加载或生成带有prepKeys()的键,并在需要时加载/解密文本。

我得到的错误是:

W/System.err: javax.crypto.IllegalBlockSizeException
W/System.err:     at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:513)
W/System.err:     at javax.crypto.Cipher.doFinal(Cipher.java:1741)
W/System.err:     at com.example.aizaku.myapplication.MainActivity.RSADecrypt(MainActivity.java:113)
W/System.err:     at com.example.aizaku.myapplication.MainActivity$1.onClick(MainActivity.java:202)
W/System.err:     at android.view.View.performClick(View.java:6294)
W/System.err:     at android.view.View$PerformClick.run(View.java:24770)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:790)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err:     at android.os.Looper.loop(Looper.java:164)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6494)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
W/System.err: Caused by: android.security.KeyStoreException: Invalid input length
W/System.err:     at android.security.KeyStore.getKeyStoreException(KeyStore.java:697)
W/System.err:     at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.update(KeyStoreCryptoOperationChunkedStreamer.java:132)
W/System.err:     at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:217)
W/System.err:     at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:506)
W/System.err:   ... 12 more

在尝试解密数据(即使是小的加密文本" abc")时会发生这种情况

decryptedBytes = cipher1.doFinal(encryptedBytes);

请帮我找到答案:)

0 个答案:

没有答案