List of usage examples for java.security SecureRandom SecureRandom
public SecureRandom()
From source file:crocserver.app.CrocSecurity.java
public static String createSecret() { byte[] bytes = new byte[10]; new SecureRandom().nextBytes(bytes); return new Base32().encodeAsString(bytes); }
From source file:com.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java
public static String encrypt(String data, String password) throws Exception { Security.addProvider(new BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); final Random r = new SecureRandom(); byte[] salt = new byte[SALT_SIZE]; r.nextBytes(salt);//w w w . j a va 2 s.c o m SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC"); cipher.init(Cipher.ENCRYPT_MODE, fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE))); byte[] encVal = cipher.doFinal(data.getBytes()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); // writing encrypted data along with the salt in the format readable by // open ssl api bos.write("Salted__".getBytes()); bos.write(salt); bos.write(encVal); String encryptedValue = new String(Base64.encode(bos.toByteArray())); bos.close(); return encryptedValue; }
From source file:Logi.GSeries.Libraries.Encryption.java
public static String encrypt(String decryptedString, String password) { try {//from w w w. ja va 2s . com // build the initialization vector (randomly). SecureRandom random = new SecureRandom(); byte initialVector[] = new byte[16]; //generate random 16 byte IV AES is always 16bytes random.nextBytes(initialVector); IvParameterSpec ivspec = new IvParameterSpec(initialVector); SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec); byte[] encrypted = cipher.doFinal(decryptedString.getBytes()); byte[] encryptedWithIV = new byte[encrypted.length + initialVector.length]; System.arraycopy(encrypted, 0, encryptedWithIV, 0, encrypted.length); System.arraycopy(initialVector, 0, encryptedWithIV, encrypted.length, initialVector.length); return Base64.encodeBase64String(encryptedWithIV); } catch (Exception ex) { Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex); return "Error"; } }
From source file:com.iterzp.momo.utils.RSAUtils.java
/** * ?//from w ww . j av a 2s . co m * * @return */ public static KeyPair generateKeyPair() { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", PROVIDER); keyPairGenerator.initialize(KEY_SIZE, new SecureRandom()); return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:Main.java
/** * Decrypts a encrypted and encoded message given a key. * @param cipherBytes//from w w w . j a v a 2 s .co m * @param key * @return * @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidAlgorithmParameterException * @throws NoSuchPaddingException * @throws InvalidKeyException */ public static byte[] decryptMessage(byte[] cipherBytes, SecretKey key) throws NoSuchAlgorithmException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException { Cipher cipher = Cipher.getInstance("AES"); //ipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING"); byte[] init = new byte[128 / 8]; SecureRandom secureRandom = new SecureRandom(); //secureRandom.nextBytes(init); for (int i = 0; i < 16; i++) init[i] = 0; cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(init)); byte[] textBytes = cipher.doFinal(cipherBytes); return textBytes; }
From source file:Main.java
/** * Generate a random String// w ww . j a v a2s . c om * * @param values The characters list to use in the randomization * @param len The number of characters in the output String * @return The randomized String */ public static String randomString(char[] values, int len) { Random rnd = new SecureRandom(); StringBuilder sb = new StringBuilder(len); for (int i = 0; i < len; i++) { sb.append(values[rnd.nextInt(values.length)]); } return sb.toString(); }
From source file:com.DSC.client.SecureChannel.java
/** * @param args/*from ww w . j a v a 2 s .c o m*/ * @throws InterruptedException * @throws IOException */ public static void main(String[] args) throws InterruptedException, IOException { /* Create their private & public keys */ ECKey key = new ECKey(); key.init(); ProgramState.publicKey = (ECPublicKeyParameters) key.getPublic(); ProgramState.privateKey = (ECPrivateKeyParameters) key.getPrivate(); /* Create the IV engine */ byte[] seed = new byte[64]; // 512 bit seed SecureRandom random = new SecureRandom(); random.nextBytes(seed); ProgramState.IVEngine = new ISAACRandomGenerator(new ISAACEngine()); ProgramState.IVEngine.init(seed); /* Create the blacklist and trusted contacts */ ProgramState.blacklist = ConcurrentHashMultiset.create(); ProgramState.trustedKeys = new ConcurrentHashMap<String, Address>(); /* Set the time for the client accurately using a NTP server */ DateTimeUtils.setCurrentMillisOffset(getTimeOffset()); ProgramState.fmt = DateTimeFormat.forPattern("HH:mm:ss"); /* Set the default nick as anonymous */ ProgramState.nick = "anonymous"; /* Initialize ISAACRandomGenerator, set ProgramState.IVEngine */ receiveController = new ReceiveController(); sendController = new SendController(); /* Start input event handler loop */ eventLoop(); }
From source file:MainClass.java
private static byte[] passwordEncrypt(char[] password, byte[] plaintext) throws Exception { int MD5_ITERATIONS = 1000; byte[] salt = new byte[8]; SecureRandom random = new SecureRandom(); random.nextBytes(salt);/* w ww .j a v a2 s. c o m*/ PBEKeySpec keySpec = new PBEKeySpec(password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC"); SecretKey key = keyFactory.generateSecret(keySpec); PBEParameterSpec paramSpec = new PBEParameterSpec(salt, MD5_ITERATIONS); Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC"); cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); byte[] ciphertext = cipher.doFinal(plaintext); ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(salt); baos.write(ciphertext); return baos.toByteArray(); }
From source file:Main.java
public static KeyPair genKeyPair() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, InvalidKeyException, SignatureException { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", "BC"); keyPairGen.initialize(1024, new SecureRandom()); KeyPair keypair = keyPairGen.genKeyPair(); return keypair; }
From source file:Main.java
private static boolean passesMillerRabin(BigInteger us, int iterations, Random rnd) { final BigInteger ONE = BigInteger.ONE; final BigInteger TWO = BigInteger.valueOf(2); // Find a and m such that m is odd and this == 1 + 2**a * m BigInteger thisMinusOne = us.subtract(ONE); BigInteger m = thisMinusOne;//from www . j a va 2 s . com int a = m.getLowestSetBit(); m = m.shiftRight(a); // Do the tests if (rnd == null) { rnd = new SecureRandom(); } for (int i = 0; i < iterations; i++) { // Generate a uniform random on (1, this) BigInteger b; do { b = new BigInteger(us.bitLength(), rnd); } while (b.compareTo(ONE) <= 0 || b.compareTo(us) >= 0); int j = 0; BigInteger z = b.modPow(m, us); while (!((j == 0 && z.equals(ONE)) || z.equals(thisMinusOne))) { if (j > 0 && z.equals(ONE) || ++j == a) return false; z = z.modPow(TWO, us); } } return true; }