public AlternativeJdkIdGenerator() { SecureRandom secureRandom = new SecureRandom(); byte[] seed = new byte[8]; secureRandom.nextBytes(seed); this.random = new Random(new BigInteger(seed).longValue()); }
private void processHandshakeFailure(Socket raw) throws Exception { SSLContext context = SSLContext.getInstance("TLS"); context.init(null, new TrustManager[] {UNTRUSTED_TRUST_MANAGER}, new SecureRandom()); SSLSocketFactory sslSocketFactory = context.getSocketFactory(); SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket( raw, raw.getInetAddress().getHostAddress(), raw.getPort(), true); try { socket.startHandshake(); // we're testing a handshake failure throw new AssertionError(); } catch (IOException expected) { } socket.close(); }
private static SSLContext sslContext(String keystoreFile, String password) throws GeneralSecurityException, IOException { KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); try (InputStream in = new FileInputStream(keystoreFile)) { keystore.load(in, password.toCharArray()); } KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keystore, password.toCharArray()); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keystore); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init( keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom()); return sslContext; } }
private AccessTokenBuilder() { try { // This operation may block on some systems with low entropy. See // this page for workaround suggestions: // http://docs.codehaus.org/display/JETTY/Connectors+slow+to+startup // System.out.println("Init SecureRandom."); random = new SecureRandom(); weakRandom = false; } catch (Exception e) { random = new Random(); weakRandom = true; System.err.println("Could not generate SecureRandom for accessToken randomness"); } }
System.out.println ("** Crypt ** " + msg); SecureRandom rnd = new SecureRandom (); rnd.nextBytes (mSalt); Db ("generated salt :" + Hex.encodeHexString (mSalt)); mDecipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(mInitVec)); System.out.println ("decryption finished to " + doutput.getName ());
static SecureRandom rnd = new SecureRandom(); static IvParameterSpec iv = new IvParameterSpec(rnd.generateSeed(16)); System.out.println("decrypted value:" + (decrypt("ThisIsASecretKey", encrypt))); cipher.init(Cipher.ENCRYPT_MODE, skeySpec,iv); byte[] encrypted = cipher.doFinal(value.getBytes()); System.out.println("encrypted string:" + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (Exception ex) {
@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; } }
public SSLContext sslContext() { try { SSLContext sslContext = Platform.get().getSSLContext(); sslContext.init(new KeyManager[] { keyManager }, new TrustManager[] { trustManager }, new SecureRandom()); return sslContext; } catch (KeyManagementException e) { throw new AssertionError(e); } }
SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCertificates, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(trustAllHostnames);
private SessionIdKit() { try { // This operation may block on some systems with low entropy. See // this page // for workaround suggestions: // http://docs.codehaus.org/display/JETTY/Connectors+slow+to+startup System.out.println("Init SecureRandom."); random = new SecureRandom(); weakRandom = false; } catch (Exception e) { System.err.println("Could not generate SecureRandom for session-id randomness"); random = new Random(); weakRandom = true; } }
new SecureRandom().nextBytes(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv); System.out.println("encrypted: " + new String(encrypted)); System.out.println("decrypted: " + new String(decrypted, "UTF-8"));
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); }
/** * Generates a new, initialized SSLContext instance that is suitable for connections that are created based on a * particular configuration. * * @return TrustManagers applicable to a connection that is established using the provided configuration. */ public synchronized SSLContext getSSLContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { final SSLContext sslContext = SSLContext.getInstance("TLSv1"); sslContext.init( getKeyManagers(), getTrustManagers(), new SecureRandom() ); return sslContext; }
SSLSocketFactory newFactory(TrustManager... managers) throws NoSuchAlgorithmException, KeyManagementException { SSLContext context = SSLContext.getInstance("TLS"); context.init(null, managers, new SecureRandom()); return context.getSocketFactory(); } }
public static void main(String[] args) throws DigestException, NoSuchAlgorithmException, IOException { SecureRandom secureRandom = new SecureRandom(); if (!new File("keys").exists()) { new File("keys").mkdir(); } for (int i = 0; i < 4; i++) { File pubFile = new File("keys/actor-key-" + i + ".pub"); File keyFile = new File("keys/actor-key-" + i + ".key"); if (pubFile.exists() && keyFile.exists()) { System.out.println("Key #" + i + " exists. Skipping..."); continue; } Curve25519KeyPair keyPair = Curve25519.keyGen(secureRandom.generateSeed(64)); FileUtils.writeByteArrayToFile(pubFile, keyPair.getPublicKey()); FileUtils.writeByteArrayToFile(keyFile, keyPair.getPrivateKey()); } System.out.println("Shared Secret: " + Base64.getEncoder().encodeToString(secureRandom.generateSeed(64))); } }
} else if (rfile.length() == 0L) { if (sr == null) sr = new SecureRandom(); sr.nextBytes(iv); rfile.write(iv); } else { rfile.read(lastBlockEnc); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); lastBlock = cipher.doFinal(lastBlockEnc); rfile.seek(rfile.length()-16); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv)); byte[] out; if (lastBlock != null) { }; Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv)); CipherInputStream cis = new CipherInputStream(fin,cipher); return cis; System.out.println(bread.readLine()); cis.close();
@Override public String generate() { SecureRandom random = new SecureRandom(); byte[] bytes = new byte[20]; random.nextBytes(bytes); return Hex.encodeHexString(bytes); }
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 SSLContext createSslContext(boolean allowInsecureConnection, Certificate[] trustCertficates, Certificate[] certificates, PrivateKey privateKey) throws GeneralSecurityException { KeyStoreHolder ksh = new KeyStoreHolder(); TrustManager[] trustManagers = null; KeyManager[] keyManagers = null; trustManagers = setupTrustCerts(ksh, allowInsecureConnection, trustCertficates); keyManagers = setupKeyManager(ksh, privateKey, certificates); SSLContext sslCtx = SSLContext.getInstance("TLS"); sslCtx.init(keyManagers, trustManagers, new SecureRandom()); sslCtx.getDefaultSSLParameters(); return sslCtx; }