Example usage for javax.crypto KeyGenerator generateKey

List of usage examples for javax.crypto KeyGenerator generateKey

Introduction

In this page you can find the example usage for javax.crypto KeyGenerator generateKey.

Prototype

public final SecretKey generateKey() 

Source Link

Document

Generates a secret key.

Usage

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

/**
 * {@inheritDoc}/*from   w w  w . jav a 2s .  c o m*/
 */
@NonNull
@Override
public SecretKey generateSecret() {
    try {
        KeyGenerator generator = KeyGenerator.getInstance("Hmac" + this.algorithm.name());
        generator.init(80);
        return generator.generateKey();
    } catch (NoSuchAlgorithmException ex) {
        throw new UnsupportedOperationException(
                "The specified algorithm is not supported by this Java VM implementation: " + ex.getMessage(),
                ex);
    }
}

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:org.jpos.qi.eeuser.ConsumersView.java

private SecretKey generateKey() throws NoSuchAlgorithmException {
    KeyGenerator gen = KeyGenerator.getInstance(HASH_ALGORITHM);
    return gen.generateKey();
}

From source file:com.muk.services.commerce.CryptoServiceImpl.java

@PostConstruct
public void postConstruct() {
    try {/*from   w w w .  jav a  2  s .c  o  m*/
        final KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        temporaryKey = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES");

        final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        final byte[] iv = new byte[cipher.getBlockSize()];
        new SecureRandom().nextBytes(iv);
        ivSpec = new IvParameterSpec(iv);
    } catch (final NoSuchAlgorithmException ex) {
        LOG.error("Failed to initalize encryption key", ex);
    } catch (final NoSuchPaddingException padEx) {
        LOG.error("Failed to get cipher.", padEx);
    }
}

From source file:se.vgregion.portal.cs.util.CryptoUtilImpl.java

/**
 * Encrypt a value and generate a keyfile. if the keyfile is not found then a new one is created
 * //from   w  w  w .  j a  va2s.c  o m
 * @param value
 *            - value to be encrypted
 * @throws GeneralSecurityException
 *             - security exception
 * @return Encrypted value
 */
@Override
public String encrypt(String value) throws GeneralSecurityException {
    if (!keyFile.exists()) {
        KeyGenerator keyGen = KeyGenerator.getInstance(AES);
        keyGen.init(KEY_SIZE);
        SecretKey sk = keyGen.generateKey();
        FileWriter fw = null;
        try {
            fw = new FileWriter(keyFile);
            fw.write(byteArrayToHexString(sk.getEncoded()));
            fw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    SecretKeySpec sks = getSecretKeySpec();

    Cipher cipher = Cipher.getInstance(AES);
    cipher.init(Cipher.ENCRYPT_MODE, sks, cipher.getParameters());
    byte[] encrypted = cipher.doFinal(value.getBytes());
    return byteArrayToHexString(encrypted);
}

From source file:org.kuali.rice.kew.documentoperation.web.DocumentContentOperationAction.java

private SecretKey getSecretKey(String encryptionKey) throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    SecretKey desKey = keygen.generateKey();

    // Create the cipher
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init((Cipher.UNWRAP_MODE), desKey);

    byte[] bytes = Base64.decodeBase64(encryptionKey.getBytes());

    SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");

    DESKeySpec keyspec = new DESKeySpec(bytes);
    desKey = desFactory.generateSecret(keyspec);
    // Create the cipher
    cipher.init((Cipher.WRAP_MODE), desKey);
    return desKey;
}

From source file:org.codice.ddf.configuration.migration.MigrationZipFileTest.java

private void generateKeyWithWrongAlgorithm(Path keyPath) throws NoSuchAlgorithmException, IOException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
    SecretKey secretKey = keyGenerator.generateKey();
    char[] hexKey = encodeHex(secretKey.getEncoded());
    writeStringToFile(keyPath.toFile(), String.valueOf(hexKey), Charsets.UTF_8);
}

From source file:org.kuali.rice.core.impl.encryption.DemonstrationGradeEncryptionServiceImpl.java

private SecretKey unwrapEncodedKey(String key) throws Exception {
    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    SecretKey desKey = keygen.generateKey();

    // Create the cipher
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init((Cipher.UNWRAP_MODE), desKey);

    byte[] bytes = Base64.decodeBase64(key.getBytes());

    SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES");

    DESKeySpec keyspec = new DESKeySpec(bytes);
    SecretKey k = desFactory.generateSecret(keyspec);

    return k;/*  www.  ja  v a  2  s .c o m*/

}

From source file:org.talend.utils.security.AES.java

public AES() {
    try {//from  www. j a v  a  2s  .c  o  m
        // TDI-28380: Database password in tac db configuration page becomes empty once restart tomcat on Solaris.
        // TDI-30348: Whole tac configuration lost for the passwords.

        Provider p = Security.getProvider("BC");
        KeyGenerator keyGen = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM, p);

        SecureRandom random = SecureRandom.getInstance(RANDOM_SHA1PRNG);
        random.setSeed(KeyValues);
        keyGen.init(128, random);

        Key key = keyGen.generateKey();

        ecipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, p);
        dcipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, p);

        ecipher.init(Cipher.ENCRYPT_MODE, key);
        dcipher.init(Cipher.DECRYPT_MODE, key);
    } catch (Exception e) {
        // log the error to avoid that break GWT service
        log.error(e.getMessage(), e);
    }
}

From source file:com.adaptris.security.password.AesCrypto.java

public String encode(String plainText, String charset) throws PasswordException {
    String result = null;/*from   www . j  a va2s .com*/
    try {
        KeyGenerator kg = KeyGenerator.getInstance(ALG);
        kg.init(KEY_LEN, SecurityUtil.getSecureRandom());
        SecretKey sessionKey = kg.generateKey();
        Cipher dataCipher = Cipher.getInstance(CIPHER);
        dataCipher.init(Cipher.ENCRYPT_MODE, sessionKey);
        byte[] encryptedBody = dataCipher.doFinal(seed(plainText, charset));
        Output output = new Output();
        output.setSessionKey(sessionKey.getEncoded());
        output.setSessionVector(dataCipher.getIV());
        output.setEncryptedData(encryptedBody);
        result = Password.PORTABLE_PASSWORD + output.write();
    } catch (Exception e) {
        throw new PasswordException(e);
    }
    return result;
}