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:org.javaweb.utils.RSAUtils.java

/**
 * RSA???//from   www  .j a v  a2 s  . c o  m
 *
 * @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.apache.hadoop.hbase.regionserver.TestEncryptionKeyRotation.java

private static byte[] extractHFileKey(Path path) throws Exception {
    HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(), path, new CacheConfig(conf), conf);
    try {/*from w w w .ja v  a 2s. c  o m*/
        reader.loadFileInfo();
        Encryption.Context cryptoContext = reader.getFileContext().getEncryptionContext();
        assertNotNull("Reader has a null crypto context", cryptoContext);
        Key key = cryptoContext.getKey();
        assertNotNull("Crypto context has no key", key);
        return key.getEncoded();
    } finally {
        reader.close();
    }
}

From source file:vellumcert.Pems.java

public static String buildKeyPem(Key privateKey) {
    StringBuilder builder = new StringBuilder();
    builder.append(DASHES);/*from w ww.  j av a 2s  .  co  m*/
    builder.append(BEGIN_PRIVATE_KEY);
    builder.append(DASHES);
    builder.append('\n');
    builder.append(Base64.encodeBase64String(privateKey.getEncoded()));
    builder.append(DASHES);
    builder.append(END_PRIVATE_KEY);
    builder.append(DASHES);
    builder.append('\n');
    return builder.toString();
}

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

/**
 * RSA???/*  ww w.j  a  v a  2  s . c  o  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:com.owncloud.android.utils.PushUtils.java

private static int saveKeyToFile(Key key, String path) {
    byte[] encoded = key.getEncoded();
    FileOutputStream keyFileOutputStream = null;
    try {/* ww  w .  j  ava2  s . co  m*/
        if (!new File(path).exists()) {
            new File(path).createNewFile();
        }
        keyFileOutputStream = new FileOutputStream(path);
        keyFileOutputStream.write(encoded);
        keyFileOutputStream.close();
        return 0;
    } catch (FileNotFoundException e) {
        Log_OC.d(TAG, "Failed to save key to file");
    } catch (IOException e) {
        Log_OC.d(TAG, "Failed to save key to file via IOException");
    }

    return -1;
}

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

public static String convertToString(Key key) {
    return convertToString(key.getEncoded(), true);
}

From source file:org.codice.ddf.security.certificate.generator.PkiTools.java

/**
 * @param key object/*  w  w  w . j  av  a2 s .c o  m*/
 * @return PEM encoded string represents the bytes of the key
 */
public static String keyToPem(Key key) {
    Validate.isTrue(key != null, "Key cannot be null");
    return derToPem(key.getEncoded());
}

From source file:hudson.plugins.ec2.EC2PrivateKey.java

static String digestOpt(Key k, String dg) throws IOException {
    try {/*from  w  ww .j av  a 2s. c o  m*/
        MessageDigest md5 = MessageDigest.getInstance(dg);

        DigestInputStream in = new DigestInputStream(new ByteArrayInputStream(k.getEncoded()), md5);
        try {
            while (in.read(new byte[128]) > 0)
                ; // simply discard the input
        } finally {
            in.close();
        }
        StringBuilder buf = new StringBuilder();
        char[] hex = Hex.encodeHex(md5.digest());
        for (int i = 0; i < hex.length; i += 2) {
            if (buf.length() > 0)
                buf.append(':');
            buf.append(hex, i, 2);
        }
        return buf.toString();
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError(e);
    }
}

From source file:com.zxy.commons.codec.rsa.RSAUtils.java

/**
 * <p>//www  .jav  a 2 s  . c o  m
 * ?
 * </p>
 * 
 * @param keyMap 
 * @return String
 * @throws Exception Exception
 */
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
    Key key = (Key) keyMap.get(PUBLIC_KEY);
    return new String(Base64.encodeBase64(key.getEncoded()));
}

From source file:com.zxy.commons.codec.rsa.RSAUtils.java

/**
 * <p>//  w  w w.  j  ava  2 s .com
 * ??
 * </p>
 * 
 * @param keyMap 
 * @return String
 * @throws Exception Exception
 */
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
    Key key = (Key) keyMap.get(PRIVATE_KEY);
    return new String(Base64.encodeBase64(key.getEncoded()));
}