byte[] toBePadded = inputData.getBytes(); RSAPadding padding = RSAPadding.getInstance(1, 512, SecureRandom.getInstanceStrong()); byte[] toBeSigned = padding.pad(toBePadded); byte[] opensslSignature = RSACore.rsa(toBeSigned, (RSAPrivateKey) privateKey, true);
byte[] bytes = new byte[20]; SecureRandom.getInstanceStrong().nextBytes(bytes);
private EntropyHarvester() throws Exception { if(Build.DEVICE != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { sr = SecureRandom.getInstanceStrong(); } else { sr = SecureRandom.getInstance("SHA1PRNG"); } md = MessageDigest.getInstance("SHA-512"); this.numberOfBytesGathered = BigInteger.ZERO; }
private static Random createSecureRandom() { try { return SecureRandom.getInstanceStrong(); } catch (NoSuchAlgorithmException nae) { LOGGER.warn("Couldn't create strong secure random generator; reason: {}.", nae.getMessage()); return new SecureRandom(); } }
/** * Initialize AES showing errors if failed */ private void initializeAES() { try { base64Encoder = new Base64Encoder(); random = SecureRandom.getInstanceStrong(); } catch (NoSuchAlgorithmException exc) { logger.error("Error initializing secure random", exc); } }
private static SecureRandom getSecureRandom(final boolean strong) { if (!strong) { return new SecureRandom(); } try { return SecureRandom.getInstanceStrong(); } catch (NoSuchAlgorithmException ex) { throw new RuntimeCryptoException( "could not get strong SecureRandom: " + ex.getMessage()); } }
/** * Generate a random RSA keypair. * * @param numBits * key's length in bits, should be power of 2) * @return * @throws NoSuchAlgorithmException */ public static KeyPair generateKeys(int numBits) throws NoSuchAlgorithmException { int numBitsPow2 = 1; while (numBitsPow2 < numBits) { numBitsPow2 <<= 1; } KeyPairGenerator kpg = KeyPairGenerator.getInstance(CIPHER_ALGORITHM); kpg.initialize(numBitsPow2, SecureRandom.getInstanceStrong()); KeyPair keyPair = kpg.generateKeyPair(); return keyPair; }
private static SecureRandom getSecureRandom(boolean strong) { if (!strong) { return new SecureRandom(); } try { return SecureRandom.getInstanceStrong(); } catch (NoSuchAlgorithmException ex) { throw new RuntimeCryptoException( "could not get strong SecureRandom: " + ex.getMessage()); } }
/** * Sign a message with RSA private key. * * @param key * @param message * the message to sign * @param signAlgo * signature algorithm to use. If empty, {@link #SIGNATURE_ALGORITHM} will be used. * @return the signature * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws SignatureException */ public static byte[] signMessage(RSAPrivateKey key, byte[] message, String signAlgo) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException { if (StringUtils.isBlank(signAlgo)) { signAlgo = SIGNATURE_ALGORITHM; } Signature sign = Signature.getInstance(signAlgo); sign.initSign(key, SecureRandom.getInstanceStrong()); sign.update(message); return sign.sign(); }
ByteArrayOutputStream osEnc = new ByteArrayOutputStream()) SecureRandom sr = SecureRandom.getInstanceStrong(); byte[] key = new byte[32]; // 32 for 256 bit key or 16 for 128 bit byte[] iv = new byte[8]; // 64 bit IV required by ChaCha20
@NonNull public RsaKeyPair generateNewKeyPair() { try { SecureRandom secureRandom = SecureRandom.getInstanceStrong(); return generateKeyPair(secureRandom); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
private void construct(byte[] val) { try { actualLength = val.length; SecureRandom rand = SecureRandom.getInstanceStrong(); byte[] keyBytes = new byte[24]; rand.nextBytes(keyBytes); byte[] ivBytes = new byte[8]; SecretKeySpec key = new SecretKeySpec(keyBytes, "DESede"); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); Cipher encrypter = Cipher.getInstance("DESede/CBC/PKCS5Padding"); decrypter = Cipher.getInstance("DESede/CBC/PKCS5Padding"); encrypter.init(Cipher.ENCRYPT_MODE, key, ivSpec); decrypter.init(Cipher.DECRYPT_MODE, key, ivSpec); encrypted = new byte[encrypter.getOutputSize(val.length)]; encLength = encrypter.update(val, 0, val.length, encrypted, 0); encLength += encrypter.doFinal(encrypted, encLength); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException ex) { throw new RuntimeException(ex); } }
public static void main(String[] args) throws NoSuchAlgorithmException { Date now = new Date(); /* use a strong random implementations, defined by: $JAVA_HOME/jre/lib/security/java.security : securerandom.strongAlgorithms= */ SecureRandom sr = SecureRandom.getInstanceStrong(); Logger.getGlobal().log(Level.INFO, "\nRequesting {0} random bytes", BYTES); byte[] b = new byte[BYTES]; /* request N random bytes. depending on the SecureRandom implementation, this can potentialy be a blocking request. this method only returns when the system has enough entropy. */ sr.nextBytes(b); System.out.println("\n" + Arrays.toString(b) + "\n"); Logger.getGlobal().log(Level.INFO, "Took {0} ms\n", (System.currentTimeMillis() - now.getTime())); }
SecureRandom sr = SecureRandom.getInstanceStrong(); byte[] data = new byte[1024*1024*4]; sr.nextBytes(data); SecureRandom sr = SecureRandom.getInstanceStrong(); byte[] key = new byte[32]; // 32 for 256 bit key or 16 for 128 bit byte[] iv = new byte[8]; // 64 bit IV required by ChaCha20
rng = SecureRandom.getInstanceStrong(); } catch (final NoSuchAlgorithmException e) { throw new RuntimeException("No strong secure random available to generate strong AES key", e);
"Unable to get interface MAC address for ID generation"); try { intfMac = ByteBuffer.allocate(Long.BYTES).putLong(SecureRandom.getInstanceStrong().nextLong()).array(); } catch (NoSuchAlgorithmException ex) { throw new IllegalStateException(ex); "Unable to get interface MAC address for ID generation"); try { intfMac = ByteBuffer.allocate(Long.BYTES).putLong(SecureRandom.getInstanceStrong().nextLong()).array(); } catch (NoSuchAlgorithmException ex) { throw new IllegalStateException(ex); "Unable to get interface MAC address for ID generation", ex); try { intfMac = ByteBuffer.allocate(Long.BYTES).putLong(SecureRandom.getInstanceStrong().nextLong()).array(); } catch (NoSuchAlgorithmException ex2) { ex2.addSuppressed(ex);
"Unable to get interface MAC address for ID generation"); try { intfMac = ByteBuffer.allocate(Long.BYTES).putLong(SecureRandom.getInstanceStrong().nextLong()).array(); } catch (NoSuchAlgorithmException ex) { throw new IllegalStateException(ex); "Unable to get interface MAC address for ID generation"); try { intfMac = ByteBuffer.allocate(Long.BYTES).putLong(SecureRandom.getInstanceStrong().nextLong()).array(); } catch (NoSuchAlgorithmException ex) { throw new IllegalStateException(ex); "Unable to get interface MAC address for ID generation", ex); try { intfMac = ByteBuffer.allocate(Long.BYTES).putLong(SecureRandom.getInstanceStrong().nextLong()).array(); } catch (NoSuchAlgorithmException ex2) { ex2.addSuppressed(ex);
seed = SecureRandom.getInstanceStrong().generateSeed(32); Files.write(keyFile, Base64.getEncoder().encode(seed), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); println("Key does not exist, creating... saving at " + keyFile.toString());
@Test public void testKSTS() throws Exception { RabbitConnectionFactoryBean fb = new RabbitConnectionFactoryBean(); Log logger = spy(TestUtils.getPropertyValue(fb, "logger", Log.class)); given(logger.isDebugEnabled()).willReturn(true); new DirectFieldAccessor(fb).setPropertyValue("logger", logger); fb.setUseSSL(true); fb.setKeyStoreType("JKS"); fb.setKeyStoreResource(new ClassPathResource("test.ks")); fb.setKeyStorePassphrase("secret"); fb.setTrustStoreResource(new ClassPathResource("test.truststore.ks")); fb.setKeyStorePassphrase("secret"); fb.setSecureRandom(SecureRandom.getInstanceStrong()); fb.afterPropertiesSet(); fb.getObject(); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(logger).debug(captor.capture()); final String log = captor.getValue(); assertThat(log, allOf(containsString("KM: ["), containsString("TM: ["), containsString("random: "))); }
public static void main(final String[] argv) throws NoSuchAlgorithmException, IOException { final NetworkParameters params = new MainNetParams(); final LocalTransactionSigner signer = new LocalTransactionSigner(); final SecureRandom secureRandom = SecureRandom.getInstanceStrong(); final ECKey key = new ECKey(secureRandom); final KeyBag bag = new KeyBag() {