Example usage for java.security Key getAlgorithm

List of usage examples for java.security Key getAlgorithm

Introduction

In this page you can find the example usage for java.security Key getAlgorithm.

Prototype

public String getAlgorithm();

Source Link

Document

Returns the standard algorithm name for this key.

Usage

From source file:org.forgerock.openidm.security.impl.SecurityResourceProvider.java

/**
 * Returns a JsonValue map representing key
 * //from   ww w.  j av  a  2  s . com
 * @param key  The key
 * @return a JsonValue map representing the key
 * @throws Exception
 */
protected Map<String, Object> getKeyMap(Key key) throws Exception {
    Map<String, Object> keyMap = new HashMap<>();
    keyMap.put("algorithm", key.getAlgorithm());
    keyMap.put("format", key.getFormat());
    keyMap.put("encoded", toPem(key));
    return keyMap;
}

From source file:org.forgerock.openidm.security.impl.SecurityResourceProvider.java

/**
 * Returns a JsonValue map representing key
 *
 * @param key  The key//w  w w  . ja va2 s  .  com
 * @return a JsonValue map representing the key
 * @throws Exception
 */
protected Map<String, Object> getSecretKeyMap(Key key) throws Exception {
    Map<String, Object> keyMap = new HashMap<>();
    keyMap.put("algorithm", key.getAlgorithm());
    keyMap.put("format", key.getFormat());
    keyMap.put("encoded", Base64.encode(key.getEncoded()));
    return keyMap;
}

From source file:org.javaweb.utils.RSAUtils.java

/**
 * RSA???//from   w w w . ja va2s  .c om
 *
 * @param data ?
 * @param key  ?
 * @return
 * @throws Exception
 */
public static String sign(byte[] data, Key key) throws Exception {
    byte[] keyBytes = key.getEncoded();
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
    PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);

    signature.initSign(privateK);
    signature.update(data);

    return Base64.encodeBase64String(signature.sign());
}

From source file:org.javaweb.utils.RSAUtils.java

/**
 * RSA???//ww w.jav a 2  s  .  co  m
 *
 * @param data ?
 * @param key  
 * @param sign ??Base64
 * @return
 * @throws Exception
 */
public static boolean verify(byte[] data, Key key, String sign) throws Exception {
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key.getEncoded());
    KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
    PublicKey publicK = keyFactory.generatePublic(keySpec);
    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initVerify(publicK);
    signature.update(data);

    return signature.verify(Base64.decodeBase64(sign));
}

From source file:org.lightjason.agentspeak.action.buildin.crypto.CDecrypt.java

@Override
public final IFuzzyValue<Boolean> execute(final IContext p_context, final boolean p_parallel,
        final List<ITerm> p_argument, final List<ITerm> p_return, final List<ITerm> p_annotation) {
    final Key l_key = p_argument.get(0).raw();
    final EAlgorithm l_algorithm = EAlgorithm.from(l_key.getAlgorithm());

    return CFuzzyValue.from(p_argument.subList(1, p_argument.size()).stream()
            .map(i -> Base64.getDecoder().decode(i.<String>raw())).allMatch(i -> {
                try {
                    p_return.add(CRawTerm.from(
                            SerializationUtils.deserialize(l_algorithm.getDecryptCipher(l_key).doFinal(i))));
                    return true;
                } catch (final NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException
                        | BadPaddingException | IllegalBlockSizeException l_exception) {
                    return false;
                }/*from w  w  w  .j a  v a2  s. c  o m*/
            }));
}

From source file:org.lightjason.agentspeak.action.buildin.crypto.CEncrypt.java

@Override
public final IFuzzyValue<Boolean> execute(final IContext p_context, final boolean p_parallel,
        final List<ITerm> p_argument, final List<ITerm> p_return, final List<ITerm> p_annotation) {
    final Key l_key = p_argument.get(0).raw();
    final EAlgorithm l_algorithm = EAlgorithm.from(l_key.getAlgorithm());

    return CFuzzyValue.from(p_argument.subList(1, p_argument.size()).stream()
            .map(i -> SerializationUtils.serialize(i.raw())).allMatch(i -> {
                try {
                    p_return.add(CRawTerm.from(Base64.getEncoder()
                            .encodeToString(l_algorithm.getEncryptCipher(l_key).doFinal(i))));
                    return true;
                } catch (final NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException
                        | BadPaddingException | IllegalBlockSizeException l_exception) {
                    return false;
                }//from   ww  w.  j  av  a  2s .  co m
            }));
}

From source file:org.lightjason.agentspeak.action.builtin.crypto.CDecrypt.java

@Nonnull
@Override//from  w ww.  j a  v a  2  s  .com
public final IFuzzyValue<Boolean> execute(final boolean p_parallel, @Nonnull final IContext p_context,
        @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return) {
    final Key l_key = p_argument.get(0).raw();
    final EAlgorithm l_algorithm;
    try {
        l_algorithm = EAlgorithm.from(l_key.getAlgorithm());
    } catch (final IllegalArgumentException l_exception) {
        return CFuzzyValue.from(false);
    }

    return CFuzzyValue.from(CCommon.flatten(p_argument.stream().skip(1)).map(ITerm::<String>raw)
            .allMatch(i -> decrypt(l_algorithm, l_key, i, p_return)));
}

From source file:org.lightjason.agentspeak.action.builtin.crypto.CEncrypt.java

@Nonnull
@Override//  w w w .  ja  va 2  s .  c  o m
public final IFuzzyValue<Boolean> execute(final boolean p_parallel, @Nonnull final IContext p_context,
        @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return) {
    final Key l_key = p_argument.get(0).raw();
    final EAlgorithm l_algorithm;
    try {
        l_algorithm = EAlgorithm.from(l_key.getAlgorithm());
    } catch (final IllegalArgumentException l_exception) {
        return CFuzzyValue.from(false);
    }

    return CFuzzyValue.from(CCommon.flatten(p_argument.stream().skip(1)).map(ITerm::<Serializable>raw)
            .allMatch(i -> encrypt(l_algorithm, l_key, i, p_return)));
}

From source file:org.lockss.protocol.BlockingStreamComm.java

private void logKeyStore(KeyStore ks, char[] privateKeyPassWord) {
    log.debug3("start of key store");
    try {/*from ww w .j  av  a  2s  .c om*/
        for (Enumeration en = ks.aliases(); en.hasMoreElements();) {
            String alias = (String) en.nextElement();
            log.debug3("Next alias " + alias);
            if (ks.isCertificateEntry(alias)) {
                log.debug3("About to Certificate");
                java.security.cert.Certificate cert = ks.getCertificate(alias);
                if (cert == null) {
                    log.debug3(alias + " null cert chain");
                } else {
                    log.debug3("Cert for " + alias + " is " + cert.toString());
                }
            } else if (ks.isKeyEntry(alias)) {
                log.debug3("About to getKey");
                Key privateKey = ks.getKey(alias, privateKeyPassWord);
                log.debug3(alias + " key " + privateKey.getAlgorithm() + "/" + privateKey.getFormat());
            } else {
                log.debug3(alias + " neither key nor cert");
            }
        }
        log.debug3("end of key store");
    } catch (Exception ex) {
        log.error("logKeyStore() threw " + ex);
    }
}

From source file:org.lockss.util.KeyStoreUtil.java

private static void initializeKeyStore(KeyStore keyStore, Configuration config)
        throws CertificateException, IOException, InvalidKeyException, KeyStoreException,
        NoSuchAlgorithmException, NoSuchProviderException, SignatureException, UnrecoverableKeyException {
    String keyAlias = config.get(PROP_KEY_ALIAS, DEFAULT_KEY_ALIAS);
    String certAlias = config.get(PROP_CERT_ALIAS, DEFAULT_CERT_ALIAS);
    String keyAlgName = config.get(PROP_KEY_ALGORITHM, DEFAULT_KEY_ALGORITHM);
    String sigAlgName = config.get(PROP_SIG_ALGORITHM, DEFAULT_SIG_ALGORITHM);
    String keyStorePassword = config.get(PROP_KEYSTORE_PASSWORD);
    String keyPassword = config.get(PROP_KEY_PASSWORD);
    int keyBits = config.getInt(PROP_KEY_BITS, DEFAULT_KEY_BITS);
    long expireIn = config.getTimeInterval(PROP_EXPIRE_IN, DEFAULT_EXPIRE_IN);
    String x500String = config.get(PROP_X500_NAME, DEFAULT_X500_NAME);

    CertAndKeyGen keypair = new CertAndKeyGen(keyAlgName, sigAlgName);
    keypair.generate(keyBits);//from w  ww .  ja v  a2  s.  c om

    PrivateKey privKey = keypair.getPrivateKey();
    log.debug3("PrivKey: " + privKey.getAlgorithm() + " " + privKey.getFormat());

    X509Certificate[] chain = new X509Certificate[1];

    X500Name x500Name = new X500Name(x500String);
    chain[0] = keypair.getSelfCertificate(x500Name, expireIn);
    log.debug3("Certificate: " + chain[0].toString());

    keyStore.load(null, keyStorePassword.toCharArray());
    keyStore.setCertificateEntry(certAlias, chain[0]);
    keyStore.setKeyEntry(keyAlias, privKey, keyPassword.toCharArray(), chain);
    Key myKey = keyStore.getKey(keyAlias, keyPassword.toCharArray());
    log.debug("MyKey: " + myKey.getAlgorithm() + " " + myKey.getFormat());
}