Encryption java.io.IOException:解密子字符串时完成密码时出错

Encryption java.io.IOException:解密子字符串时完成密码时出错,encryption,android-keystore,Encryption,Android Keystore,我遇到了一个问题,我不知道该如何解决。我使用此线程作为参考: 基本上,我把一个字符串分成块,然后加密每个块。但当需要以同样的方式解密时,它就永远不会起作用。它总是给我一个恼人的例外: “java.io.IOException:完成密码时出错” 基本上,我将字符串拆分如下: static public class RSAString { private ArrayList<String> mChunkList = new ArrayList<String>();

我遇到了一个问题,我不知道该如何解决。我使用此线程作为参考:

基本上,我把一个字符串分成块,然后加密每个块。但当需要以同样的方式解密时,它就永远不会起作用。它总是给我一个恼人的例外:

“java.io.IOException:完成密码时出错”

基本上,我将字符串拆分如下:

static public class RSAString {
    private ArrayList<String> mChunkList = new ArrayList<String>();
    RSAString() {
        mChunkList.clear();
    }
    public ArrayList<String> getChunkList() {
        return mChunkList;
    }
    RSAString(String stringSrc) {
        if (stringSrc.length() < CHUNK_SIZE) {
            mChunkList.add(stringSrc);
        } else {
            int j = 0;
            for (int i = 0; i < stringSrc.length() / CHUNK_SIZE; i++) {
                String subString = stringSrc.substring(j, j + CHUNK_SIZE);
                mChunkList.add(subString);
                j += CHUNK_SIZE;
            }
            int leftOver = stringSrc.length() % CHUNK_SIZE;
            if (leftOver > 0) {
                String subString = stringSrc.substring(j, j + leftOver);
                mChunkList.add(subString);
            }
        }
    }
}

RSA不适合批量加密,因为它的速度非常慢(与AES相比超过1000倍)。如果可以的话,可以使用对称加密算法,如AES。如果需要RSA的两个密钥,请使用加密,即使用随机对称密钥加密数据,然后使用RSA密钥加密该密钥

对称加密的另一个好处是,库自动支持批量加密,在加密之前,您无需处理将数据分割成小块的操作

// This **DOES NOT** work
final String AndroidOpenSSLString = "AndroidOpenSSL";
final String AndroidKeyStoreBCWorkaroundString = "AndroidKeyStoreBCWorkaround";
mProvider = Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? AndroidOpenSSLString : AndroidKeyStoreBCWorkaroundString;
Cipher outCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", mProvider);
outCipher.init(Cipher.DECRYPT_MODE, mPrivateKey);
for (String chunkEncrypted : rsaEcryptedText.getChunkList()) {
    byte[] cipherText = chunkEncrypted.getBytes("UTF-8");
    CipherInputStream cipherInputStream = new CipherInputStream(
            new ByteArrayInputStream(Base64.decode(cipherText, Base64.NO_WRAP)), outCipher);
    ArrayList<Byte> values = new ArrayList<>();
    int nextByte;
    while ((nextByte = cipherInputStream.read()) != -1) {
        values.add((byte) nextByte);
    }
    byte[] bytes = new byte[values.size()];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = values.get(i);
    }
    decryptedString += new String(bytes, 0, bytes.length, "UTF-8");
    cipherInputStream.close();
    cipherInputStream.reset();
}
for (String chunkEncrypted : rsaEcryptedText.getChunkList()) {
    // This works, but I am re-initializing the out cypher every time!
    // super slow!!! WHY DO I HAVE TO DO THIS?
    Cipher outCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", mProvider);
    outCipher.init(Cipher.DECRYPT_MODE, mPrivateKey);
    byte[] cipherText = chunkEncrypted.getBytes("UTF-8");
    CipherInputStream cipherInputStream = new CipherInputStream(
            new ByteArrayInputStream(Base64.decode(cipherText, Base64.NO_WRAP)), outCipher);
    ArrayList<Byte> values = new ArrayList<>();
    int nextByte;
    while ((nextByte = cipherInputStream.read()) != -1) {
        values.add((byte) nextByte);
    }
    byte[] bytes = new byte[values.size()];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = values.get(i);
    }
    decryptedString += new String(bytes, 0, bytes.length, "UTF-8");
    cipherInputStream.close();
    cipherInputStream.reset();
}
Cipher inCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", mProvider);
inCipher.init(Cipher.ENCRYPT_MODE, mPublicKey);
RSAString rsaStringPlainText = new RSAString(plainText);
for (String chunkPlain : rsaStringPlainText.getChunkList()) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CipherOutputStream cipherOutputStream = new CipherOutputStream(
            outputStream, inCipher);
    cipherOutputStream.write(chunkPlain.getBytes("UTF-8"));
    cipherOutputStream.flush();
    cipherOutputStream.close();
    byte[] ecryptedText = Base64.encode(outputStream.toByteArray(), Base64.NO_WRAP);
    encryptedStringOut.mChunkList.add(new String(ecryptedText));
}