Example usage for java.security SecureRandom setSeed

List of usage examples for java.security SecureRandom setSeed

Introduction

In this page you can find the example usage for java.security SecureRandom setSeed.

Prototype

@Override
public void setSeed(long seed) 

Source Link

Document

Reseeds this random object, using the eight bytes contained in the given long seed .

Usage

From source file:com.healthcit.cacure.utils.PasswordService.java

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;//from   ww w .j  a v a2  s.  c  om
}

From source file:com.pingidentity.adapters.idp.mobileid.restservice.MssRequestHandlerRest.java

/**
 * Creates a random transaction id beginning with 'pf'
 * // w ww.j  a  va 2s  .  c  om
 * @param digits
 *            number of digits without 'pf'
 * @return the generated transaction id
 */
public static String createTransId(int digits) {

    final String VALUES = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    SecureRandom rand = new SecureRandom();
    rand.setSeed(System.currentTimeMillis());
    StringBuffer randBuffer = new StringBuffer("pf");
    for (int i = 0; i < digits; i++) {
        randBuffer.append(VALUES.charAt(rand.nextInt(VALUES.length())));
    }
    return randBuffer.toString();
}

From source file:org.bigmouth.nvwa.utils.degist.NativeAesUtils.java

public static String encryptAES(byte[] input, String key) throws Exception {
    byte[] crypted = null;
    javax.crypto.KeyGenerator kgen = javax.crypto.KeyGenerator.getInstance("AES");
    SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    secureRandom.setSeed(key.getBytes());
    kgen.init(128, secureRandom);//from   w  ww  .  j a  v a2s. com
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec skey = new SecretKeySpec(enCodeFormat, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skey);
    crypted = cipher.doFinal(input);
    return new String(Hex.encode(crypted));
}

From source file:org.b3log.latke.util.Crypts.java

/**
 * Encrypts by AES./*from www.j av a 2  s . c  om*/
 *
 * @param content the specified content to encrypt
 * @param key     the specified key
 * @return encrypted content
 * @see #decryptByAES(java.lang.String, java.lang.String)
 */
public static String encryptByAES(final String content, final String key) {
    try {
        final KeyGenerator kgen = KeyGenerator.getInstance("AES");
        final SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(key.getBytes());
        kgen.init(128, secureRandom);
        final SecretKey secretKey = kgen.generateKey();
        final byte[] enCodeFormat = secretKey.getEncoded();
        final SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
        final Cipher cipher = Cipher.getInstance("AES");
        final byte[] byteContent = content.getBytes("UTF-8");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        final byte[] result = cipher.doFinal(byteContent);

        return Hex.encodeHexString(result);
    } catch (final Exception e) {
        LOGGER.log(Level.WARN, "Encrypt failed", e);

        return null;
    }
}

From source file:org.b3log.latke.util.Crypts.java

/**
 * Decrypts by AES.//from ww  w  . ja  v  a 2 s  .  co  m
 *
 * @param content the specified content to decrypt
 * @param key     the specified key
 * @return original content
 * @see #encryptByAES(java.lang.String, java.lang.String)
 */
public static String decryptByAES(final String content, final String key) {
    try {
        final byte[] data = Hex.decodeHex(content.toCharArray());
        final KeyGenerator kgen = KeyGenerator.getInstance("AES");
        final SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(key.getBytes());
        kgen.init(128, secureRandom);
        final SecretKey secretKey = kgen.generateKey();
        final byte[] enCodeFormat = secretKey.getEncoded();
        final SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
        final Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        final byte[] result = cipher.doFinal(data);

        return new String(result, "UTF-8");
    } catch (final Exception e) {
        LOGGER.log(Level.WARN, "Decrypt failed");

        return null;
    }
}

From source file:org.apache.abdera.security.util.KeyHelper.java

public static KeyPair generateKeyPair(String type, int size)
        throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(type);
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    keyGen.initialize(size, random);/*w w  w .  j  a  v  a2  s .  co  m*/
    random.setSeed(System.currentTimeMillis());
    return keyGen.generateKeyPair();
}

From source file:org.apache.abdera.security.util.KeyHelper.java

public static KeyPair generateKeyPair(String type, int size, String provider)
        throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(type, provider);
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", provider);
    keyGen.initialize(size, random);//  ww  w  .  ja va  2 s .c o m
    random.setSeed(System.currentTimeMillis());
    return keyGen.generateKeyPair();
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = null;
    if (android.os.Build.VERSION.SDK_INT >= 17) {
        sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    } else {/*from w w w  .  j a va 2 s. co  m*/
        sr = SecureRandom.getInstance("SHA1PRNG");
    }
    sr.setSeed(seed);
    kgen.init(128, sr); //256 bits or 128 bits,192bits  
    SecretKey skey = kgen.generateKey();
    byte[] raw = skey.getEncoded();
    return raw;
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kGen = KeyGenerator.getInstance("AES");
    SecureRandom sr;
    if (android.os.Build.VERSION.SDK_INT >= 17) {
        sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    } else {//from ww  w. java 2s.c o  m
        sr = SecureRandom.getInstance("SHA1PRNG");
    }
    sr.setSeed(seed);
    kGen.init(128, sr);
    SecretKey sKey = kGen.generateKey();
    return sKey.getEncoded();
}

From source file:com.znsx.util.licence.LicenceUtil.java

/**
 * ??/*  w ww  . j a  va  2s  .c  o  m*/
 * 
 * @param seed
 *            ??
 * @return
 * @throws Exception
 */
public static Map<String, String> generateKey(String seed) throws Exception {
    Map<String, String> map = new HashMap<String, String>(2);
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = new SecureRandom();
    random.setSeed(seed.getBytes("utf8"));
    keygen.initialize(1024, random);

    KeyPair keyPair = keygen.generateKeyPair();
    PublicKey publicKey = keyPair.getPublic();
    PrivateKey privateKey = keyPair.getPrivate();

    Base64 base64 = new Base64();
    String publicKeyString = new String(base64.encode(publicKey.getEncoded()), "utf8");
    String privateKeyString = new String(base64.encode(privateKey.getEncoded()), "utf8");
    // BASE64Encoder encoder = new BASE64Encoder();
    // map.put("public", encoder.encode(publicKey.getEncoded()));
    // map.put("private", encoder.encode(privateKey.getEncoded()));
    map.put("public", publicKeyString);
    map.put("private", privateKeyString);

    System.out.println("publicKey: " + map.get("public"));
    System.out.println("privateKey: " + map.get("private"));
    return map;
}