本文整理汇总了Java中java.security.cert.Certificate.getPublicKey方法的典型用法代码示例。如果您正苦于以下问题:Java Certificate.getPublicKey方法的具体用法?Java Certificate.getPublicKey怎么用?Java Certificate.getPublicKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.cert.Certificate
的用法示例。
在下文中一共展示了Certificate.getPublicKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import java.security.cert.Certificate; //导入方法依赖的package包/类
private static boolean test(Certificate target, Certificate signer,
String title, boolean expected) throws Exception {
System.out.print("Checking " + title + ": expected: " +
(expected ? " verified" : "NOT verified"));
boolean actual;
try {
PublicKey pubKey = signer.getPublicKey();
target.verify(pubKey);
actual = true;
} catch (SignatureException se) {
actual = false;
}
System.out.println(", actual: " +
(actual ? " verified" : "NOT verified"));
return actual == expected;
}
示例2: getKerplappKeypair
import java.security.cert.Certificate; //导入方法依赖的package包/类
private KeyPair getKerplappKeypair() throws KeyStoreException, UnrecoverableKeyException,
NoSuchAlgorithmException {
/*
* You can't store a keypair without an associated certificate chain so,
* we'll use the INDEX_CERT_ALIAS as the de-facto keypair/certificate
* chain. This cert/key is initialized when the KerplappKeyStore is
* constructed for the first time and should *always* be present.
*/
Key key = keyStore.getKey(INDEX_CERT_ALIAS, "".toCharArray());
if (key instanceof PrivateKey) {
Certificate cert = keyStore.getCertificate(INDEX_CERT_ALIAS);
PublicKey publicKey = cert.getPublicKey();
return new KeyPair(publicKey, (PrivateKey) key);
}
return null;
}
示例3: getPublicKey
import java.security.cert.Certificate; //导入方法依赖的package包/类
public PublicKey getPublicKey(String keyName) throws CryptoException
{
PublicKey publicKey = null;
try {
KeyStore mKeyStore = KeyStore.getInstance(REST_AUTH_KEYSTORE_NAME);
mKeyStore.load(null);
Certificate certificate = mKeyStore.getCertificate(keyName);
if (certificate != null)
publicKey = certificate.getPublicKey();
} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) {
throw new CryptoException(e.getMessage());
}
return publicKey;
}
示例4: getEncodedPublicKey
import java.security.cert.Certificate; //导入方法依赖的package包/类
public String getEncodedPublicKey(String keyName) throws CryptoException
{
PublicKey publicKey = null;
try {
KeyStore mKeyStore = KeyStore.getInstance(REST_AUTH_KEYSTORE_NAME);
mKeyStore.load(null);
Certificate certificate = mKeyStore.getCertificate(keyName);
if (certificate != null)
publicKey = certificate.getPublicKey();
} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) {
throw new CryptoException(e.getMessage());
}
if (publicKey != null) {
return Base64.encodeToString(publicKey.getEncoded(), Base64.NO_WRAP);
}
return null;
}
示例5: loadPublicKey
import java.security.cert.Certificate; //导入方法依赖的package包/类
/**
* Load a public key
*
* @param keystore
* @param publicKeyAlias
* @return
*/
public static PublicKey loadPublicKey( KeyStore keystore, String publicKeyAlias ) {
Certificate certificate;
try {
certificate = keystore.getCertificate(publicKeyAlias);
} catch (KeyStoreException e) {
throw new RuntimeException("Error loading public key for alias '" + publicKeyAlias + "'", e);
}
if (certificate == null) {
throw new RuntimeException("Error loading public key for alias '" + publicKeyAlias
+ "': Given alias does not exist or does not contain a certificate.");
}
if (log.isDebugEnabled()) {
log.debug("Loaded public key for alias '" + publicKeyAlias + "'");
}
return certificate.getPublicKey();
}
示例6: getKeyPair
import java.security.cert.Certificate; //导入方法依赖的package包/类
private KeyPair getKeyPair() throws
KeyStoreException, IOException,
NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
FileInputStream is = new FileInputStream("mykeys.jks");
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(is, "mypass".toCharArray());
String alias = "mykeys";
Key key = keystore.getKey(alias, "mypass".toCharArray());
if (key instanceof PrivateKey) {
// Get certificate of public key
Certificate cert = keystore.getCertificate(alias);
// Get public key
PublicKey publicKey = cert.getPublicKey();
// Return a key pair
return new KeyPair(publicKey, (PrivateKey) key);
} else throw new UnrecoverableKeyException();
}
开发者ID:tinmegali,项目名称:Oauth2-Stateless-Authentication-with-Spring-and-JWT-Token,代码行数:23,代码来源:SecretKeyProvider.java
示例7: getPublicKeyByText
import java.security.cert.Certificate; //导入方法依赖的package包/类
/**
* 根据公钥Cer文本串读取公钥
*
* @param pubKeyText
* @return
*/
public static PublicKey getPublicKeyByText(String pubKeyText) {
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance(BaofooRsaConst.KEY_X509);
BufferedReader br = new BufferedReader(new StringReader(pubKeyText));
String line = null;
StringBuilder keyBuffer = new StringBuilder();
while ((line = br.readLine()) != null) {
if (!line.startsWith("-")) {
keyBuffer.append(line);
}
}
Certificate certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(keyBuffer.toString())));
return certificate.getPublicKey();
} catch (Exception e) {
// log.error("解析公钥内容失败:", e);
}
return null;
}
示例8: getSignPublicKey
import java.security.cert.Certificate; //导入方法依赖的package包/类
/**
* 获取签名证书公钥对象
*
* @return
*/
public PublicKey getSignPublicKey() {
try {
Enumeration<String> aliasenum = keyStore.aliases();
String keyAlias = null;
if (aliasenum.hasMoreElements()) // we are readin just one
// certificate.
{
keyAlias = (String) aliasenum.nextElement();
}
Certificate cert = keyStore.getCertificate(keyAlias);
PublicKey pubkey = cert.getPublicKey();
return pubkey;
} catch (Exception e) {
LogUtil.writeErrorLog(e.toString());
return null;
}
}
示例9: getCert
import java.security.cert.Certificate; //导入方法依赖的package包/类
public static Pair<Certificate,KeyPair> getCert(String keystore, String alias, String password) {
logger.info("Loading cert from {} with alias {}",keystore,alias);
KeyStore ks = null;
try {
ks = KeyStore.getInstance("JKS");
File fl = new File(keystore);
FileInputStream stream = new FileInputStream(fl);
ks.load(stream, password.toCharArray());
final Key key = ks.getKey(alias, password.toCharArray());
Certificate cert = ks.getCertificate(alias);
KeyPair kp = new KeyPair(cert.getPublicKey(), (PrivateKey) key);
return new Pair<>(ks.getCertificate(alias), kp);
} catch (Exception e) {
logger.error("Error loading certificate: ",e);
throw new RuntimeCryptoException("Problem loading certificate");
}
}
示例10: keyStoreSelect
import java.security.cert.Certificate; //导入方法依赖的package包/类
/**
* Searches the specified keystore for a certificate that matches the
* criteria specified in the CertSelector.
*
* @return a KeySelectorResult containing the cert's public key if there
* is a match; otherwise null
*/
private KeySelectorResult keyStoreSelect(CertSelector cs)
throws KeyStoreException {
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
Certificate cert = ks.getCertificate(alias);
if (cert != null && cs.match(cert)) {
return new SimpleKeySelectorResult(cert.getPublicKey());
}
}
return null;
}
示例11: execute0
import java.security.cert.Certificate; //导入方法依赖的package包/类
@Override
protected Object execute0() throws Exception {
KeyStore ks = KeyStore.getInstance("PKCS11", XiSecurityConstants.PROVIDER_NAME_XIPKI);
ks.load(null, null);
if (verbose.booleanValue()) {
println("available aliases:");
Enumeration<?> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias2 = (String) aliases.nextElement();
println(" " + alias2);
}
}
String alias = getAlias();
println("alias: " + alias);
PrivateKey key = (PrivateKey) ks.getKey(alias, null);
if (key == null) {
println("could not find key with alias '" + alias + "'");
return null;
}
Certificate cert = ks.getCertificate(alias);
if (cert == null) {
println("could not find certificate to verify signature");
return null;
}
PublicKey pubKey = cert.getPublicKey();
String sigAlgo = "SM3withSM2";
println("signature algorithm: " + sigAlgo);
Signature sig = Signature.getInstance(sigAlgo, XiSecurityConstants.PROVIDER_NAME_XIPKI);
if (StringUtil.isNotBlank(ida)) {
sig.setParameter(new XiSM2ParameterSpec(ida.getBytes()));
}
sig.initSign(key);
byte[] data = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
sig.update(data);
byte[] signature = sig.sign(); // CHECKSTYLE:SKIP
println("signature created successfully");
Signature ver = Signature.getInstance(sigAlgo, "BC");
if (StringUtil.isNotBlank(ida)) {
ver.setParameter(new SM2ParameterSpec(ida.getBytes()));
}
ver.initVerify(pubKey);
ver.update(data);
boolean valid = ver.verify(signature);
println("signature valid: " + valid);
return null;
}
示例12: execute0
import java.security.cert.Certificate; //导入方法依赖的package包/类
@Override
protected Object execute0() throws Exception {
KeyStore ks = KeyStore.getInstance("PKCS11", XiSecurityConstants.PROVIDER_NAME_XIPKI);
ks.load(null, null);
if (verbose.booleanValue()) {
println("available aliases:");
Enumeration<?> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias2 = (String) aliases.nextElement();
println(" " + alias2);
}
}
String alias = getAlias();
println("alias: " + alias);
PrivateKey key = (PrivateKey) ks.getKey(alias, null);
if (key == null) {
println("could not find key with alias '" + alias + "'");
return null;
}
Certificate cert = ks.getCertificate(alias);
if (cert == null) {
println("could not find certificate to verify signature");
return null;
}
PublicKey pubKey = cert.getPublicKey();
String sigAlgo = getSignatureAlgo(pubKey);
println("signature algorithm: " + sigAlgo);
Signature sig = Signature.getInstance(sigAlgo, XiSecurityConstants.PROVIDER_NAME_XIPKI);
sig.initSign(key);
byte[] data = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
sig.update(data);
byte[] signature = sig.sign(); // CHECKSTYLE:SKIP
println("signature created successfully");
Signature ver = Signature.getInstance(sigAlgo, "BC");
ver.initVerify(pubKey);
ver.update(data);
boolean valid = ver.verify(signature);
println("signature valid: " + valid);
return null;
}
示例13: getPublicKeyAlias
import java.security.cert.Certificate; //导入方法依赖的package包/类
/**
* Retrieve the public key mapped to a particular name.
* If the key has expired, a KeyException is thrown.
*/
PublicKey getPublicKeyAlias(String name) throws KeyStoreException {
if (keyStore == null) {
return null;
}
Certificate cert = keyStore.getCertificate(name);
if (cert == null) {
return null;
}
PublicKey pubKey = cert.getPublicKey();
return pubKey;
}
示例14: establishCertChain
import java.security.cert.Certificate; //导入方法依赖的package包/类
/**
* Establishes a certificate chain (using trusted certificates in the
* keystore), starting with the user certificate
* and ending at a self-signed certificate found in the keystore.
*
* @param userCert the user certificate of the alias
* @param certToVerify the single certificate provided in the reply
*/
private Certificate[] establishCertChain(Certificate userCert,
Certificate certToVerify)
throws Exception
{
if (userCert != null) {
// Make sure that the public key of the certificate reply matches
// the original public key in the keystore
PublicKey origPubKey = userCert.getPublicKey();
PublicKey replyPubKey = certToVerify.getPublicKey();
if (!origPubKey.equals(replyPubKey)) {
throw new Exception(rb.getString
("Public.keys.in.reply.and.keystore.don.t.match"));
}
// If the two certs are identical, we're done: no need to import
// anything
if (certToVerify.equals(userCert)) {
throw new Exception(rb.getString
("Certificate.reply.and.certificate.in.keystore.are.identical"));
}
}
// Build a hash table of all certificates in the keystore.
// Use the subject distinguished name as the key into the hash table.
// All certificates associated with the same subject distinguished
// name are stored in the same hash table entry as a vector.
Hashtable<Principal, Vector<Certificate>> certs = null;
if (keyStore.size() > 0) {
certs = new Hashtable<Principal, Vector<Certificate>>(11);
keystorecerts2Hashtable(keyStore, certs);
}
if (trustcacerts) {
if (caks!=null && caks.size()>0) {
if (certs == null) {
certs = new Hashtable<Principal, Vector<Certificate>>(11);
}
keystorecerts2Hashtable(caks, certs);
}
}
// start building chain
Vector<Certificate> chain = new Vector<>(2);
if (buildChain((X509Certificate)certToVerify, chain, certs)) {
Certificate[] newChain = new Certificate[chain.size()];
// buildChain() returns chain with self-signed root-cert first and
// user-cert last, so we need to invert the chain before we store
// it
int j=0;
for (int i=chain.size()-1; i>=0; i--) {
newChain[j] = chain.elementAt(i);
j++;
}
return newChain;
} else {
throw new Exception
(rb.getString("Failed.to.establish.chain.from.reply"));
}
}
示例15: getPublicKey
import java.security.cert.Certificate; //导入方法依赖的package包/类
/**
* Fetches the public key matching the given alias from the defined key
* store.
*
* @param path absolute path of the trust store file
* @param password trust store password
* @param publicKeyAlias alias of the public key in the trust store
* @return public key with the given alias
*/
public static PublicKey getPublicKey(String path, String password, String publicKeyAlias) {
try (FileInputStream fis = new java.io.FileInputStream(path)) {
KeyStore keyStore = KeyStore.getInstance("jks");
keyStore.load(fis, password.toCharArray());
Certificate cert;
cert = keyStore.getCertificate(publicKeyAlias);
return cert.getPublicKey();
} catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException ex) {
LOGGER.error(ex.getMessage(), ex);
throw new XRd4JRuntimeException(ex.getMessage());
}
}