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:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    sr.setSeed(seed);
    kgen.init(128, sr);/* ww  w.  ja va 2s  .  co m*/
    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 = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    sr.setSeed(seed);
    kgen.init(128, sr);/*from w w w .jav  a  2  s .  c om*/
    SecretKey sKey = kgen.generateKey();
    byte[] raw = sKey.getEncoded();

    return raw;
}

From source file:Main.java

public static byte[] generateKey(String password) throws Exception {
    byte[] keyStart = password.getBytes("UTF-8");

    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    sr.setSeed(keyStart);
    kgen.init(128, sr);/*ww  w. j  a v a 2s.  com*/
    SecretKey skey = kgen.generateKey();
    return skey.getEncoded();
}

From source file:Main.java

public static byte[] getRandomKey(byte[] seed) throws Exception {

    KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); //random
    sr.setSeed(seed);
    kg.init(128, sr);/*from w  w w  . j  av a  2s. c  om*/
    SecretKey secretKey = kg.generateKey();
    return secretKey.getEncoded();
}

From source file:Main.java

private static byte[] getRawKey(byte[] paramArrayOfByte) throws Exception {
    KeyGenerator localKeyGenerator = KeyGenerator.getInstance("AES");
    SecureRandom localSecureRandom = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    localSecureRandom.setSeed(paramArrayOfByte);
    localKeyGenerator.init(128, localSecureRandom);
    return localKeyGenerator.generateKey().getEncoded();
}

From source file:com.example.license.RSAUtil.java

public static KeyPair generatorKeyPair(String seed) throws Exception {
    KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    SecureRandom random = new SecureRandom();
    random.setSeed(seed.getBytes());
    kpGenerator.initialize(2014, random);
    KeyPair keyPair = kpGenerator.generateKeyPair();
    return keyPair;
}

From source file:Main.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  w  w w. j a  v  a2s .  c  o  m*/
}

From source file:Main.java

private static byte[] getRawKey(byte[] seed) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    //  SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
    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   w w w .  jav  a2  s .  c o m*/
}

From source file:com.vimukti.accounter.developer.api.PublicKeyGenerator.java

private static void generate() throws NoSuchAlgorithmException, NoSuchProviderException,
        InvalidKeySpecException, KeyStoreException, CertificateException, IOException, URISyntaxException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    random.setSeed("VimTech".getBytes("UTF-8"));
    keyGen.initialize(1024, random);/* w  ww  .  jav  a2 s  . c  o m*/

    KeyPair pair = keyGen.generateKeyPair();
    PrivateKey priv = pair.getPrivate();
    PublicKey pub = pair.getPublic();
    System.out.println(priv);
    System.out.println(pub);

    byte[] encoded = pub.getEncoded();
    byte[] encodeBase64 = Base64.encodeBase64(encoded);
    System.out.println("Public Key:" + new String(encodeBase64));

    byte[] encodedPrv = priv.getEncoded();
    byte[] encodeBase64Prv = Base64.encodeBase64(encodedPrv);
    System.out.println("Private Key:" + new String(encodeBase64Prv));

    byte[] decodeBase64 = Base64.decodeBase64(encodeBase64);
    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(decodeBase64);
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");

    System.out.println(keyFactory.generatePublic(pubKeySpec).equals(pub));
}

From source file:com.aqnote.shared.cryptology.asymmetric.dsa.DSAKeyPairGenTest.java

public static void generator() throws NoSuchAlgorithmException, FileNotFoundException, IOException {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM);

    // ???//ww w  .j  ava  2  s .c  o m
    SecureRandom secrand = new SecureRandom();
    secrand.setSeed(seed); // ??
    // ??, ??keysize ?. 512  1024  64 ?
    keygen.initialize(512, secrand);
    // ?pubkey?prikey
    KeyPair keys = keygen.generateKeyPair(); // ?
    PublicKey pubkey = keys.getPublic();
    PrivateKey prikey = keys.getPrivate();

    byte[] pubkeyByteArray = Base64.encodeBase64(pubkey.getEncoded());
    OutputStream os = new FileOutputStream(new File(PUBKEY_FILE_NAME));
    ByteArrayInputStream bais = new ByteArrayInputStream(pubkeyByteArray);
    StreamUtil.io(bais, os);
    bais.close();
    os.close();

    byte[] prikeyByteArray = Base64.encodeBase64(prikey.getEncoded());
    os = new FileOutputStream(new File(PRIKEY_FILE_NAME));
    bais = new ByteArrayInputStream(prikeyByteArray);
    StreamUtil.io(bais, os);
    bais.close();
    os.close();
}