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:com.blackcrowsys.sinscrypto.AesKeyGenerator.java

@Override
public SecretKey generateSecretKey(String password, String salt)
        throws NoSuchAlgorithmException, InvalidKeySpecException, DecoderException {
    SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
    KeySpec spec = new PBEKeySpec(password.toCharArray(), Hex.decodeHex(salt.toCharArray()), ITERATION,
            KEYLENGTH);//www  .  j a  v  a  2  s  . c o m
    SecretKey key = factory.generateSecret(spec);
    return new SecretKeySpec(key.getEncoded(), ENCRYPTION);
}

From source file:io.hawkcd.agent.services.SecurityService.java

private Key generateKey() throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    char[] password = PASSWORD.toCharArray();
    byte[] salt = getBytes(SALT);

    KeySpec spec = new PBEKeySpec(password, salt, 65536, 128);
    SecretKey tmp = factory.generateSecret(spec);
    byte[] encoded = tmp.getEncoded();
    return new SecretKeySpec(encoded, "AES");
}

From source file:pl.mwaleria.safecommunicator.core.cipher.CipherManager.java

public byte[] encrypt(byte[] input, PublicKey publicKey) throws CryptoException {
    try {/*w w w.j a v  a  2 s  .  co  m*/
        KeyGenerator generator = KeyGenerator.getInstance(Constants.CIPHER_SYMETRIC_ALGORITHM);
        generator.init(Constants.CIPHER_SYMETRIC_SIZE);
        SecretKey key = generator.generateKey();
        byte[] aesKey = key.getEncoded();
        byte[] cryptedAesKey = this.encryptAsymetric(aesKey, publicKey);
        Cipher cipher = Cipher.getInstance(Constants.CIPHER_SYMETRIC_INSTANCE);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return ArrayUtils.addAll(cryptedAesKey, cipher.doFinal(input));
    } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(CipherManager.class.getName()).log(Level.SEVERE, null, ex);
        throw new CryptoException(ex);
    }
}

From source file:com.github.sshw.crypt.EncryptionBean.java

public Key keyFromPassword(String password) throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(password.toCharArray(), password.getBytes(), 1000, 128);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
    return secret;
}

From source file:org.edeoliveira.oauth2.dropwizard.oauth2.auth.CookieEncrypter.java

public CookieEncrypter() throws Exception {
    // Get the KeyGenerator
    KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
    kgen.init(BIT_LENGTH); // 192 and 256 bits may not be available

    // Generate the secret key specs
    SecretKey skey = kgen.generateKey();
    byte[] secretKey = skey.getEncoded();
    keySpec = new SecretKeySpec(secretKey, ALGORITHM);
}

From source file:com.joyent.manta.http.EncryptionHttpHelper.java

/**
 * Initializes an HMAC instance using the specified secret key.
 *
 * @param secretKey secret key to initialize with
 * @param hmac HMAC object to be initialized
 *//*from w  ww. ja va2  s  .c o m*/
private static void initHmac(final SecretKey secretKey, final HMac hmac) {
    hmac.init(new KeyParameter(secretKey.getEncoded()));
}

From source file:adminpassword.Encryption.java

public String encrypt(String word, String idKey) throws Exception {

    byte[] ivBytes;
    String password = idKey; //you can give whatever you want. This is for testing purpose
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);/*from w ww  .ja va  2 s .c o m*/

    byte[] saltBytes = bytes;

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);
    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    //encrypting the word
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = cipher.getParameters();
    ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
    byte[] encryptedTextBytes = cipher.doFinal(word.getBytes("UTF-8"));

    //prepend salt and vi
    byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length];
    System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length);
    System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length);
    System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length,
            encryptedTextBytes.length);
    return new Base64().encodeToString(buffer);
}

From source file:adminpassword.Decryption.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText, String idKey) throws Exception {
    String password = idKey;/* w ww .j a  v a2s  .  c o  m*/

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    //strip off the salt and iv
    ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText));
    byte[] saltBytes = new byte[20];
    buffer.get(saltBytes, 0, saltBytes.length);
    byte[] ivBytes1 = new byte[cipher.getBlockSize()];
    buffer.get(ivBytes1, 0, ivBytes1.length);
    byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length];
    buffer.get(encryptedTextBytes);

    // Deriving the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1));

    byte[] decryptedTextBytes = null;
    try {
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);

    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

From source file:com.titilink.camel.rest.util.PasswordUtils.java

/**
 * Rabiitsalt?AES????SHA256??./*from   w  w  w.j a va 2 s. c o  m*/
 *
 * @param Rabiit ??
 * @param salt   ?
 * @return 
 */
public synchronized static Key generateKey(char[] Rabiit, byte[] salt) {
    SecretKeyFactory factory;
    SecretKey tmpkey = null;
    SecretKey secret = null;
    try {
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");

        //AES128??AES128
        int aeskeylen = AppProperties.getAsInt("AES_KEY_LEN", AES_KEY_LEN);
        KeySpec keyspec = new PBEKeySpec(Rabiit, salt, ITERATION_COUNT, aeskeylen);
        tmpkey = factory.generateSecret(keyspec);

        //AES??
        secret = new SecretKeySpec(tmpkey.getEncoded(), ENCODER_AES);
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error("generateKey error, no such method exception.");
    } // "PBKDF2WithHmacSHA256"   JDK8??
    catch (InvalidKeySpecException e) {
        LOGGER.error("generateKey error, invalid key exception.");
    }
    return secret;
}

From source file:com.paypal.utilities.AESDecrypt.java

public AESDecrypt(String encryptedString) throws Exception {
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
    SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
    SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES");

    encrypt = Base64.decodeBase64(encryptedString.getBytes());

    eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    eCipher.init(Cipher.ENCRYPT_MODE, secretKey);

    byte[] iv = extractIV();
    dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
}