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:Main.java

private static byte[] iv() {
    byte[] iv = null;
    try {//from  www .  j  a  v a 2  s.co  m
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        SecretKey skey = kgen.generateKey();
        iv = skey.getEncoded();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return iv;
}

From source file:Main.java

public static byte[] getRandomKey(byte[] seed) throws Exception {

    KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); //random
    sr.setSeed(seed);//ww  w .  j  a  va 2s. c  om
    kg.init(128, sr);
    SecretKey secretKey = kg.generateKey();
    return secretKey.getEncoded();
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    sr.setSeed(seed);/* w  w w .  ja v  a2 s  .  c  o  m*/
    kgen.init(128, sr);
    SecretKey sKey = kgen.generateKey();
    byte[] raw = sKey.getEncoded();

    return raw;
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(seed);//from w  ww .j  a  va2 s  .  co  m
    kgen.init(128, sr);
    SecretKey sKey = kgen.generateKey();
    byte[] raw = sKey.getEncoded();

    return raw;
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = null;/* w w  w  . jav  a  2 s .co m*/
    if (android.os.Build.VERSION.SDK_INT >= 17) {
        sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    } else {
        sr = SecureRandom.getInstance("SHA1PRNG");
    }
    sr.setSeed(seed);
    kgen.init(128, sr); //256 bits or 128 bits,192bits  
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    sr.setSeed(seed);/*  w ww. j a  v a 2 s .  c o m*/
    kgen.init(128, sr); // 192 and 256 bits may not be available
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kGen = KeyGenerator.getInstance("AES");
    SecureRandom sr;//w w  w .  ja  v  a 2s. com
    if (android.os.Build.VERSION.SDK_INT >= 17) {
        sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    } else {
        sr = SecureRandom.getInstance("SHA1PRNG");
    }
    sr.setSeed(seed);
    kGen.init(128, sr);
    SecretKey sKey = kGen.generateKey();
    return sKey.getEncoded();
}

From source file:Main.java

private static byte[] generateEncryptionSecret() {
    try {/*from  w  ww .jav a  2 s.  c om*/
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        generator.init(128);

        SecretKey key = generator.generateKey();
        return key.getEncoded();
    } catch (NoSuchAlgorithmException ex) {
        Log.w("keyutil", ex);
        return null;
    }
}

From source file:com.java.demo.DesDemo.java

public static void Init() {
    try {/*  w  w  w. j  a va  2  s  . c o  m*/
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(56);
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] bytesKey = secretKey.getEncoded();

        DESKeySpec dESKeySpec = new DESKeySpec(bytesKey);
        SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
        convertSecretKey = factory.generateSecret(dESKeySpec);

    } catch (Exception e) {
    }
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    //  SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    sr.setSeed(seed);// ww  w.  j a va2s  .c  o m
    kgen.init(128, sr); // 192 and 256 bits may not be available
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}