本文整理汇总了Java中org.spongycastle.x509.X509V3CertificateGenerator.setSerialNumber方法的典型用法代码示例。如果您正苦于以下问题:Java X509V3CertificateGenerator.setSerialNumber方法的具体用法?Java X509V3CertificateGenerator.setSerialNumber怎么用?Java X509V3CertificateGenerator.setSerialNumber使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.spongycastle.x509.X509V3CertificateGenerator
的用法示例。
在下文中一共展示了X509V3CertificateGenerator.setSerialNumber方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createCert
import org.spongycastle.x509.X509V3CertificateGenerator; //导入方法依赖的package包/类
private X509Certificate createCert() throws java.security.GeneralSecurityException {
KeyPairGenerator generator = KeyPairGenerator.getInstance(KEY_PAIR_ALGORITHM);
generator.initialize(ASYMMETRIC_KEY_SIZE);
KeyPair keyPair = generator.generateKeyPair();
//Check Certificate
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
certGen.setSerialNumber(BigInteger.valueOf(1L));
certGen.setSubjectDN(new X500Principal("CN=evs"));
certGen.setIssuerDN(new X500Principal("CN=evs"));
certGen.setPublicKey(keyPair.getPublic());
certGen.setNotBefore(new Date());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, 100);
certGen.setNotAfter(calendar.getTime());
certGen.setSignatureAlgorithm(KEY_PAIR_SIGNING_ALGORITHM);
return certGen.generate(keyPair.getPrivate());
}
示例2: generateCertificate
import org.spongycastle.x509.X509V3CertificateGenerator; //导入方法依赖的package包/类
/**
* Generates a X509Certificate for the given keypair
*
* @param keyPair
* @return
*/
private X509Certificate generateCertificate(KeyPair keyPair) {
Calendar start = new GregorianCalendar();
Calendar end = new GregorianCalendar();
end.add(Calendar.YEAR, 200);
X509V3CertificateGenerator cert = new X509V3CertificateGenerator();
cert.setSerialNumber(BigInteger.ONE); //or generate a random number
cert.setSubjectDN(new X509Principal("CN=localhost")); //see examples to add O,OU etc
cert.setIssuerDN(new X509Principal("CN=localhost")); //same since it is self-signed
cert.setPublicKey(keyPair.getPublic());
cert.setNotBefore(start.getTime());
cert.setNotAfter(end.getTime());
cert.setSignatureAlgorithm("SHA1WithRSAEncryption");
PrivateKey signingKey = keyPair.getPrivate();
X509Certificate certChain = null;
try {
certChain = cert.generate(signingKey, "BC");
} catch (CertificateEncodingException | NoSuchProviderException | NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
ErrorLoggingSingleton log = ErrorLoggingSingleton.getInstance();
log.storeError(ErrorLoggingSingleton.getExceptionStackTraceAsFormattedString(e));
throw new RuntimeException(e);
}
return certChain;
}
示例3: generateCertificate
import org.spongycastle.x509.X509V3CertificateGenerator; //导入方法依赖的package包/类
public static X509Certificate generateCertificate(KeyPair pair)
throws InvalidKeyException, NoSuchProviderException, SignatureException
{
// generate the certificate
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
certGen.setIssuerDN(new X500Principal("CN=Restcomm Android SDK"));
certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));
// TODO: using 1 day for now, need to increase that
certGen.setNotAfter(new Date(System.currentTimeMillis() + 86400000));
certGen.setSubjectDN(new X500Principal("CN=Restcomm Android SDK"));
certGen.setPublicKey(pair.getPublic());
certGen.setSignatureAlgorithm("SHA1withECDSA");
certGen.addExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(false));
certGen.addExtension(X509Extensions.KeyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment));
certGen.addExtension(X509Extensions.ExtendedKeyUsage, true, new ExtendedKeyUsage(KeyPurposeId.id_kp_serverAuth));
certGen.addExtension(X509Extensions.SubjectAlternativeName, false, new GeneralNames(new GeneralName(GeneralName.rfc822Name, "[email protected]")));
// provider is Bouncy Castle
return certGen.generateX509Certificate(pair.getPrivate(), "BC");
}
示例4: createKey
import org.spongycastle.x509.X509V3CertificateGenerator; //导入方法依赖的package包/类
public static KeySet createKey( String keyAlgorithm, int keySize, String keyName,
String certSignatureAlgorithm, int certValidityYears, DistinguishedNameValues distinguishedNameValues)
{
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyAlgorithm);
keyPairGenerator.initialize(keySize);
KeyPair KPair = keyPairGenerator.generateKeyPair();
X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();
X509Principal principal = distinguishedNameValues.getPrincipal();
// generate a postitive serial number
BigInteger serialNumber = BigInteger.valueOf(new SecureRandom().nextInt());
while (serialNumber.compareTo(BigInteger.ZERO) < 0) {
serialNumber = BigInteger.valueOf(new SecureRandom().nextInt());
}
v3CertGen.setSerialNumber(serialNumber);
v3CertGen.setIssuerDN( principal);
v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60L * 60L * 24L * 30L));
v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60L * 60L * 24L * 366L * (long)certValidityYears)));
v3CertGen.setSubjectDN(principal);
v3CertGen.setPublicKey(KPair.getPublic());
v3CertGen.setSignatureAlgorithm(certSignatureAlgorithm);
X509Certificate PKCertificate = v3CertGen.generate(KPair.getPrivate(),"BC");
KeySet keySet = new KeySet();
keySet.setName(keyName);
keySet.setPrivateKey(KPair.getPrivate());
keySet.setPublicKey(PKCertificate);
return keySet;
} catch (Exception x) {
throw new RuntimeException(x.getMessage(), x);
}
}
示例5: createKey
import org.spongycastle.x509.X509V3CertificateGenerator; //导入方法依赖的package包/类
public static KeySet createKey(String keyAlgorithm, int keySize, String keyName,
String certSignatureAlgorithm, int certValidityYears, DistinguishedNameValues distinguishedNameValues) {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyAlgorithm);
keyPairGenerator.initialize(keySize);
KeyPair KPair = keyPairGenerator.generateKeyPair();
X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();
X509Principal principal = distinguishedNameValues.getPrincipal();
// generate a postitive serial number
BigInteger serialNumber = BigInteger.valueOf(new SecureRandom().nextInt());
while (serialNumber.compareTo(BigInteger.ZERO) < 0) {
serialNumber = BigInteger.valueOf(new SecureRandom().nextInt());
}
v3CertGen.setSerialNumber(serialNumber);
v3CertGen.setIssuerDN(principal);
v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60L * 60L * 24L * 30L));
v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60L * 60L * 24L * 366L * (long) certValidityYears)));
v3CertGen.setSubjectDN(principal);
v3CertGen.setPublicKey(KPair.getPublic());
v3CertGen.setSignatureAlgorithm(certSignatureAlgorithm);
X509Certificate PKCertificate = v3CertGen.generate(KPair.getPrivate(), "BC");
KeySet keySet = new KeySet();
keySet.setName(keyName);
keySet.setPrivateKey(KPair.getPrivate());
keySet.setPublicKey(PKCertificate);
return keySet;
} catch (Exception x) {
throw new RuntimeException(x.getMessage(), x);
}
}
示例6: generateV3Cert
import org.spongycastle.x509.X509V3CertificateGenerator; //导入方法依赖的package包/类
public static X509Certificate generateV3Cert(KeyPair pair) {
X509Certificate cert = null;
try {
X509V3CertificateGenerator gen = new X509V3CertificateGenerator();
gen.setPublicKey(pair.getPublic());
gen.setSerialNumber(new BigInteger(Long.toString(System
.currentTimeMillis() / 1000)));
Hashtable attrs = new Hashtable();
Vector vOrder = new Vector();
attrs.put(X509Principal.CN, "[email protected]");
vOrder.add(0, X509Principal.CN);
attrs.put(X509Principal.OU, "self");
vOrder.add(0, X509Principal.OU);
attrs.put(X509Principal.O, "eBay, Inc.");
vOrder.add(0, X509Principal.O);
attrs.put(X509Principal.L, "San Jose");
vOrder.add(0, X509Principal.L);
attrs.put(X509Principal.ST, "California");
vOrder.add(0, X509Principal.ST);
attrs.put(X509Principal.C, "USA");
vOrder.add(0, X509Principal.C);
gen.setIssuerDN(new X509Principal(vOrder, attrs));
gen.setSubjectDN(new X509Principal(vOrder, attrs));
gen.setNotBefore(new Date(System.currentTimeMillis()));
gen.setNotAfter(new Date(System.currentTimeMillis()
+ VALIDITY_PERIOD));
gen.setSignatureAlgorithm("SHA1WithECDSA");
cert = gen.generate(pair.getPrivate(), "BC");
} catch (Exception e) {
System.out.println("Unable to generate a X509Certificate." + e);
}
return cert;
}
示例7: testStoreKey
import org.spongycastle.x509.X509V3CertificateGenerator; //导入方法依赖的package包/类
/**
* Test method for the initialization of the KeyStoreController
*
* @throws Exception
*/
public void testStoreKey() throws Exception {
SimpleContainer container = new SimpleContainer();
container.register(ContainerService.KEY_CONTEXT,
new ContainerService.ContextComponent(getInstrumentation().getTargetContext()));
KeyStoreController controller = new KeyStoreController();
container.register(KEY, controller);
KeyPairGenerator generator;
generator = KeyPairGenerator.getInstance(KEY_PAIR_ALGORITHM);
generator.initialize(ASYMMETRIC_KEY_SIZE);
KeyPair keyPair = generator.generateKeyPair();
//Check Certificate
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
certGen.setSerialNumber(BigInteger.valueOf(1L));
certGen.setSubjectDN(new X500Principal("CN=evs"));
certGen.setIssuerDN(new X500Principal("CN=evs"));
certGen.setPublicKey(keyPair.getPublic());
certGen.setNotBefore(new Date());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, 100);
certGen.setNotAfter(calendar.getTime());
certGen.setSignatureAlgorithm(KEY_PAIR_SIGNING_ALGORITHM);
X509Certificate cert = certGen.generate(keyPair.getPrivate());
controller.saveCertificate(cert, "TestAlias");
container.unregister(KEY);
controller = new KeyStoreController();
container.register(KEY, controller);
assertTrue(Arrays.equals(controller.getCertificate("TestAlias").getEncoded(), cert.getEncoded()));
}
示例8: generateAndStoreOwnKeyPair
import org.spongycastle.x509.X509V3CertificateGenerator; //导入方法依赖的package包/类
/**
* Generates a KeyPair suited for asymmetric encryption.
*/
private void generateAndStoreOwnKeyPair() throws NoSuchAlgorithmException, KeyStoreException,
CertificateException, SignatureException, InvalidKeyException {
String keyPairAlgorithm;
if (ASYMMETRIC_KEY_ALGORITHM.startsWith("EC") && ASYMMETRIC_KEY_ALGORITHM.length() > 2) {
//Hardcoded Algorithms
//see org.spongycastle.jcajce.provider.asymmetric.EC.Mappings.configure(ConfigurableProvider), line 52:
// "KeyPairGenerator.ECIES" -> "KeyPairGeneratorSpi$ECDH"
keyPairAlgorithm = "EC";
Log.w(KeyStoreController.class.getSimpleName(), "Using 'EC' instead of '" + keyPairAlgorithm
+ "' to circumvent wrong BouncyCastle mappings");
} else {
keyPairAlgorithm = ASYMMETRIC_KEY_ALGORITHM;
}
KeyPairGenerator generator;
generator = KeyPairGenerator.getInstance(keyPairAlgorithm);
generator.initialize(ASYMMETRIC_KEY_SIZE);
KeyPair keyPair = generator.generateKeyPair();
//Check Certificate
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
certGen.setSerialNumber(BigInteger.valueOf(1L));
certGen.setSubjectDN(new X500Principal("CN=evs"));
certGen.setIssuerDN(new X500Principal("CN=evs"));
certGen.setPublicKey(keyPair.getPublic());
certGen.setNotBefore(new Date());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, 100);
certGen.setNotAfter(calendar.getTime());
certGen.setSignatureAlgorithm(ASYMMETRIC_SIGNING_ALGORITHM);
X509Certificate cert = certGen.generate(keyPair.getPrivate());
char[] keyPairPassword = getKeyPairPassword();
keyStore.setEntry(LOCAL_PRIVATE_KEY_ALIAS,
new KeyStore.PrivateKeyEntry(keyPair.getPrivate(), new X509Certificate[]{cert}),
new KeyStore.PasswordProtection(keyPairPassword));
Arrays.fill(keyPairPassword, (char) 0);
try (FileOutputStream fileOutputStream = new FileOutputStream(keyStoreFile)) {
keyStore.store(fileOutputStream, getKeystorePassword());
} catch (IOException ex) {
throw new KeyStoreException(ex);
}
}
示例9: doInBackground
import org.spongycastle.x509.X509V3CertificateGenerator; //导入方法依赖的package包/类
@Override
protected Void doInBackground(Void... arg0) {
Log.d("Cert Gen: ", "begin to generate");
try {
// gen the RSA keypair
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048, new SecureRandom());
KeyPair KPair = keyPairGenerator.generateKeyPair();
// generate Certificate
X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();
BigInteger serial = BigInteger.valueOf(new SecureRandom().nextInt());
String devicename = sharedPrefs.getString("device_name", "Device");
String deviceuuid = sharedPrefs.getString("uuid", "000000001111111");
v3CertGen.setSerialNumber(serial.abs());
v3CertGen.setIssuerDN(new X509Principal("CN=" + deviceuuid+"/" +devicename + ", OU=None, O=None L=None, C=None"));
v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));
v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365*10)));
v3CertGen.setSubjectDN(new X509Principal("CN=" + deviceuuid+"/" +devicename + ", OU=None, O=None L=None, C=None"));
v3CertGen.setPublicKey(KPair.getPublic());
v3CertGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
X509Certificate PKCertificate = v3CertGen.generate(KPair.getPrivate());
// create keystore
InputStream keyStoreStream = getResources().openRawResource(R.raw.defaultkeystore);
KeyStore MyKeyStore = KeyStore.getInstance("BKS");
MyKeyStore.load(keyStoreStream, "android".toCharArray());
Certificate[] certchain = new Certificate[1];
certchain[0] = PKCertificate;
PrivateKey privkey = KPair.getPrivate();
MyKeyStore.setKeyEntry("mykeypair",privkey, "passwd".toCharArray(), certchain);
// write new Keystore
OutputStream output = openFileOutput("devicekeystore.bks", Context.MODE_PRIVATE);
MyKeyStore.store(output, "android".toCharArray());
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}