public AlternativeJdkIdGenerator() { SecureRandom secureRandom = new SecureRandom(); byte[] seed = new byte[8]; secureRandom.nextBytes(seed); this.random = new Random(new BigInteger(seed).longValue()); }
private static String getSalt() throws NoSuchAlgorithmException { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); byte[] salt = new byte[16]; sr.nextBytes(salt); return Arrays.toString(salt); }
protected void init() { try { if (iv == null) { iv = new byte[getIvLength()]; rng.nextBytes(iv); } cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (InvalidAlgorithmParameterException e) { throw new RuntimeException(e); } initialized = true; }
@Override public void subscribe(ObservableEmitter<EncryptionResult> emitter) throws Exception { if (initException==null) { createKey(keyName, timeout); SecretKey secretKey=(SecretKey)ks.getKey(keyName, null); Cipher cipher=Cipher.getInstance("AES/CBC/PKCS7Padding"); SecureRandom rand=new SecureRandom(); byte[] iv=new byte[BLOCK_SIZE]; rand.nextBytes(iv); IvParameterSpec ivParams=new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParams); emitter.onNext(new EncryptionResult(ivParams.getIV(), cipher.doFinal(toEncrypt))); } else { throw initException; } }
/** * Creates a SecureRandomPool using the specified algorithm. The provided * SecureRandom is used to seed each new SecureRandom in the pool. * @param algorithm * @param seed */ public SecureRandomPool(String algorithm, int poolSize, SecureRandom seed) { if (algorithm == null) { throw new IllegalArgumentException("algorithm is null"); } pool = new ArrayList<SecureRandom>(poolSize); seedSource = getSeedSource(algorithm, (seed != null) ? seed : new SecureRandom()); try { for (int i=0; i<poolSize; i++) { byte[] seedBytes = new byte[20]; seedSource.nextBytes(seedBytes); SecureRandom random = SecureRandom.getInstance(algorithm); // (authorized) random.setSeed(seedBytes); pool.add(random); } } catch (NoSuchAlgorithmException e) { log.error("Error getting SecureRandom using {} algorithm.", algorithm, e); throw new RuntimeException(String.format("Error getting SecureRandom using %s algorithm.", algorithm), e); } }
final SecureRandom rnd = SecureRandom.getInstance("SHA1PRNG"); rnd.nextBytes(ivData); final IvParameterSpec iv = new IvParameterSpec(ivData); final IvParameterSpec iv = new IvParameterSpec(ivData);
public static byte[] generateRandomBytes(int size) { SecureRandom ng=numberGenerator; if(ng == null) numberGenerator=ng=new SecureRandom(); byte[] randomBytes=new byte[size]; ng.nextBytes(randomBytes); return randomBytes; }
private void create(KeyStore ks, ObservableEmitter<char[]> emitter) throws Exception { SecureRandom rand=new SecureRandom(); char[] passphrase=new char[128]; for (int i=0; i<passphrase.length; i++) { passphrase[i]=BASE36_SYMBOLS.charAt(rand.nextInt(BASE36_SYMBOLS.length())); } createKey(ks, keyName, timeout); SecretKey secretKey=(SecretKey)ks.getKey(keyName, null); Cipher cipher=Cipher.getInstance("AES/CBC/PKCS7Padding"); byte[] iv=new byte[BLOCK_SIZE]; rand.nextBytes(iv); IvParameterSpec ivParams=new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParams); byte[] toEncrypt=toBytes(passphrase); byte[] encrypted=cipher.doFinal(toEncrypt); BufferedSink sink=Okio.buffer(Okio.sink(encryptedFile)); sink.write(iv); sink.write(encrypted); sink.close(); emitter.onNext(passphrase); }
public SecureRandom getObject() throws Exception { SecureRandom rnd = SecureRandom.getInstance(algorithm); if (seed != null) { // Seed specified, so use it byte[] seedBytes = FileCopyUtils.copyToByteArray(seed.getInputStream()); rnd.setSeed(seedBytes); } else { // Request the next bytes, thus eagerly incurring the expense of default // seeding rnd.nextBytes(new byte[1]); } return rnd; }
cipher.init(Cipher.DECRYPT_MODE, secretSpec, new IvParameterSpec(ivBytes)); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); byte[] salt = new byte[20]; sr.nextBytes(salt); return new String(salt);
random.nextBytes(challenge); IvParameterSpec ivps = new IvParameterSpec(sKeyBytes, keysize, blocksize);
/** * Generate a 16 byte initialization vector for AES encryption. * * @return iv */ public static byte[] generateIv() { SecureRandom random = new SecureRandom(); byte[] iv = new byte[16]; random.nextBytes(iv); return iv; } }
new SecureRandom().nextBytes(iv); try { Cipher c = Secret.getCipher("AES/CFB8/NoPadding"); c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); encrypted = c.doFinal(csos.getBytes()); } catch (GeneralSecurityException x) {
public static EncryptionContext create(String secretKey) throws NoSuchAlgorithmException { SecureRandom randomSecureRandom = SecureRandom.getInstance("SHA1PRNG"); byte[] iv = new byte[16]; randomSecureRandom.nextBytes(iv); return new EncryptionContext(secretKey, iv); } }
SecretKeySpec skeySpec = new SecretKeySpec(getCryptoKeyByteArray(length=16)); Cipher encryptor = Cipher.getInstance("AES/CTR/NoPadding"); // Initialisation vector: byte[] iv = new byte[encryptor.getBlockSize()]; SecureRandom.getInstance("SHA1PRNG").nextBytes(iv); // If storing separately IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); encryptor.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); byte[] encrypted = encryptor.doFinal(plain);
public static String encrypt(String text, LazySecretKey lazySecretKey) throws Exception { SecretKey secretKey = lazySecretKey.getOrCreate(); byte[] iv = new byte[16]; secureRandom.nextBytes(iv); Cipher aesCipherForEncryption = Cipher.getInstance("AES/CBC/PKCS5Padding"); aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv)); byte[] encryptedBytes = aesCipherForEncryption.doFinal(text.getBytes(UTF_8)); return encoder.encode(encryptedBytes) + ':' + encoder.encode(iv); }
@Override public String generate() { SecureRandom random = new SecureRandom(); byte[] bytes = new byte[20]; random.nextBytes(bytes); return Hex.encodeHexString(bytes); }
final Cipher ec = Cipher.getInstance(algorithm); final byte[] bytes = new byte[16]; // initialization vector final SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(bytes); final KeyGenerator generator = KeyGenerator.getInstance("AES"); generator.init(128); final IvParameterSpec algorithmParameterSpec = new IvParameterSpec(bytes); ec.init(Cipher.ENCRYPT_MODE, generator.generateKey(), algorithmParameterSpec, secureRandom); final byte[] raw = new byte[0];
public SecureRandom getObject() throws Exception { SecureRandom rnd = SecureRandom.getInstance(algorithm); if (seed != null) { // Seed specified, so use it byte[] seedBytes = FileCopyUtils.copyToByteArray(seed.getInputStream()); rnd.setSeed(seedBytes); } else { // Request the next bytes, thus eagerly incurring the expense of default // seeding rnd.nextBytes(new byte[1]); } return rnd; }
@Override public OutputStream encryptStream(OutputStream outputStream) throws CryptoException { sr.nextBytes(initVector); try { outputStream.write(initVector); } catch (IOException e) { throw new CryptoException("Unable to write IV to stream", e); } Cipher cipher; try { cipher = Cipher.getInstance(transformation); cipher.init(Cipher.ENCRYPT_MODE, fek, new IvParameterSpec(initVector)); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) { throw new CryptoException("Unable to initialize cipher", e); } CipherOutputStream cos = new CipherOutputStream(outputStream, cipher); return new BlockedOutputStream(cos, cipher.getBlockSize(), 1024); }