org.spongycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pg Show documentation
Show all versions of pg Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle
intended for the Android platform. Android unfortunately ships with a stripped-down version of
Bouncy Castle, which prevents easy upgrades - Spongy Castle overcomes this and provides a full,
up-to-date version of the Bouncy Castle cryptographic libs.
The newest version!
package org.spongycastle.openpgp.operator.bc;
import org.spongycastle.crypto.BufferedBlockCipher;
import org.spongycastle.crypto.InvalidCipherTextException;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.spongycastle.openpgp.operator.PGPDigestCalculatorProvider;
public class BcPBESecretKeyDecryptorBuilder
{
private PGPDigestCalculatorProvider calculatorProvider;
public BcPBESecretKeyDecryptorBuilder(PGPDigestCalculatorProvider calculatorProvider)
{
this.calculatorProvider = calculatorProvider;
}
public PBESecretKeyDecryptor build(char[] passPhrase)
{
return new PBESecretKeyDecryptor(passPhrase, calculatorProvider)
{
public byte[] recoverKeyData(int encAlgorithm, byte[] key, byte[] iv, byte[] keyData, int keyOff, int keyLen)
throws PGPException
{
try
{
BufferedBlockCipher c = BcUtil.createSymmetricKeyWrapper(false, BcImplProvider.createBlockCipher(encAlgorithm), key, iv);
byte[] out = new byte[keyLen];
int outLen = c.processBytes(keyData, keyOff, keyLen, out, 0);
outLen += c.doFinal(out, outLen);
return out;
}
catch (InvalidCipherTextException e)
{
throw new PGPException("decryption failed: " + e.getMessage(), e);
}
}
};
}
}