List of usage examples for java.security SecureRandom nextBytes
@Override public void nextBytes(byte[] bytes)
From source file:Main.java
public static String encryptData(String password, String plaintextData) throws Exception { // Thank you Mr. Nelenkov String maybeThisHelps = "http://nelenkov.blogspot.com/2012/04/using-password-based-encryption-on.html"; Log.v(TAG, maybeThisHelps);/*from w w w. ja v a2 s. c om*/ int iterationCount = 100; //because Polaroid int keyLength = 256; int saltLength = keyLength; SecureRandom random = new SecureRandom(); byte[] salt = new byte[saltLength]; random.nextBytes(salt); KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded(); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[cipher.getBlockSize()]; random.nextBytes(iv); IvParameterSpec ivParams = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivParams); byte[] ciphertext = cipher.doFinal(plaintextData.getBytes("UTF-8")); String ivToString = new String(Base64.encode(iv, 0)); String saltToString = new String(Base64.encode(salt, 0)); String ciphertextToString = new String(Base64.encode(ciphertext, 0)); Log.d(TAG, ivToString + "]" + saltToString + "]" + ciphertextToString); return (ivToString + "]" + saltToString + "]" + ciphertextToString).replace("\n", ""); }
From source file:org.wso2.carbon.identity.oidc.session.DefaultOIDCSessionStateManager.java
private static String generateSaltValue() throws NoSuchAlgorithmException { byte[] bytes = new byte[16]; SecureRandom secureRandom = SecureRandom.getInstance(RANDOM_ALG_SHA1); secureRandom.nextBytes(bytes); return Base64.encodeBase64URLSafeString(bytes); }
From source file:ru.org.linux.csrf.CSRFProtectionService.java
public static void generateCSRFCookie(HttpServletRequest request, HttpServletResponse response) { SecureRandom random = new SecureRandom(); byte[] value = new byte[16]; random.nextBytes(value); String token = new String(Base64.encodeBase64(value)); Cookie cookie = new Cookie(CSRF_COOKIE, token); cookie.setMaxAge(TWO_YEARS);//from w ww .jav a2 s. c o m cookie.setPath("/"); response.addCookie(cookie); request.setAttribute(CSRF_ATTRIBUTE, token); }
From source file:com.ec2box.manage.util.EncryptionUtil.java
/** * generate salt for hash// w w w . ja va 2 s . c om * * @return salt */ public static String generateSalt() { byte[] salt = new byte[32]; SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(salt); return new String(Base64.encodeBase64(salt)); }
From source file:com.github.woki.payments.adyen.action.CSEUtil.java
private static synchronized byte[] iv(SecureRandom random, int ivSize) { byte[] iv = new byte[ivSize]; random.nextBytes(iv); return iv;/* ww w . j ava 2 s . c om*/ }
From source file:com.zimbra.cs.account.PreAuthKey.java
public static String generateRandomPreAuthKey() throws ServiceException { try {/*from w w w. ja v a2s. c o m*/ SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] key = new byte[KEY_SIZE_BYTES]; random.nextBytes(key); return new String(Hex.encodeHex(key)); } catch (NoSuchAlgorithmException e) { throw ServiceException.FAILURE("unable to initialize SecureRandom", e); } }
From source file:de.tntinteractive.portalsammler.engine.CryptoHelper.java
public static byte[] generateKey(final SecureRandom srand) throws NoSuchAlgorithmException { final byte[] key = new byte[256 / 8]; srand.nextBytes(key); return key;//from www . j a va 2s . c o m }
From source file:com.apabi.r2k.common.security.util.NonceUtils.java
/** * SecureRandom?, Hex?.//w w w. ja v a2 s . c o m * * @param length ,?. */ public static String randomHexString(final int length) { SecureRandom nonceGenerator = new SecureRandom(); byte[] nonce = new byte[length / 2]; nonceGenerator.nextBytes(nonce); return Hex.encodeHexString(nonce); }
From source file:svnserver.ext.web.config.WebServerConfig.java
private static String generateDefaultSecret() { final SecureRandom random = new SecureRandom(); final byte bytes[] = new byte[EncryptionFactoryAes.KEY_SIZE]; random.nextBytes(bytes); return new String(Hex.encodeHex(bytes)); }
From source file:Main.java
public static String generateNonce() { try {//from w ww. j a v a 2 s.c o m // Create a secure random number generator SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); // Get 1024 random bits byte[] bytes = new byte[1024 / 8]; sr.nextBytes(bytes); // Create two secure number generators with the same seed int seedByteCount = 10; byte[] seed = sr.generateSeed(seedByteCount); sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(seed); SecureRandom sr2 = SecureRandom.getInstance("SHA1PRNG"); sr2.setSeed(seed); String nonce = Long.toHexString(sr2.nextLong()); nonce = nonce.substring(0, 7); return nonce; } catch (NoSuchAlgorithmException e) { } return null; }