Example usage for javax.crypto SecretKey getEncoded

List of usage examples for javax.crypto SecretKey getEncoded

Introduction

In this page you can find the example usage for javax.crypto SecretKey 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.apache.nifi.processors.standard.util.crypto.KeyedEncryptor.java

public KeyedEncryptor(final EncryptionMethod encryptionMethod, final SecretKey key, final byte[] iv) {
    this(encryptionMethod, key == null ? new byte[0] : key.getEncoded(), iv);
}

From source file:dualcontrol.CryptoHandler.java

public void handle(DualControlKeyStoreSession dualControl, Socket socket) throws Exception {
    try {//w  w w  . j  av a  2s . co m
        this.dualControl = dualControl;
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        int length = dis.readShort();
        byte[] bytes = new byte[length];
        dis.readFully(bytes);
        String data = new String(bytes);
        String[] fields = data.split(":");
        String mode = fields[0];
        String alias = fields[1];
        this.dos = new DataOutputStream(socket.getOutputStream());
        if (mode.equals("GETKEY")) {
            if (enableGetKey) {
                SecretKey key = dualControl.loadKey(alias);
                dos.writeUTF(key.getAlgorithm());
                write(key.getEncoded());
            }
        } else {
            cipher(mode, alias, fields[2], fields[3], fields[4]);
        }
    } finally {
        dos.close();
    }
}

From source file:net.thewaffleshop.nimbus.api.EncryptionAPI.java

/**
 * Generate a {@link SecretKey} from a password and salt
 *
 * @param password//from   w ww  . ja v a  2s .  co m
 * @param salt
 * @return
 */
public SecretKey createSecretKey(String password, byte[] salt) {
    try {
        PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 1024, 256);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        SecretKey secretKey = factory.generateSecret(keySpec);
        return new SecretKeySpec(secretKey.getEncoded(), "AES");
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.che.api.crypt.server.JCEEncryptTextService.java

private SecretKey generateSecret() throws NoSuchAlgorithmException, InvalidKeySpecException {
    final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(this.secretKeyFactoryAlgorithm);
    final KeySpec keySpec = new PBEKeySpec(getMasterPassword(), this.salt, 65536, this.keySize);
    final SecretKey tempKey = keyFactory.generateSecret(keySpec);
    final SecretKey secret = new SecretKeySpec(tempKey.getEncoded(), this.cipher);
    return secret;
}

From source file:com.torchmind.authenticator.AbstractTokenGenerator.java

/**
 * {@inheritDoc}/*  ww w  .  j  a va2 s . c  o m*/
 */
@NonNull
@Override
public String buildHandshakeCode(@NonNull SecretKey secretKey, boolean humanReadable) {
    String code = (new Base32()).encodeAsString(secretKey.getEncoded());

    if (humanReadable) {
        String tmp = "";

        for (int i = 1; i <= code.length(); ++i) {
            tmp += code.charAt((i - 1));

            if ((i % 4) == 0) {
                tmp += " ";
            }
        }

        if (tmp.charAt((tmp.length() - 1)) == ' ') {
            code = tmp.substring(0, (tmp.length() - 1)).toLowerCase();
        } else {
            code = tmp.toLowerCase();
        }
    }

    return code;
}

From source file:com.cyberninjas.xerobillableexpenses.util.Settings.java

public Settings() {
    try {//w  ww  . ja v  a  2  s.  com
        String parentClass = new Exception().getStackTrace()[1].getClassName();
        this.prefs = Preferences.userNodeForPackage(Class.forName(parentClass));
        Random r = new SecureRandom();

        //Set IV
        this.iv = prefs.getByteArray("DRUGS", null);

        //Pick Random PWD
        byte[] b = new byte[128];
        r.nextBytes(b);
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        sha.update(b);
        String sHash = new String(Base64.encodeBase64(sha.digest()));

        String password = prefs.get("LAYOUT", sHash);
        if (password.equals(sHash))
            prefs.put("LAYOUT", sHash);

        //Keep 'em Guessing
        r.nextBytes(b);
        sha.update(b);
        prefs.put("PASSWORD", new String(Base64.encodeBase64(sha.digest())));

        //Set Random Salt
        byte[] tSalt = new byte[8];
        r.nextBytes(tSalt);
        byte[] salt = prefs.getByteArray("HIMALAYAN", tSalt);
        if (Arrays.equals(salt, tSalt))
            prefs.putByteArray("HIMALAYAN", salt);

        /* Derive the key, given password and salt. */
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
        SecretKey tmp = factory.generateSecret(spec);
        this.secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(RSAx509CertGen.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidKeySpecException ex) {
        Logger.getLogger(RSAx509CertGen.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(RSAx509CertGen.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.joyent.manta.client.crypto.SecretKeyUtilsTest.java

public void canGenerateAesGcmNoPaddingKey() {
    SecretKey key = SecretKeyUtils.generate(AesGcmCipherDetails.INSTANCE_128_BIT);
    Assert.assertNotNull(key, "Generated key was null");

    byte[] bytes = key.getEncoded();

    SecretKey loaded = SecretKeyUtils.loadKey(bytes, AesGcmCipherDetails.INSTANCE_128_BIT);

    Assert.assertEquals(loaded, key, "Generated key doesn't match loaded key");
}

From source file:com.joyent.manta.client.crypto.SecretKeyUtilsTest.java

public void canGenerateAesCtrNoPaddingKey() {
    SecretKey key = SecretKeyUtils.generate(AesCtrCipherDetails.INSTANCE_128_BIT);
    Assert.assertNotNull(key, "Generated key was null");

    byte[] bytes = key.getEncoded();

    SecretKey loaded = SecretKeyUtils.loadKey(bytes, AesCtrCipherDetails.INSTANCE_128_BIT);

    Assert.assertEquals(loaded, key, "Generated key doesn't match loaded key");
}

From source file:com.joyent.manta.client.crypto.SecretKeyUtilsTest.java

public void canGenerateAesCbcPkcs5PaddingKey() {
    SecretKey key = SecretKeyUtils.generate(AesCbcCipherDetails.INSTANCE_128_BIT);
    Assert.assertNotNull(key, "Generated key was null");

    byte[] bytes = key.getEncoded();

    SecretKey loaded = SecretKeyUtils.loadKey(bytes, AesCbcCipherDetails.INSTANCE_128_BIT);

    Assert.assertEquals(loaded, key, "Generated key doesn't match loaded key");
}

From source file:org.zuinnote.flink.office.common.FlinkKeyStoreManager.java

/***
 * Retrieves a password from the currently opened keystore
 * //  w w  w .j a  va2s  .  com
 * @param alias
 * @param passwordPassword
 * @return
 * @throws NoSuchAlgorithmException
 * @throws UnrecoverableEntryException
 * @throws KeyStoreException
 * @throws InvalidKeySpecException
 */
public String getPassword(String alias, String passwordPassword) throws NoSuchAlgorithmException,
        UnrecoverableEntryException, KeyStoreException, InvalidKeySpecException {
    SecretKey sk = (SecretKey) this.keystore.getKey(alias, passwordPassword.toCharArray());
    return new String(sk.getEncoded());
}