Search in sources :

Example 1 with InvalidBackupCodeException

use of org.jivesoftware.smackx.ox.exception.InvalidBackupCodeException in project Smack by igniterealtime.

the class SecretKeyBackupHelper method restoreSecretKeyBackup.

/**
 * Decrypt a secret key backup and return the {@link PGPSecretKeyRing} contained in it.
 * TODO: Return a PGPSecretKeyRingCollection instead?
 *
 * @param backup encrypted {@link SecretkeyElement} containing the backup
 * @param backupCode passphrase for decrypting the {@link SecretkeyElement}.
 * @return the TODO javadoc me please
 * @throws InvalidBackupCodeException in case the provided backup code is invalid.
 * @throws IOException IO is dangerous.
 * @throws PGPException PGP is brittle.
 */
public static PGPSecretKeyRing restoreSecretKeyBackup(SecretkeyElement backup, OpenPgpSecretKeyBackupPassphrase backupCode) throws InvalidBackupCodeException, IOException, PGPException {
    byte[] encrypted = Base64.decode(backup.getB64Data());
    InputStream encryptedIn = new ByteArrayInputStream(encrypted);
    ByteArrayOutputStream plaintextOut = new ByteArrayOutputStream();
    try {
        DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify().onInputStream(encryptedIn).withOptions(new ConsumerOptions().addDecryptionPassphrase(Passphrase.fromPassword(backupCode.toString())));
        Streams.pipeAll(decryptionStream, plaintextOut);
        decryptionStream.close();
    } catch (MissingDecryptionMethodException e) {
        throw new InvalidBackupCodeException("Could not decrypt secret key backup. Possibly wrong passphrase?", e);
    }
    byte[] decrypted = plaintextOut.toByteArray();
    return PGPainless.readKeyRing().secretKeyRing(decrypted);
}
Also used : MissingDecryptionMethodException(org.pgpainless.exception.MissingDecryptionMethodException) InvalidBackupCodeException(org.jivesoftware.smackx.ox.exception.InvalidBackupCodeException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ConsumerOptions(org.pgpainless.decryption_verification.ConsumerOptions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DecryptionStream(org.pgpainless.decryption_verification.DecryptionStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InvalidBackupCodeException (org.jivesoftware.smackx.ox.exception.InvalidBackupCodeException)1 ConsumerOptions (org.pgpainless.decryption_verification.ConsumerOptions)1 DecryptionStream (org.pgpainless.decryption_verification.DecryptionStream)1 MissingDecryptionMethodException (org.pgpainless.exception.MissingDecryptionMethodException)1