本文整理汇总了Java中org.bouncycastle.openpgp.PGPPrivateKey类的典型用法代码示例。如果您正苦于以下问题:Java PGPPrivateKey类的具体用法?Java PGPPrivateKey怎么用?Java PGPPrivateKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PGPPrivateKey类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPPrivateKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: asLiteral
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
private static PGPLiteralData asLiteral( final byte[] message, final InputStream secretKeyRing,
final String secretPwd ) throws IOException, PGPException
{
PGPPrivateKey key = null;
PGPPublicKeyEncryptedData encrypted = null;
final PGPSecretKeyRingCollection keys =
new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( secretKeyRing ),
new JcaKeyFingerprintCalculator() );
for ( final Iterator<PGPPublicKeyEncryptedData> i = getEncryptedObjects( message );
( key == null ) && i.hasNext(); )
{
encrypted = i.next();
key = getPrivateKey( keys, encrypted.getKeyID(), secretPwd );
}
if ( key == null )
{
throw new IllegalArgumentException( "secret key for message not found." );
}
final InputStream stream = encrypted
.getDataStream( new JcePublicKeyDataDecryptorFactoryBuilder().setProvider( provider ).build( key ) );
return asLiteral( stream );
}
示例2: signData
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
@ReactMethod
public void signData(final String privKeyData, final String password, final String data, Promise promise) {
try {
// region Decode Private Key
PGPSecretKey secKey = PGPUtils.getSecretKey(privKeyData);
PGPPrivateKey privKey = PGPUtils.decryptArmoredPrivateKey(secKey, password);
// endregion
// region Sign Data
String signature = PGPUtils.signArmoredAscii(privKey, data, signatureAlgo);
WritableMap resultMap = Arguments.createMap();
resultMap.putString("asciiArmoredSignature", signature);
resultMap.putString("hashingAlgo", PGPUtils.hashAlgoToString(signatureAlgo));
resultMap.putString("fingerPrint", Utils.bytesToHex(secKey.getPublicKey().getFingerprint()));
promise.resolve(resultMap);
// endregion
} catch (Exception e) {
promise.reject(e);
}
}
示例3: signB64Data
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
@ReactMethod
public void signB64Data(final String privKeyData, final String password, final String b64Data, Promise promise) {
try {
// region Decode Base64
byte[] data = Base64.decode(b64Data, Base64.DEFAULT);
// endregion
// region Decode Private Key
PGPSecretKey secKey = PGPUtils.getSecretKey(privKeyData);
PGPPrivateKey privKey = PGPUtils.decryptArmoredPrivateKey(secKey, password);
// endregion
// region Sign Data
String signature = PGPUtils.signArmoredAscii(privKey, data, signatureAlgo);
WritableMap resultMap = Arguments.createMap();
resultMap.putString("asciiArmoredSignature", signature);
resultMap.putString("hashingAlgo", PGPUtils.hashAlgoToString(signatureAlgo));
resultMap.putString("fingerPrint", Utils.bytesToHex(secKey.getPublicKey().getFingerprint()));
promise.resolve(resultMap);
// endregion
} catch (Exception e) {
promise.reject(e);
}
}
示例4: getSecretKey
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
static PGPSecretKey getSecretKey(String privateKeyData) throws IOException, PGPException {
PGPPrivateKey privKey = null;
try (InputStream privStream = new ArmoredInputStream(new ByteArrayInputStream(privateKeyData.getBytes("UTF-8")))) {
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(privStream), new JcaKeyFingerprintCalculator());
Iterator keyRingIter = pgpSec.getKeyRings();
while (keyRingIter.hasNext()) {
PGPSecretKeyRing keyRing = (PGPSecretKeyRing)keyRingIter.next();
Iterator keyIter = keyRing.getSecretKeys();
while (keyIter.hasNext()) {
PGPSecretKey key = (PGPSecretKey)keyIter.next();
if (key.isSigningKey()) {
return key;
}
}
}
}
throw new IllegalArgumentException("Can't find signing key in key ring.");
}
示例5: signArmoredAscii
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
static String signArmoredAscii(PGPPrivateKey privateKey, String data, int signatureAlgo) throws IOException, PGPException {
String signature = null;
final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(new BcPGPContentSignerBuilder(privateKey.getPublicKeyPacket().getAlgorithm(), signatureAlgo));
signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, privateKey);
ByteArrayOutputStream signatureOutput = new ByteArrayOutputStream();
try( BCPGOutputStream outputStream = new BCPGOutputStream( new ArmoredOutputStream(signatureOutput)) ) {
Utils.processStringAsStream(data, new StreamHandler() {
@Override
public void handleStreamBuffer(byte[] buffer, int offset, int length) throws IOException {
signatureGenerator.update(buffer, offset, length);
}
});
signatureGenerator.generate().encode(outputStream);
}
signature = new String(signatureOutput.toByteArray(), "UTF-8");
return signature;
}
示例6: getPrivateKey
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
/**
* ***********************************************
*/
public static PGPPrivateKey getPrivateKey( final PGPSecretKey secretKey, final String secretPwd )
{
Preconditions.checkNotNull( secretKey );
Preconditions.checkNotNull( secretPwd );
try
{
return secretKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().setProvider( provider ).build( secretPwd.toCharArray() ) );
}
catch ( Exception e )
{
LOG.error( "Unable to extract key {}: {}", secretKey.getKeyID(), e.getMessage() );
}
return null;
}
示例7: decrypt
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
public static byte[] decrypt( byte encData[], PGPPrivateKey privateKey ) throws PGPException, IOException
{
PGPPublicKeyEncryptedData pgpEncData = getPGPEncryptedData( encData );
InputStream is = getInputStream( privateKey, pgpEncData );
// IMPORTANT: pipe() should be before verify(). Otherwise we get "java.io.EOFException: Unexpected end of ZIP
// input stream".
byte data[] = pipe( is );
if ( !pgpEncData.verify() )
{
throw new PGPDataValidationException( "Data integrity check failed" );
}
return data;
}
示例8: test
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
private static void test( byte data[], PGPPrivateKey privateKey, PGPPublicKey publicKey ) throws Exception
{
byte encData[] = PGPEncrypt.encrypt( data, publicKey );
byte decData[] = PGPDecrypt.decrypt( encData, privateKey );
assertArrayEquals( data, decData );
}
示例9: decode
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
/**
* Deciphers a ghostryde file from an in-memory byte array.
*
* @throws PGPException
* @throws IOException
*/
public static DecodeResult decode(byte[] data, PGPPrivateKey key)
throws IOException, PGPException {
checkNotNull(data, "data");
Ghostryde ghost = new Ghostryde(1024 * 64);
ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
ByteArrayOutputStream output = new ByteArrayOutputStream();
String name;
DateTime modified;
try (Decryptor decryptor = ghost.openDecryptor(dataStream, key);
Decompressor decompressor = ghost.openDecompressor(decryptor);
Input input = ghost.openInput(decompressor)) {
name = input.getName();
modified = input.getModified();
ByteStreams.copy(input, output);
}
return new DecodeResult(output.toByteArray(), name, modified);
}
示例10: openDecryptor
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
/**
* Opens a new {@link Decryptor} (Reading Step 1/3)
*
* <p>This is the first step in opening a ghostryde file. After this method, you'll want to
* call {@link #openDecompressor(Decryptor)}.
*
* @param input is an {@link InputStream} of the ghostryde file data.
* @param privateKey is the private encryption key of the recipient (which is us!)
* @throws IOException
* @throws PGPException
*/
@CheckReturnValue
public Decryptor openDecryptor(@WillNotClose InputStream input, PGPPrivateKey privateKey)
throws IOException, PGPException {
checkNotNull(privateKey, "privateKey");
PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input"));
PGPEncryptedDataList crypts = pgpCast(fact.nextObject(), PGPEncryptedDataList.class);
checkState(crypts.size() > 0);
if (crypts.size() > 1) {
logger.warningfmt("crypts.size() is %d (should be 1)", crypts.size());
}
PGPPublicKeyEncryptedData crypt = pgpCast(crypts.get(0), PGPPublicKeyEncryptedData.class);
if (crypt.getKeyID() != privateKey.getKeyID()) {
throw new PGPException(String.format(
"Message was encrypted for keyid %x but ours is %x",
crypt.getKeyID(), privateKey.getKeyID()));
}
return new Decryptor(
crypt.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey)),
crypt);
}
示例11: lookupKeyPair
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
/**
* Same as {@link #lookupPublicKey} but also retrieves the associated private key.
*
* @throws VerifyException if either keys couldn't be found.
* @see #lookupPublicKey
*/
@SuppressWarnings("deprecation")
public static PGPKeyPair lookupKeyPair(
PGPPublicKeyRingCollection publics,
PGPSecretKeyRingCollection privates,
String query,
KeyRequirement want) {
PGPPublicKey publicKey = lookupPublicKey(publics, query, want);
PGPPrivateKey privateKey;
try {
PGPSecretKey secret = verifyNotNull(privates.getSecretKey(publicKey.getKeyID()),
"Keyring missing private key associated with public key id: %x (query '%s')",
publicKey.getKeyID(), query);
// We do not support putting a password on the private key so we're just going to
// put char[0] here.
privateKey = secret.extractPrivateKey(
new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
.build(new char[0]));
} catch (PGPException e) {
throw new VerifyException(e.getMessage());
}
return new PGPKeyPair(publicKey, privateKey);
}
示例12: testEncryptOnly
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
@Theory
public void testEncryptOnly(Content content) throws Exception {
Keyring keyring = new FakeKeyringModule().get();
byte[] data = content.get().getBytes(UTF_8);
PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey();
PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey();
Ghostryde ghost = new Ghostryde(1024);
ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey)) {
encryptor.write(data);
}
ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray());
bsOut.reset();
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) {
ByteStreams.copy(decryptor, bsOut);
}
assertThat(new String(bsOut.toByteArray(), UTF_8)).isEqualTo(content.get());
}
示例13: testEncryptCompressOnly
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
@Theory
public void testEncryptCompressOnly(Content content) throws Exception {
Keyring keyring = new FakeKeyringModule().get();
PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey();
PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey();
byte[] data = content.get().getBytes(UTF_8);
Ghostryde ghost = new Ghostryde(1024);
ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey);
Ghostryde.Compressor kompressor = ghost.openCompressor(encryptor)) {
kompressor.write(data);
}
assertThat(new String(bsOut.toByteArray(), UTF_8)).isNotEqualTo(content.get());
ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray());
bsOut.reset();
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey);
Ghostryde.Decompressor decompressor = ghost.openDecompressor(decryptor)) {
ByteStreams.copy(decompressor, bsOut);
}
assertThat(new String(bsOut.toByteArray(), UTF_8)).isEqualTo(content.get());
}
示例14: mockPrivateKey
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
private static PGPPrivateKey mockPrivateKey(
boolean altId,
boolean altBcpgKeyFormat,
boolean altBcpgKeyEncoded,
boolean altPublicKeyPacketEncoded)
throws IOException {
String bcpgKeyFormat = altBcpgKeyFormat ? "alternate" : "bcpgFormat";
String bcpgKeyEncoded = altBcpgKeyEncoded ? "alternate" : "bcpgEncoded";
String publicKeyPacketEncoded = altPublicKeyPacketEncoded ? "alternate" : "packetEncoded";
BCPGKey bcpgKey = mock(BCPGKey.class);
PublicKeyPacket publicKeyPacket = mock(PublicKeyPacket.class);
when(bcpgKey.getFormat()).thenReturn(bcpgKeyFormat);
when(bcpgKey.getEncoded()).thenReturn(bcpgKeyEncoded.getBytes(UTF_8));
when(publicKeyPacket.getEncoded()).thenReturn(publicKeyPacketEncoded.getBytes(UTF_8));
return new PGPPrivateKey(altId ? 2 : 1, publicKeyPacket, bcpgKey);
}
示例15: signExternal
import org.bouncycastle.openpgp.PGPPrivateKey; //导入依赖的package包/类
public byte[] signExternal(String input) throws IOException, PGPException {
PGPSecretKey signKey = readSecretKey();
PGPPrivateKey privKey = signKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray()));
PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator(
new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC"));
sigGenerator.init(PGPSignature.BINARY_DOCUMENT, privKey);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) {
BCPGOutputStream bOut = new BCPGOutputStream(aOut);
sigGenerator.update(input.getBytes(Charsets.UTF_8));
sigGenerator.generate().encode(bOut);
}
return buffer.toByteArray();
}