java.security.Security类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(465)

本文整理了Java中java.security.Security类的一些代码示例,展示了Security类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Security类的具体详情如下:
包路径:java.security.Security
类名称:Security

Security介绍

[英]Security is the central class in the Java Security API. It manages the list of security Provider that have been installed into this runtime environment.
[中]安全性是Java安全API中的核心类。它管理已安装到此运行时环境中的安全提供程序列表。

代码示例

代码示例来源:origin: gocd/gocd

public X509CertificateGenerator() {
  Security.addProvider(new BouncyCastleProvider());
  this.keyStoreManager = new KeyStoreManager();
}

代码示例来源:origin: robovm/robovm

throw new IllegalArgumentException();
Provider impProvider = Security.getProvider(provider);
if (impProvider == null) {
  throw new NoSuchProviderException(provider);
  return getInstance(type, impProvider);
} catch (Exception e) {
  throw new KeyStoreException(e);

代码示例来源:origin: square/okhttp

public static boolean isConscryptPreferred() {
 // mainly to allow tests to run cleanly
 if ("conscrypt".equals(Util.getSystemProperty("okhttp.platform", null))) {
  return true;
 }
 // check if Provider manually installed
 String preferredProvider = Security.getProviders()[0].getName();
 return "Conscrypt".equals(preferredProvider);
}

代码示例来源:origin: floragunncom/search-guard

@Override
  public Object run() {
    if(Security.getProvider("BC") == null) {
      Security.addProvider(new BouncyCastleProvider());
    }
    return null;
  }
});

代码示例来源:origin: apache/storm

@Override
protected TTransportFactory getServerTransportFactory(boolean impersonationAllowed) throws IOException {
  //create an authentication callback handler
  CallbackHandler serverCallbackHandler = new SimpleSaslServerCallbackHandler(impersonationAllowed,
                                        (userName) -> Optional.of("password".toCharArray()));
  if (Security.getProvider(SaslPlainServer.SecurityProvider.SASL_PLAIN_SERVER) == null) {
    Security.addProvider(new SaslPlainServer.SecurityProvider());
  }
  //create a transport factory that will invoke our auth callback for digest
  TSaslServerTransport.Factory factory = new TSaslServerTransport.Factory();
  factory.addServerDefinition(PLAIN, ClientAuthUtils.SERVICE, "localhost", null, serverCallbackHandler);
  LOG.error("SASL PLAIN transport factory will be used.  This is totally insecure.  Please do not use this.");
  return factory;
}

代码示例来源:origin: com.liumapp.signature/sign-helper

public static void main(String[] args) throws Exception {
  KeyStore ks = KeyStore.getInstance("JKS");
  ks.load(new FileInputStream(dataPath + "/liumapp.keystore"), "111111".toCharArray());
  String alias = (String)ks.aliases().nextElement();
  PrivateKey pk = (PrivateKey)ks.getKey(alias, "111111".toCharArray());
  Certificate[] chain = ks.getCertificateChain(alias);
  BouncyCastleProvider provider = new BouncyCastleProvider();
  Security.addProvider(provider);
  FileOutputStream os = new FileOutputStream(tempPath);
  appearance.setImageScale((float) ((1)/img.getWidth()));
  PrivateKeySignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, provider.getName());
  ExternalDigest digest = new BouncyCastleDigest();
  MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, MakeSignature.CryptoStandard.CMS);
  reader.close();
  os.close();
  stamper.close();

代码示例来源:origin: stackoverflow.com

OutputStream os = new FileOutputStream("c:\\temp\\sign\\test.pdf");
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
Security.addProvider(new BouncyCastleProvider());
FileInputStream fis = new FileInputStream("c:\\ssl\\test.pfx");
String password = "myPassword";
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(fis, password.toCharArray());
String alias = ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(alias, password.toCharArray());

代码示例来源:origin: com.liumapp.signature/sign-helper

public static void sign() throws Exception {
    BouncyCastleProvider provider = new BouncyCastleProvider();
    Security.addProvider(provider);
    KeyStore ks = KeyStore.getInstance("PKCS12");
    ks.load(new FileInputStream(cert_path), cert_pwd.toCharArray());
    String alias = (String) ks.aliases().nextElement();
    //		PrivateKey pk = (PrivateKey) ks.getKey(alias, cert_pwd.toCharArray());
    //		KeyStore ks = KeyStore.getInstance("PKCS12");
    //		ks.load(new FileInputStream(cert_path), cert_pwd.toCharArray());
    PkxSign sign = new PkxSign();
    PrivateKey pk = sign.GetPvkformPfx(cert_path, cert_pwd);
    Certificate[] chain = ks.getCertificateChain(alias);

    PdfReader reader = new PdfReader(source_pdf);
    FileOutputStream os = new FileOutputStream(output_pdf);
    PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
    // Creating the appearance
    PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
    appearance.setReason("digtal signature");
    appearance.setLocation("FuZhou");
    appearance.setVisibleSignature(new Rectangle(0, 300, 300, 109), 1, "sig");
    // Creating the signature
//        ExternalSignature pks = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, provider.getName());
//        ExternalDigest digest = new BouncyCastleDigest();
//        MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, CryptoStandard.CMS);
  }

代码示例来源:origin: igniterealtime/Smack

ByteArrayInputStream config = new ByteArrayInputStream(pkcs11Config.getBytes(StringUtils.UTF8));
    Provider p = (Provider) c.newInstance(config);
    Security.addProvider(p);
    ks = KeyStore.getInstance("PKCS11",p);
    pcb = new PasswordCallback("PKCS11 Password: ",false);
    callbackHandler.handle(new Callback[] {pcb});
    ks.load(null,pcb.getPassword());
  ks = KeyStore.getInstance("KeychainStore","Apple");
  ks.load(null,null);
      pcb = new PasswordCallback("Keystore Password: ", false);
      callbackHandler.handle(new Callback[] { pcb });
      ks.load(new FileInputStream(keystorePath), pcb.getPassword());
final SecureRandom secureRandom = new java.security.SecureRandom();
X509TrustManager customTrustManager = config.getCustomX509TrustManager();

代码示例来源:origin: loklak/loklak_server

throw new Exception("Could not find keystore");
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(keystoreFile.getAbsolutePath()), DAO.getConfig("keystore.password", "").toCharArray());
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

代码示例来源:origin: apache/zookeeper

Security.addProvider(new BouncyCastleProvider());
contentSigner = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(rootKeyPair.getPrivate());
rootCertificate = createSelfSignedCertifcate(rootKeyPair);
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, PASSWORD);
trustStore.setCertificateEntry(rootCertificate.getSubjectDN().toString(), rootCertificate);
FileOutputStream outputStream = new FileOutputStream(truststorePath);
trustStore.store(outputStream, PASSWORD);
outputStream.flush();
outputStream.close();
X509Certificate validCertificate = buildEndEntityCert(defaultKeyPair, rootCertificate, rootKeyPair.getPrivate(),
    HOSTNAME, "127.0.0.1", null, null);
writeKeystore(validCertificate, defaultKeyPair, validKeystorePath);

代码示例来源:origin: stackoverflow.com

ks = KeyStore.getInstance(KeyStore.getDefaultType());
  ks.load(null,null);
  ks.store(new FileOutputStream(new File(getFilesDir(),"out.bks")),"password".toCharArray());
} catch (KeyStoreException e) {
  e.printStackTrace();
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);

代码示例来源:origin: stackoverflow.com

String toVerify = "A1005056807CE11EE2B4CE0025305725CFrCN%3DKED,OU%3DI0020266601,OU%3DSAPWebAS,O%3DSAPTrustCommunity,C%3DDE20130611102236";
 String signed = "MIIBUQYJKoZIhvcNAQcCoIIBQjCCAT4CAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3DQEHATGCAR0wggEZAgEBMG8wZDELMAkGA1UEBhMCREUxHDAaBgNVBAoTE1NBUCBUcnVzdCBDb21tdW5pdHkxEzARBgNVBAsTClNBUCBXZWIgQVMxFDASBgNVBAsTC0kwMDIwMjY2NjAxMQwwCgYDVQQDEwNLRUQCByASEgITMlYwCQYFKw4DAhoFAKBdMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTEzMDYxMTA4MjM1MVowIwYJKoZIhvcNAQkEMRYEFGy7jXb/pUqMYdk2dss2Qe6hNroaMAkGByqGSM44BAMELjAsAhRMJ+t5/3RxQAsHKnIoPY4BnO0qCAIUAbKRwWNjOYsewB56zoZqnZwRyWw=";
 byte[] signedByte = Base64.decode(signed);
 Security.addProvider(new BouncyCastleProvider());
 CMSSignedData s = new CMSSignedData(new CMSProcessableByteArray(toVerify.getBytes()), signedByte);
 SignerInformationStore signers = s.getSignerInfos();
 SignerInformation signerInfo = (SignerInformation)signers.getSigners().iterator().next();
 FileInputStream fis = new FileInputStream("c:\\sap.cer");
 CertificateFactory cf = CertificateFactory.getInstance("X.509");
 X509Certificate cert = (X509Certificate)cf.generateCertificates(fis).iterator().next();
 boolean result = signerInfo.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider("SUN").build(cert.getPublicKey())); 
 System.out.println("Verified: "+result);

代码示例来源:origin: stackoverflow.com

Security.addProvider(new BouncyCastleProvider());
keyPairGenerator.initialize(1024, new SecureRandom());
certGen.setPublicKey(keyPair.getPublic());
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
X509Certificate cert = certGen.generate(keyPair.getPrivate(), "BC");
KeyStore clientKeystore = KeyStore.getInstance("PKCS12", "BC");
clientKeystore.load(null, null);
clientKeystore.setKeyEntry("mkey", keyPair.getPrivate(), null, new X509Certificate[] { cert });
clientKeystore.store(new FileOutputStream("admin.pkcs"), "pass".toCharArray());

代码示例来源:origin: stackoverflow.com

KeyStore keystore = KeyStore.getInstance("JKS");
InputStream is = new FileInputStream(PATH_TO_KEYSTORE);
keystore.load(is, KEYSTORE_PASSWORD.toCharArray());
return keystore;
Security.addProvider(new BouncyCastleProvider());
Certificate[] certchain = (Certificate[]) keystore.getCertificateChain(KEY_ALIAS_IN_KEYSTORE);

代码示例来源:origin: stackoverflow.com

Security.addProvider( new org.bouncycastle.jce.provider.BouncyCastleProvider() );
    issuer, serial,
    notBefore, notAfter,
    subject, keyPair.getPublic()
);
org.bouncycastle.cert.X509CertificateHolder certificateHolder = certificateBuilder.build(
    new org.bouncycastle.operator.jcajce.JcaContentSignerBuilder( "GOST3411withECGOST3410" )
        .build( keyPair.getPrivate() )
);
org.bouncycastle.cert.jcajce.JcaX509CertificateConverter certificateConverter = new org.bouncycastle.cert.jcajce.JcaX509CertificateConverter();
X509Certificate certificate = certificateConverter.getCertificate( certificateHolder );
KeyStore keyStore = KeyStore.getInstance( "JKS" );
keyStore.load( null, null ); // initialize new keystore
keyStore.setEntry(
    "alias",
    new KeyStore.PrivateKeyEntry(
        keyPair.getPrivate(),
        new Certificate[] { certificate }
    ),
    new KeyStore.PasswordProtection( "entryPassword".toCharArray() )
);
keyStore.store( new FileOutputStream( "test.jks" ), "keystorePassword".toCharArray() );

代码示例来源:origin: stackoverflow.com

java.security.provider hsm_provider = (java.security.provider)Class.forName("com.ncipher.provider.km.nCipherKM").newInstance();  
java.security.Security.addProvider(hsm_provider);  
myKeyStore = KeyStore.getInstance(KeyStore.getDefaulttype,"nCipherKM");  
myKeyStore .load(new FileInputStream(KeyStorePath),null);

代码示例来源:origin: Pay-Group/best-pay-sdk

Security.addProvider(new BouncyCastleProvider());
SecretKeySpec aesKey = new SecretKeySpec(DigestUtils.md5Hex(key).toLowerCase().getBytes(), ALGORITHM);
Cipher cipher = null;
try {
  cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
} catch (NoSuchAlgorithmException e) {
  e.printStackTrace();
  cipher.init(Cipher.DECRYPT_MODE, aesKey);
} catch (InvalidKeyException e) {
  e.printStackTrace();
  return new String(cipher.doFinal(Base64.getDecoder().decode(data)));
} catch (IllegalBlockSizeException e) {
  e.printStackTrace();

代码示例来源:origin: stackoverflow.com

Security.addProvider(new BouncyCastleProvider());
signature.initSign(keyPair.getPrivate());
signature.update(message.getBytes());
byte [] signatureBytes = signature.sign();
verifier.initVerify(keyPair.getPublic());
verifier.update(message.getBytes());
if (verifier.verify(signatureBytes)) {

代码示例来源:origin: stackoverflow.com

Security.addProvider(new BouncyCastleProvider());
PrivateKey key = (PrivateKey)reader.readObject();        
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(null);
keystore.setCertificateEntry("cert-alias", cert);
keystore.setKeyEntry("key-alias", key, "changeit".toCharArray(), new Certificate[] {cert});

相关文章