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.tool.PrivateKeyTool.java

public static String coverPrivateKey2String(Key key) {
    String keyContent = Base64.encodeBase64String(key.getEncoded());
    return BEGIN_KEY + lineSeparator + keyContent + END_KEY;
}

From source file:fi.ilmoeuro.membertrack.util.Crypto.java

public static String hash(String candidate, String salt) {
    try {/*from w  ww .  j  a v a2s .c om*/
        SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec ks = new PBEKeySpec(candidate.toCharArray(), salt.getBytes(StandardCharsets.US_ASCII), 1024,
                128);
        SecretKey sk = skf.generateSecret(ks);
        Key k = new SecretKeySpec(sk.getEncoded(), "AES");
        return Hex.encodeHexString(k.getEncoded());
    } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
        throw new RuntimeException("Error while hashing", ex);
    }
}

From source file:birch.util.EncryptionKeyFileUtil.java

public static String createKey(String cipher, int keysize) throws InvalidKeyException,
        IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException {
    KeyGenerator keyGenerator;/* ww w .j  a  va  2  s  .c  om*/
    Key key;

    keyGenerator = KeyGenerator.getInstance(cipher);
    keyGenerator.init(keysize);
    key = keyGenerator.generateKey();

    return new String(Base64.encodeBase64(key.getEncoded()));
}

From source file:com.shenit.commons.codec.CodecUtils.java

/**
 * ??/*from   w  ww  . ja  va2s.  c  o  m*/
 * 
 * @return
 */
public static byte[] generateRandomKey(String codec) {
    SecureRandom sr = new SecureRandom();
    // DES?KeyGenerator
    KeyGenerator kg;
    try {
        kg = KeyGenerator.getInstance(codec);
        kg.init(sr);
        // ?
        Key secret = kg.generateKey();
        // ??
        return secret.getEncoded();
    } catch (NoSuchAlgorithmException e) {
        LOG.warn("[generateKey] not supported algorithm -> " + codec);
    }
    return null;
}

From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java

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

From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java

/**
 * ??/* www  .  ja  v a  2s. c  o  m*/
 * 
 * @param keyMap
 *            map
 * @return byte[] ?
 */
public static byte[] getPrivateKey(Map<String, Object> keyMap) {
    Key key = (Key) keyMap.get(PRIVATE_KEY);
    return key.getEncoded();
}

From source file:com.streamreduce.util.SecurityUtil.java

/**
 * Returns a new encryption key//from  w  w w .j  ava2 s. com
 *
 * @return encryption key in base64 encoding
 */
public static String generateNewKey() {
    AesCipherService cipherService = new AesCipherService();
    Key newKey = cipherService.generateNewKey();
    return new SimpleByteSource(newKey.getEncoded()).toBase64();
}

From source file:com.kuzumeji.platform.standard.SecurityServiceTest.java

private static String dumpKeyPair(final Key key) {
    return MessageFormat.format("?:{0} ?:{1} ?:{2}", key.getAlgorithm(), key.getFormat(),
            Hex.encodeHexString(key.getEncoded()));
}

From source file:org.artifactory.security.CryptoHelper.java

public static String toBase64(Key key) {
    return toBase64(key.getEncoded());
}

From source file:com.google.api.auth.DefaultJwksSupplierTest.java

private static void assertKeysEqual(Key expected, Key actual) {
    assertEquals(expected.getAlgorithm(), actual.getAlgorithm());
    assertEquals(new String(Hex.encode(expected.getEncoded())), new String(Hex.encode(actual.getEncoded())));
    assertEquals(expected.getFormat(), actual.getFormat());
}