本文整理汇总了Java中org.spongycastle.crypto.generators.RSAKeyPairGenerator类的典型用法代码示例。如果您正苦于以下问题:Java RSAKeyPairGenerator类的具体用法?Java RSAKeyPairGenerator怎么用?Java RSAKeyPairGenerator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RSAKeyPairGenerator类属于org.spongycastle.crypto.generators包,在下文中一共展示了RSAKeyPairGenerator类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateKeyRingGenerator
import org.spongycastle.crypto.generators.RSAKeyPairGenerator; //导入依赖的package包/类
public final static PGPKeyRingGenerator generateKeyRingGenerator (String keyId, char[] pass) throws PGPException{
RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
kpg.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x10001), new SecureRandom(), 4096, 12));
PGPKeyPair rsakp_sign = new BcPGPKeyPair(PGPPublicKey.RSA_SIGN, kpg.generateKeyPair(), new Date());
PGPKeyPair rsakp_enc = new BcPGPKeyPair(PGPPublicKey.RSA_ENCRYPT, kpg.generateKeyPair(), new Date());
PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator();
signhashgen.setKeyFlags(false, KeyFlags.SIGN_DATA|KeyFlags.CERTIFY_OTHER|KeyFlags.SHARED);
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});
signhashgen.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);
PGPSignatureSubpacketGenerator enchashgen = new PGPSignatureSubpacketGenerator();
enchashgen.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);
PGPDigestCalculator sha1Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1);
PGPDigestCalculator sha256Calc = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA256);
PBESecretKeyEncryptor pske = (new BcPBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha256Calc, 0xc0)).build(pass);
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator (PGPSignature.POSITIVE_CERTIFICATION, rsakp_sign,
keyId, sha1Calc, signhashgen.generate(), null, new BcPGPContentSignerBuilder(rsakp_sign.getPublicKey().getAlgorithm(),
HashAlgorithmTags.SHA1), pske);
keyRingGen.addSubKey(rsakp_enc, enchashgen.generate(), null);
return keyRingGen;
}
示例2: createKeyRingGenerator
import org.spongycastle.crypto.generators.RSAKeyPairGenerator; //导入依赖的package包/类
/**
* creates and initializes a PGP Key Ring Generator
*
* @param userId
* the user id to use
* @param password
* the password used for the private key
* @param keySize
* the key size used for the keys
* @return the initialized key ring generator or null if something goes wrong
*/
private PGPKeyRingGenerator createKeyRingGenerator(String userId, String password, int keySize) {
LOGGER.trace("createKeyRingGenerator(String, String, int)");
LOGGER.trace("User ID: {}, Password: {}, Key Size: {}", userId, password == null ? "not set" : "********", keySize);
PGPKeyRingGenerator generator = null;
try {
LOGGER.debug("Creating RSA key pair generator");
RSAKeyPairGenerator generator1 = new RSAKeyPairGenerator();
generator1.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x10001), getSecureRandom(), keySize, 12));
LOGGER.debug("Generating Signing Key Pair");
BcPGPKeyPair signingKeyPair = new BcPGPKeyPair(PGPPublicKey.RSA_SIGN, generator1.generateKeyPair(), new Date());
LOGGER.debug("Generating Encyption Key Pair");
BcPGPKeyPair encryptionKeyPair = new BcPGPKeyPair(PGPPublicKey.RSA_ENCRYPT, generator1.generateKeyPair(), new Date());
LOGGER.debug("Generating Signature Key Properties");
PGPSignatureSubpacketGenerator signatureSubpacketGenerator = new PGPSignatureSubpacketGenerator();
signatureSubpacketGenerator.setKeyFlags(false, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER);
signatureSubpacketGenerator.setPreferredSymmetricAlgorithms(false, getPreferredEncryptionAlgorithms());
signatureSubpacketGenerator.setPreferredHashAlgorithms(false, getPreferredHashingAlgorithms());
signatureSubpacketGenerator.setPreferredCompressionAlgorithms(false, getPreferredCompressionAlgorithms());
LOGGER.debug("Generating Encyption Key Properties");
PGPSignatureSubpacketGenerator encryptionSubpacketGenerator = new PGPSignatureSubpacketGenerator();
encryptionSubpacketGenerator.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);
LOGGER.info("Creating PGP Key Ring Generator");
generator = new PGPKeyRingGenerator(PGPPublicKey.RSA_SIGN, signingKeyPair, userId, new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1), signatureSubpacketGenerator.generate(), null, new BcPGPContentSignerBuilder(PGPPublicKey.RSA_SIGN, HashAlgorithmTags.SHA256), new BcPBESecretKeyEncryptorBuilder(getEncryptionAlgorithm()).build(password.toCharArray()));
generator.addSubKey(encryptionKeyPair, encryptionSubpacketGenerator.generate(), null);
} catch (PGPException e) {
LOGGER.error("{}", e.getMessage());
generator = null;
}
return generator;
}
示例3: ProtocolServer
import org.spongycastle.crypto.generators.RSAKeyPairGenerator; //导入依赖的package包/类
public ProtocolServer(Class protocolInterfaceType, Object protocolImpl){
RSAKeyPairGenerator rsaGen = new RSAKeyPairGenerator();
rsaGen.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x1001), new SecureRandom(), 2048, 25));
keyPair = rsaGen.generateKeyPair();
ChannelFactory channelFactory = new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()
);
nettyServer = new NettyServer(new ReflectResponder(
protocolInterfaceType,
protocolImpl), new InetSocketAddress(7001),
channelFactory, new SSLChannelPipelineFactory(keyPair),
null);
}
示例4: generateKeyPair
import org.spongycastle.crypto.generators.RSAKeyPairGenerator; //导入依赖的package包/类
@Override
public AsymmetricCipherKeyPair generateKeyPair() {
// Can't return null as public key is extracted from it
RSAKeyPairGenerator gen = new RSAKeyPairGenerator();
RSAKeyGenerationParameters params = new RSAKeyGenerationParameters(
BigInteger.valueOf(0x10001), getSecureRandom(), 32, 1);
gen.init(params);
return gen.generateKeyPair();
}
示例5: generateKey
import org.spongycastle.crypto.generators.RSAKeyPairGenerator; //导入依赖的package包/类
@Override
public PGPKeyRingGenerator generateKey(String email, char[] password) throws Exception {
RSAKeyPairGenerator rsaKeyPairGenerator=new RSAKeyPairGenerator();
rsaKeyPairGenerator.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x10001),new SecureRandom(),2048,12));
PGPKeyPair keyPair=new BcPGPKeyPair(PGPPublicKey.RSA_SIGN,rsaKeyPairGenerator.generateKeyPair(),new Date());
PGPKeyPair enKeyPair=new BcPGPKeyPair(PGPPublicKey.RSA_ENCRYPT,rsaKeyPairGenerator.generateKeyPair(),new Date());
// 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
});
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);
PBESecretKeyEncryptor pske =
(new BcPBESecretKeyEncryptorBuilder
(PGPEncryptedData.AES_256, sha256Calc, 0xc0))
.build(password);
// Finally, create the keyring itself. The constructor
// takes parameters that allow it to generate the self
// signature.
PGPKeyRingGenerator keyRingGen =
new PGPKeyRingGenerator
(PGPSignature.POSITIVE_CERTIFICATION, keyPair,
email, sha1Calc, signhashgen.generate(), null,
new BcPGPContentSignerBuilder
(keyPair.getPublicKey().getAlgorithm(),
HashAlgorithmTags.SHA1),
pske);
// Add our encryption subkey, together with its signature.
keyRingGen.addSubKey
(enKeyPair, enchashgen.generate(), null);
return keyRingGen;
}
示例6: generateCertificate
import org.spongycastle.crypto.generators.RSAKeyPairGenerator; //导入依赖的package包/类
/**
* Create a self-signed X.509 Certificate
*
* @param dn the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"
*/
private X509Certificate generateCertificate(String dn) throws Exception {
X500Name x500nameIssuer = new X500Name("CN=TestCA,L=Den Haag, C=NL");
X500Name x500nameSubject = new X500Name(dn);
BigInteger serial = new BigInteger(64, new Random());
Date notBefore = new Date();
// Set Expiration Date
Calendar tempCal = Calendar.getInstance();
tempCal.setTime(notBefore);
tempCal.add(Calendar.DATE, 365);
Date notAfter = tempCal.getTime();
// Create Pubkey
RSAKeyPairGenerator keyGen = new RSAKeyPairGenerator();
keyGen.init(new RSAKeyGenerationParameters(new BigInteger("10001", 16), SecureRandom.getInstance("SHA1PRNG"), 1024, 80));
AsymmetricCipherKeyPair keys = keyGen.generateKeyPair();
SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(keys.getPublic());
X509v3CertificateBuilder builder = new X509v3CertificateBuilder(
x500nameIssuer,
serial,
notBefore,
notAfter,
Locale.US,
x500nameSubject,
subPubKeyInfo
);
try {
// Export Private Key Info into Java PrivateKey
BigInteger modulus = ((RSAKeyParameters) keys.getPrivate()).getModulus();
BigInteger exponent = ((RSAKeyParameters) keys.getPrivate()).getExponent();
RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(modulus, exponent);
KeyFactory factory = KeyFactory.getInstance("RSA");
ContentSigner sigGen = new JcaContentSignerBuilder("SHA1withRSA").build(factory.generatePrivate(privateSpec));
X509CertificateHolder holder = builder.build(sigGen);
InputStream is = new ByteArrayInputStream(holder.toASN1Structure().getEncoded());
return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(is);
} catch (OperatorCreationException e) {
e.printStackTrace();
}
throw new Exception("Unable to Create Test X509 Cert");
}
示例7: testOpenPgp
import org.spongycastle.crypto.generators.RSAKeyPairGenerator; //导入依赖的package包/类
/**
* https://github.com/wesabe/grendel/blob/master/src/test/java/com/wesabe/
* grendel/openpgp/tests/KeySetGeneratorTest.java
*
* @throws PGPException
*/
@Test
public void testOpenPgp() throws PGPException {
String id = "[email protected]";
String pass = "hello";
int s2kcount = 0xc1;
// http://bouncycastle-pgp-cookbook.blogspot.tw/2013/01/generating-rsa-keys.html
RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
kpg.init(new RSAKeyGenerationParameters(BigInteger.valueOf(0x10001),
new SecureRandom(), 2048, 12));
BcPGPKeyPair bkpSign = new BcPGPKeyPair(PGPPublicKey.RSA_SIGN,
kpg.generateKeyPair(), new Date());
assertNotNull(bkpSign);
BcPGPKeyPair bkpEncrypt = new BcPGPKeyPair(PGPPublicKey.RSA_ENCRYPT,
kpg.generateKeyPair(), new Date());
assertNotNull(bkpEncrypt);
PGPSignatureSubpacketGenerator signhashgen = new PGPSignatureSubpacketGenerator();
signhashgen.setKeyFlags(false, KeyFlags.SIGN_DATA
| KeyFlags.CERTIFY_OTHER);
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, });
signhashgen.setFeature(false, Features.FEATURE_MODIFICATION_DETECTION);
PGPSignatureSubpacketGenerator enchashgen = new PGPSignatureSubpacketGenerator();
enchashgen.setKeyFlags(false, KeyFlags.ENCRYPT_COMMS
| KeyFlags.ENCRYPT_STORAGE);
PGPDigestCalculator sha1Calc = new BcPGPDigestCalculatorProvider()
.get(HashAlgorithmTags.SHA1);
PGPDigestCalculator sha256Calc = new BcPGPDigestCalculatorProvider()
.get(HashAlgorithmTags.SHA256);
// bcpg 1.48 exposes this API that includes s2kcount. Earlier
// versions use a default of 0x60.
PBESecretKeyEncryptor pske = (new BcPBESecretKeyEncryptorBuilder(
PGPEncryptedData.AES_256, sha256Calc))
.build(pass.toCharArray());
// Finally, create the keyring itself. The constructor
// takes parameters that allow it to generate the self
// signature.
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(
PGPSignature.POSITIVE_CERTIFICATION, bkpSign, id, sha1Calc,
signhashgen.generate(), null, new BcPGPContentSignerBuilder(
bkpSign.getPublicKey().getAlgorithm(),
HashAlgorithmTags.SHA1), pske);
// Add our encryption subkey, together with its signature.
keyRingGen.addSubKey(bkpEncrypt, enchashgen.generate(), null);
assertNotNull(keyRingGen);
}