Example usage for java.security SecureRandom SecureRandom

List of usage examples for java.security SecureRandom SecureRandom

Introduction

In this page you can find the example usage for java.security SecureRandom SecureRandom.

Prototype

public SecureRandom(byte[] seed) 

Source Link

Document

Constructs a secure random number generator (RNG) implementing the default random number algorithm.

Usage

From source file:Main.java

public static void main(String[] args) {
    SecureRandom secureRandom = new SecureRandom(new byte[] { 1, 3, 3, 7 });
    int[] randoms = IntStream.generate(secureRandom::nextInt).filter(n -> n > 0).limit(10).toArray();
    System.out.println(Arrays.toString(randoms));

    int[] nums = IntStream.iterate(1, n -> n * 2).limit(11).toArray();
    System.out.println(Arrays.toString(nums));
}

From source file:Main.java

public static void main(String[] args) throws NoSuchAlgorithmException {
    int numBytes = (new Integer("1111")).intValue();

    SecureRandom srand = new SecureRandom(new byte[] { 1, 2, 3, 4 });

    byte[] bytes = new byte[numBytes];
    srand.nextBytes(bytes);/*from  ww w . ja v a2 s  .  c o  m*/
    System.out.println(new String(bytes));
}

From source file:Main.java

public static void main(String[] args) throws NoSuchAlgorithmException {
    int numBytes = (new Integer("1111")).intValue();

    SecureRandom srand = new SecureRandom(new byte[] { 1, 2, 3, 4 });

    Provider provider = srand.getProvider();
    System.out.println(provider.getName());

    byte[] bytes = new byte[numBytes];
    srand.nextBytes(bytes);//from ww  w .j a va  2s.  c om
    System.out.println(bytes);
}

From source file:Main.java

public static void main(String[] args) throws NoSuchAlgorithmException {
    int numBytes = (new Integer("1111")).intValue();

    SecureRandom srand = new SecureRandom(new byte[] { 1, 2, 3, 4 });

    Provider provider = srand.getProvider();

    SecureRandom newRandom = SecureRandom.getInstance("SHA1PRNG", provider);

    System.out.println(provider.getName());

    byte[] bytes = new byte[numBytes];
    srand.nextBytes(bytes);//from w w  w . j a v  a 2s.  c  om
    System.out.println(bytes);
}

From source file:Main.java

private static Key newDesInstance(String strKey) {
    Key key = null;//  www .  j a  v  a2 s  . c om
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(new SecureRandom(strKey.getBytes()));
        key = keyGenerator.generateKey();
        keyGenerator = null;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return key;
}

From source file:Main.java

/**
 * do not work/*from   w w  w .  j a va 2 s  . co  m*/
 */
private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(128, new SecureRandom(seed)); // 192 and 256 bits may not be available
    return keyGenerator.generateKey().getEncoded();
}

From source file:Main.java

public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(encryptKey.getBytes()));

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

    return cipher.doFinal(content.getBytes("utf-8"));
}

From source file:Main.java

public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(decryptKey.getBytes()));

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
    byte[] decryptBytes = cipher.doFinal(encryptBytes);

    return new String(decryptBytes);
}

From source file:Main.java

private static Key getKey(Context mContext) {
    if (key == null) {
        try {//from w ww . jav  a 2s  .co m
            KeyGenerator _generator = KeyGenerator.getInstance("DES");
            _generator.init(new SecureRandom(mContext.getPackageName().getBytes()));
            key = _generator.generateKey();
            _generator = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return key;
}

From source file:com.nkapps.billing.services.AuthenticationService.java

public AuthenticationService() {
    random = new SecureRandom((new Date()).toString().getBytes());
}