Example usage for org.bouncycastle.openpgp PGPSecretKeyRing copyWithNewPassword

List of usage examples for org.bouncycastle.openpgp PGPSecretKeyRing copyWithNewPassword

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPSecretKeyRing copyWithNewPassword.

Prototype

public static PGPSecretKeyRing copyWithNewPassword(PGPSecretKeyRing ring, PBESecretKeyDecryptor oldKeyDecryptor,
        PBESecretKeyEncryptor newKeyEncryptor) throws PGPException 

Source Link

Document

Return a copy of the passed in secret key ring, with the private keys (where present) associated with the master key and sub keys are encrypted using a new password and the passed in algorithm.

Usage

From source file:org.kontalk.crypto.PGPUtils.java

License:Open Source License

public static PGPSecretKeyRing copySecretKeyRingWithNewPassword(byte[] privateKeyData, char[] oldPassphrase,
        char[] newPassphrase) throws PGPException, IOException {

    // load the secret key ring
    KeyFingerPrintCalculator fpr = new BcKeyFingerprintCalculator();
    PGPSecretKeyRing secRing = new PGPSecretKeyRing(privateKeyData, fpr);

    PGPDigestCalculatorProvider sha1CalcProv = new JcaPGPDigestCalculatorProviderBuilder().build();
    PBESecretKeyDecryptor decryptor = new JcePBESecretKeyDecryptorBuilder(sha1CalcProv)
            .setProvider(PGPUtils.PROVIDER).build(oldPassphrase);

    PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build()
            .get(HashAlgorithmTags.SHA1);
    PBESecretKeyEncryptor encryptor = new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc)
            .setProvider(PROVIDER).build(newPassphrase);

    return PGPSecretKeyRing.copyWithNewPassword(secRing, decryptor, encryptor);
}