Java Code Examples for java.security.SecureRandom#setSeed()
The following examples show how to use
java.security.SecureRandom#setSeed() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AES.java From AndroidStudyDemo with GNU General Public License v2.0 | 6 votes |
/** * 获取256位的加密密钥 * @param seed * @return * @throws Exception */ @SuppressLint("TrulyRandom") private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = null; // 在4.2以上版本中,SecureRandom获取方式发生了改变 if (android.os.Build.VERSION.SDK_INT >= JELLY_BEAN_4_2) { sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); } else { sr = SecureRandom.getInstance("SHA1PRNG"); } sr.setSeed(seed); // 256 bits or 128 bits,192bits kgen.init(256, sr); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }
Example 2
Source File: GoogleAuthCode.java From zheshiyigeniubidexiangmu with MIT License | 6 votes |
/** * Generate a random secret key. This must be saved by the server and * associated with the users account to verify the code displayed by Google * Authenticator. The user must register this secret on their device. * 生成一个随机秘钥 * * @return secret key */ public static String generateSecretKey() { SecureRandom sr = null; try { sr = SecureRandom.getInstance(RANDOM_NUMBER_ALGORITHM); sr.setSeed(Base64.decodeBase64(SEED)); byte[] buffer = sr.generateSeed(SECRET_SIZE); Base32 codec = new Base32(); byte[] bEncodedKey = codec.encode(buffer); String encodedKey = new String(bEncodedKey); return encodedKey; } catch (NoSuchAlgorithmException e) { // should never occur... configuration error } return null; }
Example 3
Source File: CredentialEncrypter.java From emodb with Apache License 2.0 | 6 votes |
private SecretKey createKey(byte[] initializationBytes) { try { // Create the encryption key from a SecureRandom seeded with the secret SEED value and initialization bytes. // This way different initialization values will generate different keys. SecureRandom random = SecureRandom.getInstance(SEED_PRNG); random.setSeed(BaseEncoding.base64().decode(SEED)); random.setSeed(initializationBytes); byte[] keyBytes = new byte[16]; random.nextBytes(keyBytes); return new SecretKeySpec(keyBytes, ALGORITHM); } catch (NoSuchAlgorithmException e) { // This shouldn't happen since SHA1PRNG is supported by all JVMs. throw Throwables.propagate(e); } }
Example 4
Source File: AESUtil.java From datax-web with MIT License | 6 votes |
/** * 生成加密秘钥 * * @return * @throws NoSuchAlgorithmException */ private static KeyGenerator getKeyGenerator() { String key = JobAdminConfig.getAdminConfig().getDataSourceAESKey(); KeyGenerator keygen = null; try { keygen = KeyGenerator.getInstance(KEY_ALGORITHM); SecureRandom secureRandom = SecureRandom.getInstance(DEFAULT_CIPHER_ALGORITHM); secureRandom.setSeed(key.getBytes()); keygen.init(128, secureRandom); } catch (NoSuchAlgorithmException e) { log.warn("Get key generator error {}", e.getMessage()); } return keygen; }
Example 5
Source File: StatInterfaceDatabase.java From Rumble with GNU General Public License v3.0 | 5 votes |
private String generateRandomSalt(int length) { SecureRandom random = new SecureRandom(); random.setSeed(System.currentTimeMillis()); byte[] buf = new byte[length]; random.nextBytes(buf); return Base64.encodeToString(buf,0,length,Base64.NO_WRAP); }
Example 6
Source File: SelfSeed.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) { try { SecureRandom sr1 = SecureRandom.getInstance("SHA1PRNG"); sr1.setSeed(seed); byte randomBytes[] = new byte[NUM_BYTES]; sr1.nextBytes(randomBytes); SecureRandom sr2 = new SecureRandom(seed); if (sr2.getAlgorithm().equals("SHA1PRNG") == false) { System.out.println("Default PRNG is not SHA1PRNG, skipping test"); return; } byte otherRandomBytes[] = new byte[NUM_BYTES]; sr2.nextBytes(otherRandomBytes); // make sure the random bytes generated are the same for (int i = 0; i < NUM_BYTES; i++) { if (randomBytes[i] != otherRandomBytes[i]) throw new SecurityException("FAILURE: " + "Returned bytes not equal"); } // success } catch (Exception e) { throw new SecurityException("FAILURE: " + e.toString()); } }
Example 7
Source File: BosCrypto.java From zhangshangwuda with Apache License 2.0 | 5 votes |
private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = null; if (android.os.Build.VERSION.SDK_INT >= 17) { sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); } else { sr = SecureRandom.getInstance("SHA1PRNG"); } sr.setSeed(seed); kgen.init(128, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }
Example 8
Source File: IdsRepository.java From aptoide-client-v8 with GNU General Public License v3.0 | 5 votes |
private String generateRandomAdvertisingId() { byte[] data = new byte[16]; String androidId = this.androidId; if (androidId == null) { androidId = UUID.randomUUID() .toString(); } SecureRandom secureRandom = new SecureRandom(); secureRandom.setSeed(androidId.hashCode()); secureRandom.nextBytes(data); return UUID.nameUUIDFromBytes(data) .toString(); }
Example 9
Source File: SettingConfig.java From Alice-LiveMan with GNU Affero General Public License v3.0 | 5 votes |
private Cipher getCipher(String key, int mode) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { KeyGenerator keygen = KeyGenerator.getInstance("AES"); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(key.getBytes()); keygen.init(128, secureRandom); SecretKey originalKey = keygen.generateKey(); byte[] raw = originalKey.getEncoded(); SecretKey secretKey = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(mode, secretKey); return cipher; }
Example 10
Source File: ApiKeyGenerator.java From Alpine with Apache License 2.0 | 5 votes |
/** * Generates a cryptographically secure API key of the specified length. * @param chars the length of the API key to generate * @return a String representation of the API key */ public static String generate(final int chars) { final SecureRandom secureRandom = new SecureRandom(); final char[] buff = new char[chars]; for (int i = 0; i < chars; ++i) { if (i % 10 == 0) { secureRandom.setSeed(secureRandom.nextLong()); } buff[i] = VALID_CHARACTERS[secureRandom.nextInt(VALID_CHARACTERS.length)]; } return new String(buff); }
Example 11
Source File: FakeRandom.java From keywhiz with Apache License 2.0 | 5 votes |
/** @return a SecureRandom instance that always produces the same stream. */ public static SecureRandom create() { try { SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(4); return random; } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Algorithm SHA1PRNG does not exist"); } }
Example 12
Source File: EncryptionTests.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
public byte[] generateKeyData() throws NoSuchAlgorithmException { SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(System.currentTimeMillis()); byte bytes[] = new byte[DESedeKeySpec.DES_EDE_KEY_LEN]; random.nextBytes(bytes); return bytes; }
Example 13
Source File: AESUtil.java From taoshop with Apache License 2.0 | 5 votes |
/** * 加密 * @param content * @param encryptKey * @return * @throws Exception */ public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); //防止linux下 随机生成key SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" ); secureRandom.setSeed(encryptKey.getBytes()); kgen.init(128, secureRandom); SecretKey secretKey = kgen.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES");// 创建密码器 byte[] byteContent = content.getBytes("utf-8"); cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 return cipher.doFinal(byteContent); }
Example 14
Source File: AESEncryptor.java From elemeimitate with Apache License 2.0 | 5 votes |
private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(seed); kgen.init(128, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }
Example 15
Source File: Crypto.java From PowerFileExplorer with GNU General Public License v3.0 | 5 votes |
/** * byte[] key = Crypto.generateKey("randomtext".getBytes()); * outputStream.write(encrypt(key,contact.getBytes())); */ public static byte[] generateKey(byte[] randomNumberSeed) { SecretKey sKey = null; try { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(randomNumberSeed); keyGen.init(256,random); sKey = keyGen.generateKey(); } catch (NoSuchAlgorithmException e) { Log.e(TAG,"No such algorithm exception"); } return sKey.getEncoded(); }
Example 16
Source File: CryptUtil.java From iSCAU-Android with GNU General Public License v3.0 | 5 votes |
private byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = null; if (android.os.Build.VERSION.SDK_INT >= JELLY_BEAN_4_2) { sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); } else { sr = SecureRandom.getInstance("SHA1PRNG"); } sr.setSeed(seed); kgen.init(128, sr); // 192 and 256 bits may not be available SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }
Example 17
Source File: GoogleAuthenticatorUtil.java From ZTuoExchange_framework with MIT License | 5 votes |
public static String generateSecretKey() { SecureRandom sr = null; try { sr = SecureRandom.getInstance(RANDOM_NUMBER_ALGORITHM); sr.setSeed(Base64.decodeBase64(SEED)); byte[] buffer = sr.generateSeed(SECRET_SIZE); Base32 codec = new Base32(); byte[] bEncodedKey = codec.encode(buffer); String encodedKey = new String(bEncodedKey); return encodedKey; }catch (NoSuchAlgorithmException e) { // should never occur... configuration error } return null; }
Example 18
Source File: MacNativePRNGSetSeed.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Exception { SecureRandom sr = SecureRandom.getInstance("NativePRNG"); sr.setSeed(1); }
Example 19
Source File: MacNativePRNGSetSeed.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Exception { SecureRandom sr = SecureRandom.getInstance("NativePRNG"); sr.setSeed(1); }
Example 20
Source File: DynRH2Lev.java From Clusion with GNU General Public License v3.0 | 3 votes |
public static TreeMultimap<String, byte[]> tokenUpdate(byte[] key, Multimap<String, String> lookup) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, IOException { TreeMultimap<String, byte[]> tokenUp = TreeMultimap.create(Ordering.natural(), Ordering.usingToString()); SecureRandom random = new SecureRandom(); random.setSeed(CryptoPrimitives.randomSeed(16)); byte[] iv = new byte[16]; for (String word : lookup.keySet()) { byte[] key3 = CryptoPrimitives.generateCmac(key, 3 + new String()); byte[] key4 = CryptoPrimitives.generateCmac(key, 4 + word); for (String id : lookup.get(word)) { random.nextBytes(iv); int counter = 0; if (state.get(word) != null) { counter = state.get(word); } state.put(word, counter + 1); byte[] l = CryptoPrimitives.generateCmac(key4, "" + counter); byte[] value = CryptoPrimitives.encryptAES_CTR_String(key3, iv, id, sizeOfFileIdentifer); tokenUp.put(new String(l), value); } } return tokenUp; }