本文整理汇总了Java中org.spongycastle.jce.X509Principal类的典型用法代码示例。如果您正苦于以下问题:Java X509Principal类的具体用法?Java X509Principal怎么用?Java X509Principal使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
X509Principal类属于org.spongycastle.jce包,在下文中一共展示了X509Principal类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateCertificate
import org.spongycastle.jce.X509Principal; //导入依赖的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;
}
示例2: createKey
import org.spongycastle.jce.X509Principal; //导入依赖的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);
}
}
示例3: getPrincipal
import org.spongycastle.jce.X509Principal; //导入依赖的package包/类
public X509Principal getPrincipal() {
Vector<ASN1ObjectIdentifier> oids = new Vector<ASN1ObjectIdentifier>();
Vector<String> values = new Vector<String>();
for (Map.Entry<ASN1ObjectIdentifier,String> entry : entrySet()) {
if (entry.getValue() != null && !entry.getValue().equals("")) {
oids.add( entry.getKey());
values.add( entry.getValue());
}
}
return new X509Principal(oids,values);
}
示例4: createKey
import org.spongycastle.jce.X509Principal; //导入依赖的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: getPrincipal
import org.spongycastle.jce.X509Principal; //导入依赖的package包/类
public X509Principal getPrincipal() {
Vector<ASN1ObjectIdentifier> oids = new Vector<ASN1ObjectIdentifier>();
Vector<String> values = new Vector<String>();
for (Entry<ASN1ObjectIdentifier,String> entry : entrySet()) {
if (entry.getValue() != null && !entry.getValue().equals("")) {
oids.add( entry.getKey());
values.add( entry.getValue());
}
}
return new X509Principal(oids,values);
}
示例6: generateV3Cert
import org.spongycastle.jce.X509Principal; //导入依赖的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: doInBackground
import org.spongycastle.jce.X509Principal; //导入依赖的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;
}