• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java SymmetricKeyAlgorithmTags类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags的典型用法代码示例。如果您正苦于以下问题:Java SymmetricKeyAlgorithmTags类的具体用法?Java SymmetricKeyAlgorithmTags怎么用?Java SymmetricKeyAlgorithmTags使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



SymmetricKeyAlgorithmTags类属于org.bouncycastle.bcpg包,在下文中一共展示了SymmetricKeyAlgorithmTags类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: encrypt

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
public static byte[] encrypt( final byte[] message, final PGPPublicKey publicKey, boolean armored )
        throws PGPException
{
    try
    {
        final ByteArrayInputStream in = new ByteArrayInputStream( message );
        final ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        final PGPLiteralDataGenerator literal = new PGPLiteralDataGenerator();
        final PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator( CompressionAlgorithmTags.ZIP );
        final OutputStream pOut =
                literal.open( comData.open( bOut ), PGPLiteralData.BINARY, "filename", in.available(), new Date() );
        Streams.pipeAll( in, pOut );
        comData.close();
        final byte[] bytes = bOut.toByteArray();
        final PGPEncryptedDataGenerator generator = new PGPEncryptedDataGenerator(
                new JcePGPDataEncryptorBuilder( SymmetricKeyAlgorithmTags.AES_256 ).setWithIntegrityPacket( true )
                                                                                   .setSecureRandom(
                                                                                           new SecureRandom() )

                                                                                   .setProvider( provider ) );
        generator.addMethod( new JcePublicKeyKeyEncryptionMethodGenerator( publicKey ).setProvider( provider ) );
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputStream theOut = armored ? new ArmoredOutputStream( out ) : out;
        OutputStream cOut = generator.open( theOut, bytes.length );
        cOut.write( bytes );
        cOut.close();
        theOut.close();
        return out.toByteArray();
    }
    catch ( Exception e )
    {
        throw new PGPException( "Error in encrypt", e );
    }
}
 
开发者ID:subutai-io,项目名称:base,代码行数:35,代码来源:PGPEncryptionUtil.java


示例2: createWrapper

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
static Wrapper createWrapper(int encAlgorithm)
    throws PGPException
{
    switch (encAlgorithm)
    {
    case SymmetricKeyAlgorithmTags.AES_128:
    case SymmetricKeyAlgorithmTags.AES_192:
    case SymmetricKeyAlgorithmTags.AES_256:
        return new RFC3394WrapEngine(new AESFastEngine());
    case SymmetricKeyAlgorithmTags.CAMELLIA_128:
    case SymmetricKeyAlgorithmTags.CAMELLIA_192:
    case SymmetricKeyAlgorithmTags.CAMELLIA_256:
        return new RFC3394WrapEngine(new CamelliaEngine());
    default:
        throw new PGPException("unknown wrap algorithm: " + encAlgorithm);
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:18,代码来源:BcImplProvider.java


示例3: createEncryptedNonCompressedData

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
void createEncryptedNonCompressedData(ByteArrayOutputStream bos, String keyringPath) throws Exception, IOException, PGPException,
        UnsupportedEncodingException {
    PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5)
            .setSecureRandom(new SecureRandom()).setProvider(getProvider()));
    encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(readPublicKey(keyringPath)));
    OutputStream encOut = encGen.open(bos, new byte[512]);
    PGPLiteralDataGenerator litData = new PGPLiteralDataGenerator();
    OutputStream litOut = litData.open(encOut, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[512]);

    try {
        litOut.write("Test Message Without Compression".getBytes("UTF-8"));
        litOut.flush();
    } finally {
        IOHelper.close(litOut);
        IOHelper.close(encOut, bos);
    }
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:18,代码来源:PGPDataFormatTest.java


示例4: createKeyWrapper

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
Cipher createKeyWrapper(int encAlgorithm)
    throws PGPException
{
    try
    {
        switch (encAlgorithm)
        {
        case SymmetricKeyAlgorithmTags.AES_128:
        case SymmetricKeyAlgorithmTags.AES_192:
        case SymmetricKeyAlgorithmTags.AES_256:
            return helper.createCipher("AESWrap");
        default:
            throw new PGPException("unknown wrap algorithm: " + encAlgorithm);
        }
    }
    catch (GeneralSecurityException e)
    {
        throw new PGPException("cannot create cipher: " + e.getMessage(), e);
    }
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:21,代码来源:OperatorHelper.java


示例5: getSymmetricCipherName

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
static String getSymmetricCipherName(
    int    algorithm)
{
    switch (algorithm)
    {
    case SymmetricKeyAlgorithmTags.NULL:
        return null;
    case SymmetricKeyAlgorithmTags.TRIPLE_DES:
        return "DESEDE";
    case SymmetricKeyAlgorithmTags.IDEA:
        return "IDEA";
    case SymmetricKeyAlgorithmTags.CAST5:
        return "CAST5";
    case SymmetricKeyAlgorithmTags.BLOWFISH:
        return "Blowfish";
    case SymmetricKeyAlgorithmTags.SAFER:
        return "SAFER";
    case SymmetricKeyAlgorithmTags.DES:
        return "DES";
    case SymmetricKeyAlgorithmTags.AES_128:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_192:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_256:
        return "AES";
    case SymmetricKeyAlgorithmTags.TWOFISH:
        return "Twofish";
    default:
        throw new IllegalArgumentException("unknown symmetric algorithm: " + algorithm);
    }
}
 
开发者ID:NoYouShutup,项目名称:CryptMeme,代码行数:32,代码来源:PGPUtil.java


示例6: getKeyLen

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
private static int getKeyLen(int algID)
    throws PGPException
{
    switch (algID)
    {
    case SymmetricKeyAlgorithmTags.AES_128:
        return 16;
    case SymmetricKeyAlgorithmTags.AES_192:
        return 24;
    case SymmetricKeyAlgorithmTags.AES_256:
        return 32;
    default:
        throw new PGPException("unknown symmetric algorithm ID: " + algID);
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:16,代码来源:RFC6637KDFCalculator.java


示例7: getKeyEncryptionOID

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
public static ASN1ObjectIdentifier getKeyEncryptionOID(int algID)
    throws PGPException
{
    switch (algID)
    {
    case SymmetricKeyAlgorithmTags.AES_128:
        return NISTObjectIdentifiers.id_aes128_wrap;
    case SymmetricKeyAlgorithmTags.AES_192:
        return NISTObjectIdentifiers.id_aes192_wrap;
    case SymmetricKeyAlgorithmTags.AES_256:
        return NISTObjectIdentifiers.id_aes256_wrap;
    default:
        throw new PGPException("unknown symmetric algorithm ID: " + algID);
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:16,代码来源:RFC6637Utils.java


示例8: BcEncryptor

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
public BcEncryptor(BcPublicKey publicKey, BcPrivateKey ourKey) {
    this.publicKey = checkNotNull(publicKey);
    this.ourKey = checkNotNull(ourKey);

    Security.addProvider(new BouncyCastleProvider());

    dataEncryptor = new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_256);
    dataEncryptor.setWithIntegrityPacket(true);
    dataEncryptor.setSecureRandom(new SecureRandom());
}
 
开发者ID:quedexnet,项目名称:java-api,代码行数:11,代码来源:BcEncryptor.java


示例9: testExceptionDecryptorIncorrectInputFormatSymmetricEncryptedData

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
@Test
public void testExceptionDecryptorIncorrectInputFormatSymmetricEncryptedData() throws Exception {

    byte[] payload = "Not Correct Format".getBytes("UTF-8");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5)
            .setSecureRandom(new SecureRandom()).setProvider(getProvider()));

    encGen.addMethod(new JcePBEKeyEncryptionMethodGenerator("pw".toCharArray()));

    OutputStream encOut = encGen.open(bos, new byte[1024]);
    PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP);
    OutputStream comOut = new BufferedOutputStream(comData.open(encOut));
    PGPLiteralDataGenerator litData = new PGPLiteralDataGenerator();
    OutputStream litOut = litData.open(comOut, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[1024]);
    litOut.write(payload);
    litOut.flush();
    litOut.close();
    comOut.close();
    encOut.close();
    MockEndpoint mock = getMockEndpoint("mock:exception");
    mock.expectedMessageCount(1);
    template.sendBody("direct:subkeyUnmarshal", bos.toByteArray());
    assertMockEndpointsSatisfied();

    checkThrownException(mock, IllegalArgumentException.class, null, "The input message body has an invalid format.");
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:29,代码来源:PGPDataFormatTest.java


示例10: getHeaders

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
protected Map<String, Object> getHeaders() {
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(PGPKeyAccessDataFormat.KEY_USERID, "[email protected]");
    headers.put(PGPKeyAccessDataFormat.KEY_USERIDS, Collections.singletonList("second"));
    headers.put(PGPKeyAccessDataFormat.SIGNATURE_KEY_USERID, "[email protected]");
    headers.put(PGPDataFormat.KEY_PASSWORD, "sdude");
    headers.put(PGPDataFormat.SIGNATURE_KEY_PASSWORD, "sdude");
    headers.put(PGPKeyAccessDataFormat.ENCRYPTION_ALGORITHM, SymmetricKeyAlgorithmTags.AES_128);
    headers.put(PGPKeyAccessDataFormat.SIGNATURE_HASH_ALGORITHM, HashAlgorithmTags.SHA512);
    headers.put(PGPKeyAccessDataFormat.COMPRESSION_ALGORITHM, CompressionAlgorithmTags.ZLIB);
    headers.put(PGPKeyAccessDataFormat.SIGNATURE_KEY_USERIDS, Collections.singletonList("second"));
    return headers;
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:14,代码来源:PGPDataFormatDynamicTest.java


示例11: getPassphrase

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
private char[] getPassphrase(final PgpKey pgpKey) {
	final PGPSecretKey secretKey = getPgpSecretKeyOrFail(pgpKey);
	if (secretKey.getKeyEncryptionAlgorithm() != SymmetricKeyAlgorithmTags.NULL) {
		final PgpAuthenticationCallback callback = getPgpAuthenticationCallbackOrFail();
		return callback.getPassphrase(pgpKey);
	}
	return null;
}
 
开发者ID:subshare,项目名称:subshare,代码行数:9,代码来源:BcPgpEncoder.java


示例12: generateKeyPair

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
public SecretKey generateKeyPair(final String id, final char[] pass) throws CryptoException {
  try {

    // This object generates individual key-pairs.
    final RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
    kpg.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x10001), new SecureRandom(), 2048, 12));

    // First create the master (signing) key with the generator.
    final PGPKeyPair keyPair = new BcPGPKeyPair(PGPPublicKey.RSA_GENERAL, kpg.generateKeyPair(), new Date());

    // Add a self-signature on the id
    final PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator();
    signhashgen.setKeyFlags(true, KeyFlags.CERTIFY_OTHER | KeyFlags.SIGN_DATA | KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);
    signhashgen.setPreferredCompressionAlgorithms(false, new int[] { CompressionAlgorithmTags.ZIP });
    signhashgen.setPreferredHashAlgorithms(false, new int[] { HashAlgorithmTags.SHA1 });
    signhashgen.setPreferredSymmetricAlgorithms(false, new int[] { SymmetricKeyAlgorithmTags.AES_256 });
    signhashgen.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);

    // Create a signature on the encryption subkey.
    final PGPSignatureSubpacketGenerator enchashgen = new PGPSignatureSubpacketGenerator();
    enchashgen.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);

    // Objects used to encrypt the secret key.

    // Finally, create the keyring itself. The constructor
    // takes parameters that allow it to generate the self
    // signature.
    final PGPDigestCalculator sha1Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
    final PBESecretKeyEncryptor secretKeyEncryptor = new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_128, sha1Calc).build(pass);
    final BcPGPContentSignerBuilder contentSigner = new BcPGPContentSignerBuilder(keyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1);
    final PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, keyPair, id, sha1Calc,
        signhashgen.generate(), null, contentSigner, secretKeyEncryptor);

    // return new SimpleKeyPair(new BcPGPPublicKey(publicKeyRing.getPublicKey()),
    return new BcPGPSecretKey(keyRingGen.generateSecretKeyRing().getSecretKey());
  }
  catch (final Exception e) {
    throw new CryptoException(e);
  }
}
 
开发者ID:hsch,项目名称:bcpg-simple,代码行数:41,代码来源:BcPGPKeyFactory.java


示例13: getSymmetricCipherName

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
public static String getSymmetricCipherName(
    int    algorithm)
{
    switch (algorithm)
    {
    case SymmetricKeyAlgorithmTags.NULL:
        return null;
    case SymmetricKeyAlgorithmTags.TRIPLE_DES:
        return "DESEDE";
    case SymmetricKeyAlgorithmTags.IDEA:
        return "IDEA";
    case SymmetricKeyAlgorithmTags.CAST5:
        return "CAST5";
    case SymmetricKeyAlgorithmTags.BLOWFISH:
        return "Blowfish";
    case SymmetricKeyAlgorithmTags.SAFER:
        return "SAFER";
    case SymmetricKeyAlgorithmTags.DES:
        return "DES";
    case SymmetricKeyAlgorithmTags.AES_128:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_192:
        return "AES";
    case SymmetricKeyAlgorithmTags.AES_256:
        return "AES";
    case SymmetricKeyAlgorithmTags.CAMELLIA_128:
        return "Camellia";
    case SymmetricKeyAlgorithmTags.CAMELLIA_192:
        return "Camellia";
    case SymmetricKeyAlgorithmTags.CAMELLIA_256:
        return "Camellia";
    case SymmetricKeyAlgorithmTags.TWOFISH:
        return "Twofish";
    default:
        throw new IllegalArgumentException("unknown symmetric algorithm: " + algorithm);
    }
}
 
开发者ID:Gadreel,项目名称:divconq,代码行数:38,代码来源:PGPUtil.java


示例14: generateKeyRingGenerator

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
public final static PGPKeyRingGenerator generateKeyRingGenerator(String id, char[] pass, int s2kcount,
		KeyGenPane.BackgroundTask bgt) throws Exception
{
	// This object generates individual key-pairs.
	RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();

	// Boilerplate RSA parameters, no need to change anything
	// except for the RSA key-size (2048). You can use whatever key-size
	// makes sense for you -- 4096, etc.
	kpg.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x10001), new SecureRandom(), 2048, 12));
	bgt.setProgressPub(10);
	// First create the master (signing) key with the generator.
	PGPKeyPair rsakp_sign = new BcPGPKeyPair(PGPPublicKey.RSA_SIGN, kpg.generateKeyPair(), new Date());
	// Then an encryption subkey.
	PGPKeyPair rsakp_enc = new BcPGPKeyPair(PGPPublicKey.RSA_ENCRYPT, kpg.generateKeyPair(), new Date());
	bgt.setProgressPub(50);
	// Add a self-signature on the id
	PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator();
	bgt.setProgressPub(55);
	// Add signed metadata on the signature.
	// 1) Declare its purpose
	signhashgen.setKeyFlags(false, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER);
	bgt.setProgressPub(60);
	// 2) Set preferences for secondary crypto algorithms to use when
	// sending messages to this key.
	signhashgen.setPreferredSymmetricAlgorithms(false, new int[] { SymmetricKeyAlgorithmTags.AES_256,
			SymmetricKeyAlgorithmTags.AES_192, SymmetricKeyAlgorithmTags.AES_128 });
	signhashgen.setPreferredHashAlgorithms(false, new int[] { HashAlgorithmTags.SHA256, HashAlgorithmTags.SHA1,
			HashAlgorithmTags.SHA384, HashAlgorithmTags.SHA512, HashAlgorithmTags.SHA224, });
	// 3) Request senders add additional checksums to the message (useful
	// when verifying unsigned messages.)
	signhashgen.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);

	// Create a signature on the encryption subkey.
	PGPSignatureSubpacketGenerator enchashgen = new PGPSignatureSubpacketGenerator();
	// Add metadata to declare its purpose
	enchashgen.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);

	// Objects used to encrypt the secret key.
	PGPDigestCalculator sha1Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
	PGPDigestCalculator sha256Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA256);
	bgt.setProgressPub(70);
	// bcpg 1.48 exposes this API that includes s2kcount. Earlier versions
	// use a default of 0x60.
	PBESecretKeyEncryptor pske = (new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha256Calc,
			s2kcount)).build(pass);

	// Finally, create the keyring itself. The constructor takes parameters
	// that allow it to generate the self signature.
	PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, rsakp_sign, id,
			sha1Calc, signhashgen.generate(), null,
			new BcPGPContentSignerBuilder(rsakp_sign.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1), pske);
	bgt.setProgressPub(80);
	// Add our encryption subkey, together with its signature.
	keyRingGen.addSubKey(rsakp_enc, enchashgen.generate(), null);
	bgt.setProgressPub(90);
	return keyRingGen;
}
 
开发者ID:AnonymOnline,项目名称:saveOrganizer,代码行数:59,代码来源:RSAGen.java


示例15: generateKeyRingGenerator

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
static PGPKeyRingGenerator generateKeyRingGenerator(String userId, int numBits, char[] passphrase) throws Exception  {
  RSAKeyPairGenerator keyPairGenerator = new RSAKeyPairGenerator();

  keyPairGenerator.init(
    new RSAKeyGenerationParameters(
      BigInteger.valueOf(0x10001),
      new SecureRandom(),
      numBits,
      12
    )
  );

  PGPKeyPair rsaKeyPairSign = new BcPGPKeyPair(
    PGPPublicKey.RSA_SIGN,
    keyPairGenerator.generateKeyPair(),
    new Date()
  );

  PGPKeyPair rsaKeyPairEncrypt = new BcPGPKeyPair(
    PGPPublicKey.RSA_ENCRYPT,
    keyPairGenerator.generateKeyPair(),
    new Date()
  );

  PGPSignatureSubpacketGenerator signHashGenerator = new PGPSignatureSubpacketGenerator();

  signHashGenerator.setKeyFlags(false, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER);

  signHashGenerator.setPreferredSymmetricAlgorithms(
    false,
    new int[] {
      SymmetricKeyAlgorithmTags.AES_256,
      SymmetricKeyAlgorithmTags.AES_192,
      SymmetricKeyAlgorithmTags.AES_128
    }
  );

  signHashGenerator.setPreferredHashAlgorithms(
    false,
    new int[] {
      HashAlgorithmTags.SHA512,
      HashAlgorithmTags.SHA384,
      HashAlgorithmTags.SHA256,
      HashAlgorithmTags.SHA1,    // Not recommended
      HashAlgorithmTags.SHA224,  // Not recommended
    }
  );

  signHashGenerator.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);

  PGPSignatureSubpacketGenerator encryptHashGenerator = new PGPSignatureSubpacketGenerator();

  encryptHashGenerator.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);

  PGPDigestCalculator sha1DigestCalculator = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
  PGPDigestCalculator sha512DigestCalculator = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA512);

  PBESecretKeyEncryptor secretKeyEncryptor = (
    new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha512DigestCalculator)
  )
    .build(passphrase);

  PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(
    PGPSignature.NO_CERTIFICATION,
    rsaKeyPairSign,
    userId,
    sha1DigestCalculator,
    signHashGenerator.generate(),
    null,
    new BcPGPContentSignerBuilder(rsaKeyPairSign.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA512),
    secretKeyEncryptor
  );

  keyRingGen.addSubKey(rsaKeyPairEncrypt, encryptHashGenerator.generate(), null);

  return keyRingGen;
}
 
开发者ID:quan-to,项目名称:react-native-pgp,代码行数:78,代码来源:PGPUtils.java


示例16: parseSecretKeyFromSExpr

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
/**
 * Parse a secret key from one of the GPG S expression keys associating it with the passed in public key.
 *
 * @return a secret key object.
 */
public static PGPSecretKey parseSecretKeyFromSExpr(InputStream inputStream, PBEProtectionRemoverFactory keyProtectionRemoverFactory, PGPPublicKey pubKey)
    throws IOException, PGPException
{
    SXprUtils.skipOpenParenthesis(inputStream);

    String type;

    type = SXprUtils.readString(inputStream, inputStream.read());
    if (type.equals("protected-private-key"))
    {
        SXprUtils.skipOpenParenthesis(inputStream);

        String curveName;

        String keyType = SXprUtils.readString(inputStream, inputStream.read());
        if (keyType.equals("ecc"))
        {
            SXprUtils.skipOpenParenthesis(inputStream);

            String curveID = SXprUtils.readString(inputStream, inputStream.read());
            curveName = SXprUtils.readString(inputStream, inputStream.read());

            SXprUtils.skipCloseParenthesis(inputStream);
        }
        else
        {
            throw new PGPException("no curve details found");
        }

        byte[] qVal;

        SXprUtils.skipOpenParenthesis(inputStream);

        type = SXprUtils.readString(inputStream, inputStream.read());
        if (type.equals("q"))
        {
            qVal = SXprUtils.readBytes(inputStream, inputStream.read());
        }
        else
        {
            throw new PGPException("no q value found");
        }

        SXprUtils.skipCloseParenthesis(inputStream);

        byte[] dValue = getDValue(inputStream, keyProtectionRemoverFactory, curveName);
        // TODO: check SHA-1 hash.

        return new PGPSecretKey(new SecretKeyPacket(pubKey.getPublicKeyPacket(), SymmetricKeyAlgorithmTags.NULL, null, null, new ECSecretBCPGKey(new BigInteger(1, dValue)).getEncoded()), pubKey);
    }

    throw new PGPException("unknown key type found");
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:59,代码来源:PGPSecretKey.java


示例17: getDValue

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
private static byte[] getDValue(InputStream inputStream, PBEProtectionRemoverFactory keyProtectionRemoverFactory, String curveName)
    throws IOException, PGPException
{
    String type;
    SXprUtils.skipOpenParenthesis(inputStream);

    String protection;
    S2K s2k;
    byte[] iv;
    byte[] secKeyData;

    type = SXprUtils.readString(inputStream, inputStream.read());
    if (type.equals("protected"))
    {
        protection = SXprUtils.readString(inputStream, inputStream.read());

        SXprUtils.skipOpenParenthesis(inputStream);

        s2k = SXprUtils.parseS2K(inputStream);

        iv = SXprUtils.readBytes(inputStream, inputStream.read());

        SXprUtils.skipCloseParenthesis(inputStream);

        secKeyData = SXprUtils.readBytes(inputStream, inputStream.read());
    }
    else
    {
        throw new PGPException("protected block not found");
    }

    PBESecretKeyDecryptor keyDecryptor = keyProtectionRemoverFactory.createDecryptor(protection);

    // TODO: recognise other algorithms
    byte[] key = keyDecryptor.makeKeyFromPassPhrase(SymmetricKeyAlgorithmTags.AES_128, s2k);

    byte[] data = keyDecryptor.recoverKeyData(SymmetricKeyAlgorithmTags.AES_128, key, iv, secKeyData, 0, secKeyData.length);

    //
    // parse the secret key S-expr
    //
    InputStream keyIn = new ByteArrayInputStream(data);

    SXprUtils.skipOpenParenthesis(keyIn);
    SXprUtils.skipOpenParenthesis(keyIn);
    SXprUtils.skipOpenParenthesis(keyIn);
    String name = SXprUtils.readString(keyIn, keyIn.read());
    return SXprUtils.readBytes(keyIn, keyIn.read());
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:50,代码来源:PGPSecretKey.java


示例18: makeRandomKey

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
public static byte[] makeRandomKey(
    int             algorithm,
    SecureRandom    random) 
    throws PGPException
{
    int        keySize = 0;
    
    switch (algorithm)
    {
    case SymmetricKeyAlgorithmTags.TRIPLE_DES:
        keySize = 192;
        break;
    case SymmetricKeyAlgorithmTags.IDEA:
        keySize = 128;
        break;
    case SymmetricKeyAlgorithmTags.CAST5:
        keySize = 128;
        break;
    case SymmetricKeyAlgorithmTags.BLOWFISH:
        keySize = 128;
        break;
    case SymmetricKeyAlgorithmTags.SAFER:
        keySize = 128;
        break;
    case SymmetricKeyAlgorithmTags.DES:
        keySize = 64;
        break;
    case SymmetricKeyAlgorithmTags.AES_128:
        keySize = 128;
        break;
    case SymmetricKeyAlgorithmTags.AES_192:
        keySize = 192;
        break;
    case SymmetricKeyAlgorithmTags.AES_256:
        keySize = 256;
        break;
    case SymmetricKeyAlgorithmTags.TWOFISH:
        keySize = 256;
        break;
    default:
        throw new PGPException("unknown symmetric algorithm: " + algorithm);
    }
    
    byte[]    keyBytes = new byte[(keySize + 7) / 8];
    
    random.nextBytes(keyBytes);
    
    return keyBytes;
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:50,代码来源:PGPUtil.java


示例19: signAndEncrypt

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
public static byte[] signAndEncrypt( final byte[] message, final PGPSecretKey secretKey, final String secretPwd,
                                     final PGPPublicKey publicKey, final boolean armored ) throws PGPException
{
    try
    {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(
                new JcePGPDataEncryptorBuilder( SymmetricKeyAlgorithmTags.AES_256 ).setWithIntegrityPacket( true )
                                                                                   .setSecureRandom(
                                                                                           new SecureRandom() )
                                                                                   .setProvider( provider ) );

        encryptedDataGenerator.addMethod(
                new JcePublicKeyKeyEncryptionMethodGenerator( publicKey ).setSecureRandom( new SecureRandom() )
                                                                         .setProvider( provider ) );

        final OutputStream theOut = armored ? new ArmoredOutputStream( out ) : out;
        final OutputStream encryptedOut = encryptedDataGenerator.open( theOut, new byte[4096] );

        final PGPCompressedDataGenerator compressedDataGenerator =
                new PGPCompressedDataGenerator( CompressionAlgorithmTags.ZIP );
        final OutputStream compressedOut = compressedDataGenerator.open( encryptedOut, new byte[4096] );
        final PGPPrivateKey privateKey = secretKey.extractPrivateKey(
                new JcePBESecretKeyDecryptorBuilder().setProvider( provider ).build( secretPwd.toCharArray() ) );
        final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
                new JcaPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1 )
                        .setProvider( provider ) );
        signatureGenerator.init( PGPSignature.BINARY_DOCUMENT, privateKey );
        final Iterator<?> it = secretKey.getPublicKey().getUserIDs();
        if ( it.hasNext() )
        {
            final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
            spGen.setSignerUserID( false, ( String ) it.next() );
            signatureGenerator.setHashedSubpackets( spGen.generate() );
        }
        signatureGenerator.generateOnePassVersion( false ).encode( compressedOut );
        final PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
        final OutputStream literalOut = literalDataGenerator
                .open( compressedOut, PGPLiteralData.BINARY, "filename", new Date(), new byte[4096] );
        final InputStream in = new ByteArrayInputStream( message );
        final byte[] buf = new byte[4096];
        for ( int len; ( len = in.read( buf ) ) > 0; )
        {
            literalOut.write( buf, 0, len );
            signatureGenerator.update( buf, 0, len );
        }
        in.close();
        literalDataGenerator.close();
        signatureGenerator.generate().encode( compressedOut );
        compressedDataGenerator.close();
        encryptedDataGenerator.close();
        theOut.close();
        return out.toByteArray();
    }
    catch ( Exception e )
    {
        throw new PGPException( "Error in signAndEncrypt", e );
    }
}
 
开发者ID:subutai-io,项目名称:base,代码行数:60,代码来源:PGPEncryptionUtil.java


示例20: generateKeyRingGenerator

import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; //导入依赖的package包/类
private static PGPKeyRingGenerator generateKeyRingGenerator( String id, char[] pass, int s2kcount, int keySize,
                                                             KeyPair keyPair ) throws PGPException
{
    // This object generates individual key-pairs.
    RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();

    // Boilerplate RSA parameters, no need to change anything
    // except for the RSA key-size (2048). You can use whatever
    // key-size makes sense for you -- 4096, etc.
    kpg.init( new RSAKeyGenerationParameters( BigInteger.valueOf( 0x10001 ), new SecureRandom(), keySize, 12 ) );

    // First create the master (signing) key with the generator.
    PGPKeyPair rsakp_sign = new BcPGPKeyPair( PGPPublicKey.RSA_GENERAL, kpg.generateKeyPair(), new Date() );
    // Then an encryption subkey.
    PGPKeyPair rsakp_enc = new BcPGPKeyPair( PGPPublicKey.RSA_GENERAL, kpg.generateKeyPair(), new Date() );

    keyPair.setPrimaryKeyId( Long.toHexString( rsakp_sign.getKeyID() ) );
    keyPair.setPrimaryKeyFingerprint( BytesToHex( rsakp_sign.getPublicKey().getFingerprint() ) );
    keyPair.setSubKeyId( Long.toHexString( rsakp_enc.getKeyID() ) );
    keyPair.setSubKeyFingerprint( BytesToHex( rsakp_enc.getPublicKey().getFingerprint() ) );

    // Add a self-signature on the id
    PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator();

    // Add signed metadata on the signature.
    // 1) Declare its purpose
    signhashgen.setKeyFlags( false, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER );
    // 2) Set preferences for secondary crypto algorithms to use
    //    when sending messages to this key.
    signhashgen.setPreferredSymmetricAlgorithms( false, new int[] {
            SymmetricKeyAlgorithmTags.AES_256, SymmetricKeyAlgorithmTags.AES_192, SymmetricKeyAlgorithmTags.AES_128,
            SymmetricKeyAlgorithmTags.CAST5, SymmetricKeyAlgorithmTags.TRIPLE_DES
    } );
    signhashgen.setPreferredHashAlgorithms( false, new int[] {
            HashAlgorithmTags.SHA256, HashAlgorithmTags.SHA1, HashAlgorithmTags.SHA384, HashAlgorithmTags.SHA512,
            HashAlgorithmTags.SHA224,
    } );
    signhashgen.setPreferredCompressionAlgorithms( false, new int[] {
            CompressionAlgorithmTags.ZLIB, CompressionAlgorithmTags.BZIP2, CompressionAlgorithmTags.ZIP
    } );
    // 3) Request senders add additional checksums to the
    //    message (useful when verifying unsigned messages.)
    signhashgen.setFeature( false, Features.FEATURE_MODIFICATION_DETECTION );

    // Create a signature on the encryption subkey.
    PGPSignatureSubpacketGenerator enchashgen = new PGPSignatureSubpacketGenerator();
    // Add metadata to declare its purpose
    enchashgen.setKeyFlags( false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE );

    // Objects used to encrypt the secret key.
    PGPDigestCalculator sha1Calc = new BcPGPDigestCalculatorProvider().get( HashAlgorithmTags.SHA1 );

    // bcpg 1.48 exposes this API that includes s2kcount. Earlier
    // versions use a default of 0x60.
    PBESecretKeyEncryptor pske =
            ( new BcPBESecretKeyEncryptorBuilder( PGPEncryptedData.CAST5, sha1Calc, s2kcount ) ).build( pass );
    // Finally, create the keyring itself. The constructor
    // takes parameters that allow it to generate the self
    // signature.
    PGPKeyRingGenerator keyRingGen =
            new PGPKeyRingGenerator( PGPSignature.POSITIVE_CERTIFICATION, rsakp_sign, id, sha1Calc,
                    signhashgen.generate(), null,
                    new BcPGPContentSignerBuilder( rsakp_sign.getPublicKey().getAlgorithm(),
                            HashAlgorithmTags.SHA1 ), pske );

    // Add our encryption subkey, together with its signature.
    keyRingGen.addSubKey( rsakp_enc, enchashgen.generate(), null );
    return keyRingGen;
}
 
开发者ID:subutai-io,项目名称:base,代码行数:70,代码来源:PGPEncryptionUtil.java



注:本文中的org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java Service类代码示例发布时间:2022-05-21
下一篇:
Java Music类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap