MuntashirAkon/AppManager · error · BackupException

Failed to encrypt

Error message

Failed to encrypt 

What it means

Thrown by SBConverter.backupApkFile when mBackupItem.encrypt() throws IOException while encrypting the freshly created APK backup archive(s). The message uses Arrays.toString(sourceFiles), so the file list in the message shows the pre-encryption paths that failed to encrypt.

Source

Thrown at app/src/main/java/io/github/muntashirakon/AppManager/backup/convert/SBConverter.java:194

                mChecksum.add(CERT_PREFIX + i, checksums[i]);
            }
        } catch (Exception ignore) {
        }
        // Backup APK files
        String[] apkFiles = ArrayUtils.appendElement(String.class, mDestMetadata.metadata.splitConfigs, mDestMetadata.metadata.apkName);
        String sourceBackupFilePrefix = BackupUtils.getSourceFilePrefix(getExt(mDestMetadata.info.tarType));
        Path[] sourceFiles;
        try {
            // We have to specify APK files because the folder may contain many
            sourceFiles = TarUtils.create(mDestMetadata.info.tarType, sourceDir, mBackupItem.getUnencryptedBackupPath(), sourceBackupFilePrefix,
                    apkFiles, null, null, false).toArray(new Path[0]);
        } catch (Throwable th) {
            throw new BackupException("APK files backup is requested but no APK files have been backed up.", th);
        }
        try {
            sourceFiles = mBackupItem.encrypt(sourceFiles);
        } catch (IOException e) {
            throw new BackupException("Failed to encrypt " + Arrays.toString(sourceFiles));
        }
        for (Path file : sourceFiles) {
            mChecksum.add(file.getName(), DigestUtils.getHexDigest(mDestMetadata.info.checksumAlgo, file));
        }
    }

    private void backupData() throws BackupException {
        List<Path> dataFiles = new ArrayList<>(3);
        try {
            if (mDestMetadata.info.flags.backupInternalData()) {
                dataFiles.add(getIntDataFile());
            }
            if (mDestMetadata.info.flags.backupExternalData()) {
                dataFiles.add(getExtDataFile());
            }
            if (mDestMetadata.info.flags.backupMediaObb()) {
                dataFiles.add(getObbFile());
            }

View on GitHub (pinned to 0152f468fc)

Solutions

  1. Verify the encryption credentials (password/key/keystore alias) configured for the backup item are correct and available.
  2. Check free disk space in the backup destination before converting.
  3. Confirm the backup destination path is writable (storage permissions, scoped-storage on Android 11+).
  4. Either fix encryption settings or disable encryption in destination metadata (crypto flag) so encrypt() is not invoked.
  5. Retry the conversion; transient I/O failures (e.g., media store sync) can cause spurious IOExceptions.

Example fix

// before: encrypted metadata with stale key
metadataV2.crypto = true;
// after: ensure credentials match or disable encryption
if (encryptionKey == null) {
    metadataV2.crypto = false;
} else {
    metadataV2.crypto = true; // key verified against keystore beforehand
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (metadata.info.crypto) {
    if (encryptionKey == null || !keyExistsInKeystore(alias))
        throw new IllegalStateException("encryption requested but credentials unavailable");
}
if (destDir.getUsableSpace() < estimatedSize * 2)
    throw new IllegalStateException("insufficient space for encrypted copy");

Try / catch

try { converter.convert(); }
catch (BackupException e) {
    if (e.getMessage().startsWith("Failed to encrypt")) {
        // re-prompt for password or disable encryption, then retry
    }
}

Prevention

When it happens

Trigger: An encrypted backup was requested (crypto flag set in metadata) and encrypt() failed, e.g. wrong/missing encryption key or password, unsupported cipher in the backup item, output file not writable, or disk full while writing the encrypted copy of the tar in mBackupItem.getUnencryptedBackupPath().

Common situations: User changed their backup password/key between runs; converting a backup with encryption enabled but the keystore alias or password is unavailable; low storage on the device so the encrypted output cannot be written.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/5e7aad0a4dd8a1ee. Report an issue: GitHub.