Example usage for java.security SecureRandom nextBytes

List of usage examples for java.security SecureRandom nextBytes

Introduction

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

Prototype

@Override
public void nextBytes(byte[] bytes) 

Source Link

Document

Generates a user-specified number of random bytes.

Usage

From source file:com.DSC.client.SecureChannel.java

/**
 * @param args// w  w w.  ja v a  2  s .co m
 * @throws InterruptedException 
 * @throws IOException 
 */
public static void main(String[] args) throws InterruptedException, IOException {
    /* Create their private & public keys */
    ECKey key = new ECKey();
    key.init();
    ProgramState.publicKey = (ECPublicKeyParameters) key.getPublic();
    ProgramState.privateKey = (ECPrivateKeyParameters) key.getPrivate();

    /* Create the IV engine */
    byte[] seed = new byte[64]; // 512 bit seed 
    SecureRandom random = new SecureRandom();
    random.nextBytes(seed);
    ProgramState.IVEngine = new ISAACRandomGenerator(new ISAACEngine());
    ProgramState.IVEngine.init(seed);

    /* Create the blacklist and trusted contacts */
    ProgramState.blacklist = ConcurrentHashMultiset.create();
    ProgramState.trustedKeys = new ConcurrentHashMap<String, Address>();

    /* Set the time for the client accurately using a NTP server */
    DateTimeUtils.setCurrentMillisOffset(getTimeOffset());
    ProgramState.fmt = DateTimeFormat.forPattern("HH:mm:ss");

    /* Set the default nick as anonymous */
    ProgramState.nick = "anonymous";

    /* Initialize ISAACRandomGenerator, set ProgramState.IVEngine */
    receiveController = new ReceiveController();
    sendController = new SendController();

    /* Start input event handler loop */
    eventLoop();
}

From source file:Main.java

static byte[] generateSalt(int length) {
    byte[] salt = new byte[length];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);
    return salt;/*w w  w.j  a va 2 s .c om*/
}

From source file:Main.java

public static byte[] generateSalt() {
    byte[] salt = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);

    return salt;//from ww w.j a v a  2 s  .  c o m
}

From source file:MainClass.java

private static byte[] passwordEncrypt(char[] password, byte[] plaintext) throws Exception {
    int MD5_ITERATIONS = 1000;
    byte[] salt = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);

    PBEKeySpec keySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
    SecretKey key = keyFactory.generateSecret(keySpec);
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, MD5_ITERATIONS);
    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(salt);//  w w w .  ja  v  a 2 s.c  o m
    baos.write(ciphertext);
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] getRandomKey() {
    byte[] keybytes = new byte[16];
    SecureRandom sr = new SecureRandom();
    sr.nextBytes(keybytes);
    return keybytes;
}

From source file:Main.java

public static byte[] generateSecureBytes(int size) {
    SecureRandom sr = new SecureRandom();
    byte[] bytes = new byte[size];
    sr.nextBytes(bytes);
    return bytes;
}

From source file:MainClass.java

public static IvParameterSpec createCtrIvForAES(int messageNumber, SecureRandom random) {
    byte[] ivBytes = new byte[16];
    random.nextBytes(ivBytes);
    ivBytes[0] = (byte) (messageNumber >> 24);
    ivBytes[1] = (byte) (messageNumber >> 16);
    ivBytes[2] = (byte) (messageNumber >> 8);
    ivBytes[3] = (byte) (messageNumber >> 0);
    for (int i = 0; i != 7; i++) {
        ivBytes[8 + i] = 0;//  ww  w . j  a  va2  s  . c  om
    }
    ivBytes[15] = 1;
    return new IvParameterSpec(ivBytes);
}

From source file:fi.ilmoeuro.membertrack.util.Crypto.java

public static String randomSalt() {
    byte[] randomBytes = new byte[32];
    SecureRandom random = new SecureRandom();
    random.nextBytes(randomBytes);
    String salt = Hex.encodeHexString(randomBytes);
    return salt;/*from w w w  . j  av a2 s  . c  om*/
}

From source file:Main.java

/**
 * Generates a new random app ID. Currently the App id consists of 8
 * hexadecimal digits generated based on the Android SecureRandom class.
 * //from  w w  w. ja  v a  2  s .c om
 * @return
 */
@SuppressLint("TrulyRandom")
public static String generateAppId() {
    SecureRandom sr = new SecureRandom();
    byte[] random = new byte[4];
    sr.nextBytes(random);
    return String.format("%02x%02x%02x%02x", random[0], random[1], random[2], random[3]);
}

From source file:Main.java

public static byte[] makeSalt() {
    SecureRandom random = new SecureRandom();
    byte[] salt = new byte[SALT_LENGTH / 8];
    random.nextBytes(salt);

    return salt;/*from   w w  w  . j ava2 s . com*/
}