Example usage for java.security Key getEncoded

List of usage examples for java.security Key getEncoded

Introduction

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

Prototype

public byte[] getEncoded();

Source Link

Document

Returns the key in its primary encoding format, or null if this key does not support encoding.

Usage

From source file:com.aqnote.shared.cryptology.cert.util.KeyStoreUtil.java

public static KeyStore createPCSK12KeyStore(String alias, Key key, char[] pwd, Certificate[] chain)
        throws CertException {

    try {/*from  www. ja  v  a  2 s  . c om*/
        KeyStore keyStore = KeyStore.getInstance(PKCS12_STORE_TYPE);
        keyStore.load(null, pwd);
        if (pwd == null) {
            keyStore.setKeyEntry(alias, key.getEncoded(), chain);
        } else {
            keyStore.setKeyEntry(alias, key, pwd, chain);
        }
        return keyStore;
    } catch (KeyStoreException e) {
        throw new CertException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new CertException(e);
    } catch (CertificateException e) {
        throw new CertException(e);
    } catch (IOException e) {
        throw new CertException(e);
    }
}

From source file:org.sakaiproject.linktool.LinkToolUtil.java

/**
 * Writes <code>key</code> to file with name <code>filename</code>
 *
 * @throws IOException if something goes wrong.
 *//*from  ww w .  j av a  2 s .  c o m*/
private static void writeKey(Key key, String filename) {
    FileOutputStream file = null;
    try {
        file = new FileOutputStream(filename);
        file.write(key.getEncoded());
    } catch (FileNotFoundException e) {
        M_log.error("Unable to write new key to " + filename);
    } catch (IOException e) {
        M_log.error("Unable to write new key to " + filename);
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (Exception e) {
                M_log.error("Unable to write new key to " + filename);
            }
        }
    }
}

From source file:cn.mrdear.pay.util.RSAUtils.java

/**
 * ?/*w  w w  .  jav  a2  s . c  o  m*/
 * 
 * @param key
 *            
 * @return (BASE64?)
 */
public static String getKeyString(Key key) {
    Assert.notNull(key);

    return Base64.encodeBase64String(key.getEncoded());
}

From source file:cn.util.RSAUtils.java

public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
    Key key = (Key) keyMap.get("public");
    byte[] publicKey = key.getEncoded();
    return Base64Util.encryptBASE64(publicKey);
}

From source file:cn.util.RSAUtils.java

public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
    Key key = (Key) keyMap.get("private");
    byte[] privateKey = key.getEncoded();
    return Base64Util.encryptBASE64(privateKey);
}

From source file:net.jmhertlein.core.crypto.Keys.java

/**
 * Gets a Base64 representation of the given Key
 *
 * @param key//w  ww .ja  v a2 s.  com
 *
 * @return
 */
public static String getBASE64ForKey(Key key) {
    return Base64.encodeBase64String(key.getEncoded());
}

From source file:org.aon.esolutions.appconfig.client.util.RSAEncryptUtil.java

/**
 * Convert a Key to string encoded as BASE64
 * @param key The key (private or public)
 * @return A string representation of the key
 *///from  ww w. jav  a2 s.c o  m
public static String getKeyAsString(Key key) {
    // Get the bytes of the key
    byte[] keyBytes = key.getEncoded();
    // Convert key to BASE64 encoded string
    return encodeBASE64(keyBytes);
}

From source file:gemlite.core.util.RSAUtils.java

/**
 * <p>/*from w  w  w  .  j  a v  a 2 s .  c om*/
 * ?
 * </p>
 * 
 * @param keyMap
 *          
 * @return
 * @throws Exception
 */
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
    Key key = (Key) keyMap.get(PUBLIC_KEY);
    return Base64Utils.encode(key.getEncoded());
}

From source file:gemlite.core.util.RSAUtils.java

/**
 * <p>/*w  w w.  j a va2 s  .c  om*/
 * ??
 * </p>
 * 
 * @param keyMap
 *          
 * @return
 * @throws Exception
 */
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
    Key key = (Key) keyMap.get(PRIVATE_KEY);
    return Base64Utils.encode(key.getEncoded());
}

From source file:jenkins.bouncycastle.api.PEMEncodable.java

/**
 * Generates an digest from a Key object in the specified digest format. The supported digest formats will depend on
 * the JVM API./*from  w  w w  .j  a  v a2  s  .co m*/
 * 
 * @param k key to generate the digest from
 * @param algorithm digest format
 * @return the generated digest
 * @throws NoSuchAlgorithmException when provided digest algorithm is not available
 */
@Nonnull
public static byte[] getKeyDigest(@Nonnull Key k, @Nonnull String algorithm) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance(algorithm);
    md.update(k.getEncoded());
    return md.digest();
}