本文整理汇总了Java中org.bouncycastle.openpgp.PGPSecretKey类的典型用法代码示例。如果您正苦于以下问题:Java PGPSecretKey类的具体用法?Java PGPSecretKey怎么用?Java PGPSecretKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PGPSecretKey类属于org.bouncycastle.openpgp包,在下文中一共展示了PGPSecretKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的package包/类
static PGPSecretKey readSecretKey() throws Exception {
InputStream input = new ByteArrayInputStream(getSecKeyRing());
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(input),
new BcKeyFingerprintCalculator());
@SuppressWarnings("rawtypes")
Iterator keyRingIter = pgpSec.getKeyRings();
while (keyRingIter.hasNext()) {
PGPSecretKeyRing keyRing = (PGPSecretKeyRing) keyRingIter.next();
@SuppressWarnings("rawtypes")
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.");
}
示例2: signData
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的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.PGPSecretKey; //导入依赖的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.PGPSecretKey; //导入依赖的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: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的package包/类
public static PGPSecretKey readSecretKey( PGPSecretKeyRing keyRing ) throws PGPException
{
try
{
Iterator keyIter = keyRing.getSecretKeys();
while ( keyIter.hasNext() )
{
PGPSecretKey key = ( PGPSecretKey ) keyIter.next();
if ( key.isSigningKey() )
{
return key;
}
}
}
catch ( Exception e )
{
LOG.error( e.getMessage() );
}
return null;
}
示例6: getX509CertificateFromPgpKeyPair
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的package包/类
public static X509Certificate getX509CertificateFromPgpKeyPair( PGPPublicKey pgpPublicKey,
PGPSecretKey pgpSecretKey, String secretPwd,
String issuer, String subject, Date dateOfIssue,
Date dateOfExpiry, BigInteger serial )
throws PGPException, CertificateException, IOException
{
JcaPGPKeyConverter c = new JcaPGPKeyConverter();
PublicKey publicKey = c.getPublicKey( pgpPublicKey );
PrivateKey privateKey = c.getPrivateKey( pgpSecretKey.extractPrivateKey(
new JcePBESecretKeyDecryptorBuilder().setProvider( provider ).build( secretPwd.toCharArray() ) ) );
X509v3CertificateBuilder certBuilder =
new X509v3CertificateBuilder( new X500Name( issuer ), serial, dateOfIssue, dateOfExpiry,
new X500Name( subject ), SubjectPublicKeyInfo.getInstance( publicKey.getEncoded() ) );
byte[] certBytes = certBuilder.build( new JCESigner( privateKey, "SHA256withRSA" ) ).getEncoded();
CertificateFactory certificateFactory = CertificateFactory.getInstance( "X.509" );
return ( X509Certificate ) certificateFactory.generateCertificate( new ByteArrayInputStream( certBytes ) );
}
示例7: getPrivateKey
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的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;
}
示例8: testSignEncryptAndDecryptVerify
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的package包/类
@Test
public void testSignEncryptAndDecryptVerify() throws Exception
{
PGPSecretKey signingKey =
PGPEncryptionUtil.findSecretKeyByFingerprint( findFile( SECRET_KEYRING ), SECRET_KEY_FINGERPRINT );
PGPPublicKey encryptingKey =
PGPEncryptionUtil.findPublicKeyByFingerprint( findFile( PUBLIC_KEYRING ), PUBLIC_KEY_FINGERPRINT );
byte[] signedAndEncryptedMessage =
PGPEncryptionUtil.signAndEncrypt( MESSAGE.getBytes(), signingKey, SECRET_PWD, encryptingKey, true );
PGPSecretKey decryptingSecretKey = PGPEncryptionUtil.findSecretKeyByFingerprint( findFile( SECRET_KEYRING ),
PGPEncryptionUtil.BytesToHex( encryptingKey.getFingerprint() ) );
byte[] decryptedAndVerifiedMessage = PGPEncryptionUtil
.decryptAndVerify( signedAndEncryptedMessage, decryptingSecretKey, SECRET_PWD,
signingKey.getPublicKey() );
assertTrue( Arrays.equals( MESSAGE.getBytes(), decryptedAndVerifiedMessage ) );
}
示例9: testGetX509CertificateFromPgpKeyPair
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的package包/类
@Test
public void testGetX509CertificateFromPgpKeyPair() throws Exception
{
Date today = new Date();
PGPPublicKey pgpPublicKey = PGPEncryptionUtil.findPublicKeyById( findFile( PUBLIC_KEYRING ), PUBLIC_KEY_ID );
PGPSecretKey pgpSecretKey = PGPEncryptionUtil.findSecretKeyById( findFile( SECRET_KEYRING ), SECRET_KEY_ID );
X509Certificate x509Certificate = PGPEncryptionUtil
.getX509CertificateFromPgpKeyPair( pgpPublicKey, pgpSecretKey, SECRET_PWD,
"C=ZA, ST=Western Cape, L=Cape Town, O=Thawte Consulting cc,"
+ " OU=Certification Services Division,"
+ " CN=Thawte Server CA/[email protected]",
"C=US, ST=Maryland, L=Pasadena, O=Brent Baccala,"
+ "OU=FreeSoft, CN=www.freesoft.org/[email protected]",
today, new Date( today.getTime() + ( 1000 * 60 * 60 * 24 ) ), new BigInteger( "1" ) );
assertNotNull( x509Certificate );
JcaPGPKeyConverter c = new JcaPGPKeyConverter();
PublicKey publicKey = c.getPublicKey( pgpSecretKey.getPublicKey() );
x509Certificate.verify( publicKey, new BouncyCastleProvider() );
}
示例10: readSecretKey
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的package包/类
private static PGPSecretKey readSecretKey( InputStream is ) throws IOException, PGPException
{
PGPSecretKeyRingCollection pgpSec =
new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream( is ), 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." );
}
示例11: getSecretKeyByFingerprint
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的package包/类
@Override
public PGPSecretKey getSecretKeyByFingerprint( String fingerprint )
{
PGPSecretKey secretKey = null;
try
{
ByteArrayInputStream barIn =
new ByteArrayInputStream( securityDataService.getSecretKeyData( fingerprint ).getData() );
secretKey = PGPEncryptionUtil.findSecretKeyByFingerprint( barIn, fingerprint );
}
catch ( Exception ex )
{
LOG.error( " ***** Error getting Secret key:" + ex.toString(), ex );
}
return secretKey;
}
示例12: signPublicKey
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的package包/类
/**
* Signs a public key
*
* @param publicKeyRing a public key ring containing the single public key to sign
* @param id the id we are certifying against the public key
* @param secretKey the signing key
* @param secretKeyPassword the signing key password
*
* @return a public key ring with the signed public key
*/
@Override
public PGPPublicKeyRing signPublicKey( PGPPublicKeyRing publicKeyRing, String id, PGPSecretKey secretKey,
String secretKeyPassword )
{
try
{
if ( Strings.isNullOrEmpty( secretKeyPassword ) )
{
secretKeyPassword = keyManager.getSecurityKeyData().getSecretKeyringPwd();
}
return PGPEncryptionUtil.signPublicKey( publicKeyRing, id, secretKey, secretKeyPassword );
}
catch ( Exception e )
{
//throw custom exception
throw new ActionFailedException( e );
}
}
示例13: lookupKeyPair
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的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);
}
示例14: serializeKeyPair
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的package包/类
/**
* Serialize a PGPKeyPair
*
* <p>Use this to serialize a PGPPrivateKey as well (pairing it with the corresponding
* PGPPublicKey), as private keys can't be serialized on their own.
*/
public static byte[] serializeKeyPair(PGPKeyPair keyPair) throws IOException, PGPException {
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
// NOTE: We have to close the ArmoredOutputStream before calling the underlying OutputStream's
// "toByteArray". Failing to do so would result in a truncated serialization as we took the
// byte array before the ArmoredOutputStream wrote all the data.
//
// Even "flushing" the ArmoredOutputStream isn't enough - as there are parts that are only
// written by the ArmoredOutputStream when it is closed: the "-----END PGP PRIVATE KEY
// BLOCK-----" (or similar) footer.
try (ArmoredOutputStream out = new ArmoredOutputStream(byteStream)) {
new PGPSecretKey(
keyPair.getPrivateKey(),
keyPair.getPublicKey(),
new JcaPGPDigestCalculatorProviderBuilder()
.setProvider("BC")
.build()
.get(HashAlgorithmTags.SHA256),
true,
null).encode(out);
}
return byteStream.toByteArray();
}
}
示例15: signExternal
import org.bouncycastle.openpgp.PGPSecretKey; //导入依赖的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();
}