我的Android应用程序有一个问题,只会经常发生 . 我的数据库中基本上有一堆数据,它被转换为XML String,然后XML String被加密并保存在一个文件中 . 问题是,有时,当文件超过大约3000行时,文件已损坏,因此当文件被解密时,它的末尾会有一些垃圾,而某些文件会丢失 . 我不确定问题是在加密String还是将对象转换为XML . 我试图重现,我似乎无法理解为什么它偶尔会发生?

下面是代码

public static boolean exportDB(int wellnessDayId) {
    Serializer serializer = new Persister();
    WellnessDay backToXMLWellnesday = ExportDbToXml.buildXmlDataModel(wellnessDayId);
    if (backToXMLWellnesday == null) {
        return false;
    }
    StringWriter sw = new StringWriter();
    FileOutputStream fos = null;
    FileOutputStream fosbackup = null;
    try {
        serializer.write(backToXMLWellnesday, sw);
        DesHelper des = new DesHelper("12345678");
        byte[] decryptedBytes = sw.toString().getBytes();

        //Calculating the size that the array should be (ie multiples of 8)
        Double len = Math.ceil(((double) decryptedBytes.length) / EIGHT) * EIGHT;
        byte[] decryptedBytesPadded = new byte[len.intValue()];

        //Initializing to whitespace character
        byte whiteSpaceBytevalue = 32;
        for (int k=0; k< decryptedBytesPadded.length; k++){
            decryptedBytesPadded[k]= whiteSpaceBytevalue;
        }

        //Copying the array into the byte array that is the correct length (ie multiples of 8)
        System.arraycopy(decryptedBytes, 0, decryptedBytesPadded, 0, Math.min(decryptedBytes.length, len.intValue()));

        byte[] encryptedBytes = des.encrypt(decryptedBytesPadded);
        String filename = "Export.blb";
        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File(sdCard.getAbsolutePath() + "/export");
        dir.mkdirs();

        File file = new File(dir, filename);
        file.createNewFile();
        fos = new FileOutputStream(file);

        for (int p = 0; p < encryptedBytes.length; p++) {
            fos.write(encryptedBytes[p]);
        }

        Format formatter;
        Date date = Calendar.getInstance().getTime();
        formatter = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
        String dateFormatted = formatter.format(date);
        //Create Backup file in /Wellnessdays/archive
         File dirBackup = new File(sdCard.getAbsolutePath() + "/archive");
        dirBackup.mkdirs();
        File filebackup = new File(dirBackup, dateFormatted + " - " + filename);
        filebackup.createNewFile();
        fosbackup = new FileOutputStream(filebackup);
        for (int p = 0; p < encryptedBytes.length; p++) {
            fosbackup.write(encryptedBytes[p]);
        }


    } catch (FileNotFoundException e) {
        Log.e(TAG, e.getMessage());
        return false;
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
        return false;
    } catch (GeneralSecurityException e) {
        Log.e(TAG, e.getMessage());
        return false;
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        return false;
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                Log.e("DatabaseExportToXML", e.getMessage());
            }
        }
        if (fosbackup!=null){
            try {
                fosbackup.close();
            } catch (IOException e) {
                Log.e("DatabaseExportToXML", e.getMessage());
            }
        }
    }
    return true;
}

public class DesHelper {

public static int MAX_KEY_LENGTH = DESKeySpec.DES_KEY_LEN;
//private static String ENCRYPTION_ALGORITHM = "DES/CBC/PKCS5Padding";
private static String ENCRYPTION_ALGORITHM = "DES/ECB/NoPadding";
private static String ENCRYPTION_KEY_TYPE = "DES";

private final SecretKeySpec keySpec;

public DesHelper(String passphrase) {
    byte[] key;
    try {
        key = passphrase.getBytes("UTF8");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException(e);
    }
    //The key is always 8 bytes, no need to pad the key
    keySpec = new SecretKeySpec(key, ENCRYPTION_KEY_TYPE);
}

private byte[] padKeyToLength(byte[] key, int len) {
    byte[] newKey = new byte[len];
    System.arraycopy(key, 0, newKey, 0, Math.min(key.length, len));
    return newKey;
}

public byte[] encrypt(byte[] unencrypted) throws GeneralSecurityException {
    return doCipher(unencrypted, Cipher.ENCRYPT_MODE);
}

public byte[] decrypt(byte[] encrypted) throws GeneralSecurityException {
    return doCipher(encrypted, Cipher.DECRYPT_MODE);
}

private byte[] doCipher(byte[] original, int mode) throws GeneralSecurityException {
    Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    cipher.init(mode, keySpec);
    return cipher.doFinal(original);
}

}

注意:这是在AsyncTask中运行的

我不明白为什么这种情况经常发生,XML Serializer可以接受的对象大小是否有最大限制?或者是否存在可以加密的字符串大小的问题?