List of usage examples for java.security SecureRandom nextBytes
@Override public void nextBytes(byte[] bytes)
From source file:servlets.module.challenge.BrokenCryptoHomeMade.java
public static String randomKeyLengthString() { String result = new String(); try {/*ww w . j a va 2 s. c o m*/ byte byteArray[] = new byte[16]; SecureRandom psn1 = SecureRandom.getInstance("SHA1PRNG"); psn1.setSeed(psn1.nextLong()); psn1.nextBytes(byteArray); result = new String(byteArray, Charset.forName("US-ASCII")); //log.debug("Generated Key = " + result); if (result.length() != 16) { log.error("Generated Key is the incorrect Length: Shortening "); result = result.substring(0, 15); if (result.length() != 16) log.fatal("Encryption key length is Still not Right"); } } catch (Exception e) { log.error("Random Number Error : " + e.toString()); } return result; }
From source file:UUID.java
/** * DOCUMENT ME!/*w ww .jav a2 s. c om*/ * * @return DOCUMENT ME! */ public static UUID randomUUID() { SecureRandom securerandom = numberGenerator; if (securerandom == null) { numberGenerator = securerandom = new SecureRandom(); } byte[] abyte0 = new byte[16]; securerandom.nextBytes(abyte0); abyte0[6] &= 0xf; abyte0[6] |= 0x40; abyte0[8] &= 0x3f; abyte0[8] |= 0x80; UUID uuid = new UUID(abyte0); return new UUID(abyte0); }
From source file:eu.uqasar.service.AuthenticationService.java
/** * * @return @throws NoSuchAlgorithmException *///from w w w . java2s . com public static 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 source file:utils.Hash.java
/** * Creates a psedorandom base64 string/*from w ww . ja va 2 s . c o m*/ * @return Random String */ public static String randomBase64String() { String result = new String(); try { byte byteArray[] = new byte[256]; SecureRandom psn1 = SecureRandom.getInstance("SHA1PRNG"); Base64 base64 = new Base64(); psn1.setSeed(psn1.nextLong()); psn1.nextBytes(byteArray); result = new String(byteArray, Charset.forName("US-ASCII")); result = base64.encode(thisString(thisString(byteArray.toString())).getBytes()).toString(); log.debug("Generated String = " + result); } catch (Exception e) { log.error("Random Number Error : " + e.toString()); } return result; }
From source file:com.ntsync.android.sync.client.ClientKeyHelper.java
/** * //from w w w . jav a2s . c om * Get Private Key or create a new one. * * @param account * the account we're syncing * @return Private Key * @throws InvalidKeyException */ @SuppressLint("TrulyRandom") public static SecretKey getOrCreatePrivateKey(Account account, AccountManager accountManager) throws IOException, InvalidKeyException { SecretKey key = getPrivateKey(account, accountManager); if (key == null) { Log.i(TAG, "Create new private Key"); String pwd = PasswortGenerator.createPwd(PWD_WORD_LEN); SecureRandom random = new SecureRandom(); byte[] salt = new byte[SALT_LENGHT]; random.nextBytes(salt); key = createKey(account, accountManager, pwd, salt, false, null); } return key; }
From source file:org.odk.collect.android.utilities.EnhancedDigestScheme.java
/** * Creates a random cnonce value based on the current time. * // ww w . ja va2s. co m * @return The cnonce value as String. */ public static String createCnonce() { SecureRandom rnd = new SecureRandom(); byte[] tmp = new byte[8]; rnd.nextBytes(tmp); return encode(tmp); }
From source file:com.gfw.press.encrypt.Encrypt.java
/** * ?//from ww w .j av a 2s . co m * * @param content * ? * @return ?? */ public static String simpleEncrypt(String content, String key) { int SIMPLE_ENCRYPT_KEY_LENGTH = 16; byte[] keyBytes = null; byte[] encrypt = null; byte[] allData = null; try { keyBytes = key.getBytes(CHARSET); byte[] _keyBytes = new byte[SIMPLE_ENCRYPT_KEY_LENGTH]; if (keyBytes.length < SIMPLE_ENCRYPT_KEY_LENGTH) { SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(_keyBytes); System.arraycopy(keyBytes, 0, _keyBytes, 0, keyBytes.length); } else { System.arraycopy(keyBytes, 0, _keyBytes, 0, SIMPLE_ENCRYPT_KEY_LENGTH); } keyBytes = _keyBytes; Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding"); IvParameterSpec IVSpec = new IvParameterSpec(keyBytes); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, 0, keyBytes.length, "AES"), IVSpec); encrypt = cipher.doFinal(content.getBytes(CHARSET)); allData = new byte[encrypt.length + keyBytes.length]; System.arraycopy(keyBytes, 0, allData, 0, keyBytes.length); System.arraycopy(encrypt, 0, allData, keyBytes.length, encrypt.length); return encodeBase64(allData); } catch (Exception e) { } finally { keyBytes = encrypt = allData = null; } return ""; }
From source file:com.cloud.consoleproxy.ConsoleProxy.java
private static String genDefaultEncryptorPassword() { try {/*www . java 2 s. com*/ SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] randomBytes = new byte[16]; random.nextBytes(randomBytes); return Base64.encodeBase64String(randomBytes); } catch (NoSuchAlgorithmException e) { s_logger.error("Unexpected exception ", e); assert (false); } return "Dummy"; }
From source file:net.mybox.mybox.Common.java
/** * Generate a random salt value to be used for encrypting a password * @return/*from w ww . ja va 2s. co m*/ */ public static String generateSalt() throws NoSuchAlgorithmException { SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] bSalt = new byte[8]; random.nextBytes(bSalt); return byteToBase64(bSalt); }
From source file:org.projectforge.common.NumberHelper.java
/** * Generates secure random bytes of the given length and return base 64 encoded bytes as url safe String. This is not the length of the * resulting string!/*from www . j av a2 s . c o m*/ * @param numberOfBytes * @return */ public static String getSecureRandomUrlSaveString(final int numberOfBytes) { final SecureRandom random = new SecureRandom(); final byte[] bytes = new byte[numberOfBytes]; random.nextBytes(bytes); return Base64.encodeBase64URLSafeString(bytes); }