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.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java

/**
 * Random data generator//from   w  ww . ja  v  a  2 s  .  c om
 *
 * Creates a new SecureRandom instance for each all.  Each instance is self seeding.
 *
 * @param numberOfBytes Number of Random Bytes to return
 * @return Random bytes Base64 Encoded
 */
public String generateRandomBytes(Integer numberOfBytes) {
    byte[] ivBytes = new byte[numberOfBytes];
    final SecureRandom rng = new SecureRandom();
    rng.nextBytes(ivBytes);
    return Base64.encodeBase64String(ivBytes);
}

From source file:de.widone.services.UserService.java

private byte[] getSalt() {
    try {//w  ww  .  ja  v  a  2  s  .c  o  m
        SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG");
        byte[] salt = new byte[8];
        rnd.nextBytes(salt);
        return salt;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.bitsofproof.supernode.api.BIP39Test.java

@Test
public void bip39EncodeDecodeTest() throws IOException, JSONException, ValidationException {
    JSONObject testData = readObject(TESTS);
    JSONArray english = testData.getJSONArray("english");
    for (int i = 0; i < testData.length(); ++i) {
        JSONArray test = english.getJSONArray(i);
        byte[] m = BIP39.decode(test.getString(1), "BOP");
        assertTrue(test.getString(1).equals(BIP39.encode(m, "BOP")));
    }/*w ww  . ja  v a2s. c  o m*/
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < 1000; ++i) {
        byte[] secret = new byte[32];
        random.nextBytes(secret);
        String e = BIP39.encode(secret, "BOP");
        assertTrue(Arrays.equals(BIP39.decode(e, "BOP"), secret));
    }
}

From source file:edu.ku.brc.helpers.Encryption.java

/**
 * Encrypts the string from its array of bytes
 * @param input the actual string (in bytes) that is to be encrypted
 * @param password a password, which is really any string, but must be the same string that was used to decrypt it.
 * @return a byte array of the encrypted chars
 * @throws Exception in case something goes wrong
 *///from   w w  w .  j a  v a  2  s  .  c  o  m
public static byte[] encrypt(byte[] input, char[] password) throws Exception {
    /*
     * Get ourselves a random number generator, needed in a number of places for encrypting.
     */
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); //$NON-NLS-1$

    /*
     * A "salt" is considered an essential part of password-based encryption. The salt is
     * selected at random for each encryption. It is not considered "sensitive", so it is tacked
     * onto the generated ciphertext without any special processing. It doesn't matter if an
     * attacker actually gets the salt. The salt is used as part of the key, with the very
     * useful result that if you Encryption the same plaintext with the same password twice, you
     * get *different* ciphertexts. There are lots of pages on the 'net with information about
     * salts and password-based encryption, so read them if you want more details. Suffice to
     * say salt=good, no salt=bad.
     */
    byte[] salt = new byte[SALT_LENGTH];
    sr.nextBytes(salt);

    /*
     * We've now got enough information to build the actual key. We do this by encapsulating the
     * variables in a PBEKeySpec and using a SecretKeyFactory to transform the spec into a key.
     */
    PBEKeySpec keyspec = new PBEKeySpec(password, salt, ITERATION_COUNT);
    SecretKeyFactory skf = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey key = skf.generateSecret(keyspec);

    /*
     * We'll use a ByteArrayOutputStream to conveniently gather up data as it's encrypted.
     */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    /*
     * We've to a key, but to actually Encryption something, we need a "cipher". The cipher is
     * created, then initialized with the key, salt, and iteration count. We use a
     * PBEParameterSpec to hold the salt and iteration count needed by the Cipher object.
     */
    PBEParameterSpec paramspec = new PBEParameterSpec(salt, ITERATION_COUNT);
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, key, paramspec, sr);

    /*
     * First, in our output, we need to save the salt in plain unencrypted form.
     */
    baos.write(salt);

    /*
     * Next, Encryption our plaintext using the Cipher object, and write it into our output buffer.
     */
    baos.write(cipher.doFinal(input));

    /*
     * We're done. For security reasons, we probably want the PBEKeySpec object to clear its
     * internal copy of the password, so it can't be stolen later.
     */
    keyspec.clearPassword();
    return baos.toByteArray();
}

From source file:com.hypersocket.auth.PasswordEncryptionService.java

public byte[] generateSalt() throws NoSuchAlgorithmException {
    // VERY important to use SecureRandom instead of just Random
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

    // Generate a 8 byte (64 bit) salt as recommended by RSA PKCS5
    byte[] salt = new byte[8];
    random.nextBytes(salt);

    return salt;//from  w w w  .j av  a 2 s  .co m
}

From source file:com.afwsamples.testdpc.safetynet.SafetyNetFragment.java

/**
 * For simplicity, we generate the nonce in the client. However, it should be generated on the
 * server for anti-replay protection.//w  w w .j  a  v a2s.co m
 */
private byte[] generateNonce() {
    byte[] nonce = new byte[32];
    SecureRandom secureRandom = new SecureRandom();
    secureRandom.nextBytes(nonce);
    return nonce;
}

From source file:edu.tamu.tcat.crypto.spongycastle.SecureTokenImpl.java

private byte[] createIV() {
    SecureRandom random = new SecureRandom();
    byte[] iv = new byte[ivSize / 8];
    random.nextBytes(iv);
    return iv;//w  w  w .  ja v a2 s.co  m
}

From source file:com.haulmont.cuba.core.sys.encryption.Sha1EncryptionModule.java

protected String generateSalt() throws NoSuchAlgorithmException {
    SecureRandom random = SecureRandom.getInstance(RANDOMIZE_ALGORITHM);
    byte[] salt = new byte[SALT_LENGTH_BYTES];
    random.nextBytes(salt);
    return new String(Hex.encodeHex(salt));
}

From source file:cl.whyem.testsutilityproject.otpgenerator.KeyBase.java

protected byte[] randomBytes(int count) {
    SecureRandom random = new SecureRandom();
    byte[] buf = new byte[count];
    random.nextBytes(buf);
    return buf;/*from w  w w . j  a v  a  2  s  .c  o  m*/
}

From source file:edu.tamu.tcat.crypto.bouncycastle.SecureTokenImpl.java

/**
 * Create an initialization vector from a {@link SecureRandom} for use with block chaining algorithms.
 *///from  w ww .j  a va  2 s.c o m
private byte[] createIV() {
    SecureRandom random = new SecureRandom();
    byte[] iv = new byte[ivSize / 8];
    random.nextBytes(iv);
    return iv;
}