List of usage examples for java.security SecureRandom SecureRandom
public SecureRandom()
From source file:Main.java
/** * Generates a random token./*from w w w. j a v a 2s . co m*/ * * @return A nonce, with a length of 32, to be used with the {@link SafetyNet} request. */ public static byte[] generateOneTimeNonce() { if (secureRandom == null) { secureRandom = new SecureRandom(); } byte[] nonce = new byte[32]; secureRandom.nextBytes(nonce); return nonce; }
From source file:com.lingxiang2014.util.RSAUtils.java
public static KeyPair generateKeyPair() { try {//from w ww. j a v a 2 s . c o m 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
public static BigInteger createPrimeBigger(BigInteger valueThatDeterminesNumberOfBits) { Random random = new SecureRandom(); return createPrimeBigger(valueThatDeterminesNumberOfBits, random); }
From source file:com.gmu.uav.RadioSecurity.java
public static String computeSHA256(String password) { MessageDigest messagDigest = null; String saltedPassword = null; SecureRandom random = new SecureRandom(); byte[] salt = new byte[SALT_BYTES]; random.nextBytes(salt);/*from ww w.j a v a 2 s. com*/ saltedPassword = salt + password; try { messagDigest = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } messagDigest.update(saltedPassword.getBytes()); byte finalHash[] = messagDigest.digest(); return HASH_ITERATIONS + ":" + toHex(salt) + ":" + toHex(finalHash); }
From source file:Main.java
public static BigInteger createPrimeBigger(BigInteger valueThatDeterminesNumberOfBits) { int numbits = valueThatDeterminesNumberOfBits.bitLength() + 1; Random random = new SecureRandom(); BigInteger ret = BigInteger.probablePrime(numbits, random); return ret;/*from www .j ava2 s .c om*/ }
From source file:Main.java
public static byte[] DESTemplet(byte[] data, byte[] key, String algorithm, String transformation, boolean isEncrypt) { try {/*from w ww. j a va2s. c o m*/ SecretKeySpec keySpec = new SecretKeySpec(key, algorithm); Cipher cipher = Cipher.getInstance(transformation); SecureRandom random = new SecureRandom(); cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random); return cipher.doFinal(data); } catch (Throwable e) { e.printStackTrace(); } return null; }
From source file:Main.java
static KeyPair generateRsaKeyPair(int keySizeInBits) { if (keySizeInBits == 0 || keySizeInBits % 8 != 0) return null; try {/*from ww w . j av a 2 s . c om*/ KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "SC"); generator.initialize(keySizeInBits, new SecureRandom()); KeyPair pair = generator.generateKeyPair(); // Log.d("","public : "+keyToBase64(pair.getPublic())); // Log.d("","private: "+keyToBase64(pair.getPrivate())); return pair; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static byte[] desTemplate(byte[] data, byte[] key, String algorithm, String transformation, boolean isEncrypt) { if (data == null || data.length == 0 || key == null || key.length == 0) return null; try {//from w w w. j a v a2s.co m SecretKeySpec keySpec = new SecretKeySpec(key, algorithm); Cipher cipher = Cipher.getInstance(transformation); SecureRandom random = new SecureRandom(); cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random); return cipher.doFinal(data); } catch (Throwable e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static String getJwkPublic(KeyPair kp) { try {/*from ww w . j a va 2 s . c o m*/ JSONObject jk = new JSONObject(); jk.put("kty", "RSA"); // generate random kid SecureRandom random = new SecureRandom(); String kid = new BigInteger(130, random).toString(32); jk.put("kid", kid); jk.put("e", "AQAB"); KeyFactory kfactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); jk.put("n", encodeB64(kspec.getModulus().toByteArray())); JSONArray ja = new JSONArray(); ja.put(jk); JSONObject jo = new JSONObject(); jo.put("keys", ja); // Log.d("getJwkPublic key: ",pubkey.toString()); // Log.d("getJwkPublic jwk: ",jo.toString()); return jo.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String getJwkPrivate(KeyPair kp) { try {//from w w w .j a v a 2 s. c om JSONObject jk = new JSONObject(); jk.put("kty", "RSA"); // generate random kid SecureRandom random = new SecureRandom(); String kid = new BigInteger(130, random).toString(32); jk.put("kid", kid); jk.put("e", "AQAB"); KeyFactory kfactory = KeyFactory.getInstance("RSA"); RSAPrivateKeySpec privkspec = (RSAPrivateKeySpec) kfactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class); RSAPublicKeySpec pubkspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); // Log.d("getJwkPrivate n",pubkspec.getPublicExponent().toString()); // Log.d("getJwkPrivate d",privkspec.getPrivateExponent().toString()); jk.put("n", encodeB64(pubkspec.getModulus().toByteArray())); jk.put("d", encodeB64(privkspec.getPrivateExponent().toByteArray())); JSONArray ja = new JSONArray(); ja.put(jk); JSONObject jo = new JSONObject(); jo.put("keys", ja); return jo.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }