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:org.apache.myfaces.shared.util.StateUtils.java

private static byte[] findSecret(String secret, String algorithm) {
    byte[] bytes = null;

    if (secret == null) {
        try {/*from   www .ja v  a 2  s  .  c  om*/
            KeyGenerator kg = KeyGenerator.getInstance(algorithm);
            bytes = kg.generateKey().getEncoded();

            if (log.isLoggable(Level.FINE)) {
                log.fine("generated random password of length " + bytes.length);
            }
        } catch (NoSuchAlgorithmException e) {
            // Generate random password length 8, 
            int length = 8;
            bytes = new byte[length];
            new Random().nextBytes(bytes);

            if (log.isLoggable(Level.FINE)) {
                log.fine("generated random password of length " + length);
            }
        }
    } else {
        bytes = new Base64().decode(secret.getBytes());
    }

    return bytes;
}

From source file:org.apache.myfaces.shared.util.StateUtils.java

private static byte[] findMacSecret(String secret, String algorithm) {
    byte[] bytes = null;

    if (secret == null) {
        try {//from  ww w . j  a v  a2  s  .c  o m
            KeyGenerator kg = KeyGenerator.getInstance(algorithm);
            bytes = kg.generateKey().getEncoded();

            if (log.isLoggable(Level.FINE)) {
                log.fine("generated random mac password of length " + bytes.length);
            }
        } catch (NoSuchAlgorithmException e) {
            // Generate random password length 8, 
            int length = 8;
            bytes = new byte[length];
            new Random().nextBytes(bytes);

            if (log.isLoggable(Level.FINE)) {
                log.fine("generated random mac password of length " + length);
            }
        }
    } else {
        bytes = new Base64().decode(secret.getBytes());
    }

    return bytes;
}

From source file:net.sf.xfd.provider.PublicProvider.java

private static @Nullable Key getSalt(Context c) {
    if (cookieSalt == null) {
        synchronized (PublicProvider.class) {
            if (cookieSalt == null) {
                try {
                    try (ObjectInputStream oos = new ObjectInputStream(c.openFileInput(COOKIE_FILE))) {
                        cookieSalt = (Key) oos.readObject();
                    } catch (ClassNotFoundException | IOException e) {
                        LogUtil.logCautiously("Unable to read key file, probably corrupted or missing", e);

                        final File corrupted = c.getFileStreamPath(COOKIE_FILE);

                        //noinspection ResultOfMethodCallIgnored
                        corrupted.delete();
                    }/*  w w  w. j  a va2s. c  o m*/

                    if (cookieSalt != null) {
                        return cookieSalt;
                    }

                    final KeyGenerator keygen = KeyGenerator.getInstance("HmacSHA1");
                    keygen.init(COOKIE_SIZE * Byte.SIZE);

                    cookieSalt = keygen.generateKey();

                    try (ObjectOutputStream oos = new ObjectOutputStream(
                            c.openFileOutput(COOKIE_FILE, Context.MODE_PRIVATE))) {
                        oos.writeObject(cookieSalt);
                    } catch (IOException e) {
                        LogUtil.logCautiously("Failed to save key file", e);

                        return null;
                    }
                } catch (NoSuchAlgorithmException e) {
                    throw new AssertionError("failed to initialize hash functions", e);
                }
            }
        }
    }

    return cookieSalt;
}

From source file:jef.tools.security.EncrypterUtil.java

/**
 * ?KEY/*from   w w w  . j av  a  2  s.c  o m*/
 * 
 * @param value
 * @param algom
 *            
 * @return
 */
public static final SecretKey generateKey(String algom, int keylength) {
    try {
        KeyGenerator keygen = KeyGenerator.getInstance(algom);
        keygen.init(keylength);
        SecretKey deskey = keygen.generateKey();
        return deskey;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.openintents.safe.CryptoHelper.java

/**
 * @return null if failure, otherwise hex string version of key
 * @author Isaac Potoczny-Jones/*  www  .jav a 2s.  c  o m*/
 */
public static String generateMasterKey() throws NoSuchAlgorithmException {
    try {
        KeyGenerator keygen;
        keygen = KeyGenerator.getInstance("AES");
        keygen.init(256);
        SecretKey genDesKey = keygen.generateKey();
        return toHexString(genDesKey.getEncoded());
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "generateMasterKey(): " + e.toString());
        throw e;
    }
}

From source file:es.jamisoft.comun.utils.cipher.CifradoDES.java

private SecretKey generaDesKey() {
    try {//from   w  w  w .  j  ava 2 s . c  o m
        KeyGenerator keygen = KeyGenerator.getInstance("DES");

        return keygen.generateKey();
    } catch (Exception e) {
        System.out.println("Error descifrando un texto:" + e);
        e.printStackTrace();

        return null;
    }
}

From source file:es.jamisoft.comun.utils.cipher.Cifrado3DES.java

private SecretKey generaDesKey() {
    try {//w  ww  . ja  v a  2 s . c  o m
        KeyGenerator keygen = KeyGenerator.getInstance("DESede");

        return keygen.generateKey();
    } catch (Exception e) {
        System.out.println("Error descifrando un texto:" + e);
        e.printStackTrace();

        return null;
    }
}

From source file:com.javiermoreno.springboot.rest.CryptographyServiceImplBlowfish.java

public CryptographyServiceImplBlowfish()
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
    keyGenerator.init(128);/*from   w w w  .j a  v  a 2 s . c  o m*/
    key = keyGenerator.generateKey();
    cipherEncrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);
    cipherDecrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipherDecrypt.init(Cipher.DECRYPT_MODE, key);
}

From source file:com.jwm123.loggly.reporter.TripleDesCipher.java

private void getKey() throws NoSuchAlgorithmException, IOException {
    File keyFile = appDir.getFileDir(keyPath);
    if (keyFile.exists()) {
        key = Base64.decode(FileUtils.readFileToString(keyFile));
    } else {/* w  ww  .j a  v  a 2s . co  m*/
        KeyGenerator generator = KeyGenerator.getInstance("DESede");
        SecretKey desKey = generator.generateKey();
        key = desKey.getEncoded();
        FileUtils.writeStringToFile(keyFile, new String(Base64.encode(key)));
    }

}

From source file:com.thoughtworks.go.security.AESCipherProvider.java

private byte[] generateKey() throws NoSuchAlgorithmException {
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(128);// w  w w  .  j av a 2  s  . c o m
    byte[] key = keygen.generateKey().getEncoded();
    return key;
}