Example usage for java.security SecureRandom getInstance

List of usage examples for java.security SecureRandom getInstance

Introduction

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

Prototype

public static SecureRandom getInstance(String algorithm, SecureRandomParameters params)
        throws NoSuchAlgorithmException 

Source Link

Document

Returns a SecureRandom object that implements the specified Random Number Generator (RNG) algorithm and supports the specified SecureRandomParameters request.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SecureRandom ran = SecureRandom.getInstance("SHA1PRNG", "SUN");
    ran.setSeed(101L);//from   www  . j av a  2s . c  om
    ran.setSeed(101L);
    byte[] seeds = ran.getSeed(24);
    for (int i = 0; i < seeds.length; i++) {
        System.out.println("Seed[" + i + "]:" + seeds[i]);
    }
}

From source file:Main.java

public static void main(String[] args) throws NoSuchAlgorithmException {
    int numBytes = (new Integer("1111")).intValue();

    SecureRandom srand = new SecureRandom(new byte[] { 1, 2, 3, 4 });

    Provider provider = srand.getProvider();

    SecureRandom newRandom = SecureRandom.getInstance("SHA1PRNG", provider);

    System.out.println(provider.getName());

    byte[] bytes = new byte[numBytes];
    srand.nextBytes(bytes);/*w  ww  .j av  a  2 s .  c om*/
    System.out.println(bytes);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    KeyGenerator keyGen = KeyGenerator.getInstance("DES");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    random.setSeed(101L);//from   w ww .j a  va2  s.c  o m
    keyGen.init(56, random);
    SecretKey sKey = keyGen.generateKey();
    SecretKeyFactory kfactory = SecretKeyFactory.getInstance("DES");

    DESKeySpec kspec = (DESKeySpec) kfactory.getKeySpec(sKey, DESKeySpec.class);

    System.out.println(sKey);
    FileOutputStream fos = new FileOutputStream("secretKeys");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    oos.writeObject(kspec.getKey());

    FileInputStream fin = new FileInputStream("secretKeys");
    ObjectInputStream ois = new ObjectInputStream(fin);

    byte[] kMaterial = (byte[]) ois.readObject();

    DESKeySpec keyspec = new DESKeySpec(kMaterial);
    SecretKey newKey = kfactory.generateSecret(keyspec);
    System.out.println(newKey);
    System.out.println("Do the keys equal :" + newKey.equals(sKey));

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
    random.setSeed(101L);//from  w  w  w . jav a  2s. co m
    keyGen.initialize(1024, random);
    KeyPair keypair = keyGen.generateKeyPair();

    KeyFactory kfactory = KeyFactory.getInstance("DSA");

    DSAPublicKeySpec kspec = (DSAPublicKeySpec) kfactory.getKeySpec(keypair.getPublic(),
            DSAPublicKeySpec.class);

    System.out.println(keypair.getPublic());
    FileOutputStream fos = new FileOutputStream("publicKeys");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

    oos.writeObject(kspec.getY());
    oos.writeObject(kspec.getP());
    oos.writeObject(kspec.getQ());
    oos.writeObject(kspec.getG());

    FileInputStream fin = new FileInputStream("publicKeys");
    ObjectInputStream ois = new ObjectInputStream(fin);

    BigInteger Y = (BigInteger) ois.readObject();
    BigInteger P = (BigInteger) ois.readObject();
    BigInteger Q = (BigInteger) ois.readObject();
    BigInteger G = (BigInteger) ois.readObject();

    DSAPublicKeySpec keyspec = new DSAPublicKeySpec(Y, P, Q, G);
    PublicKey pkey = kfactory.generatePublic(keyspec);
    System.out.println(pkey);
}

From source file:GenSig.java

public static void main(String[] args) {

    /* Generate a DSA signature */

    if (args.length != 1) {
        System.out.println("Usage: GenSig nameOfFileToSign");
    } else/*  w  w  w . j a v  a2  s  .c o  m*/
        try {

            /* Generate a key pair */

            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

            keyGen.initialize(1024, random);

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

            /*
             * Create a Signature object and initialize it with the private
             * key
             */

            Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");

            dsa.initSign(priv);

            /* Update and sign the data */

            FileInputStream fis = new FileInputStream(args[0]);
            BufferedInputStream bufin = new BufferedInputStream(fis);
            byte[] buffer = new byte[1024];
            int len;
            while (bufin.available() != 0) {
                len = bufin.read(buffer);
                dsa.update(buffer, 0, len);
            }
            ;

            bufin.close();

            /*
             * Now that all the data to be signed has been read in, generate
             * a signature for it
             */

            byte[] realSig = dsa.sign();

            /* Save the signature in a file */
            FileOutputStream sigfos = new FileOutputStream("sig");
            sigfos.write(realSig);

            sigfos.close();

            /* Save the public key in a file */
            byte[] key = pub.getEncoded();
            FileOutputStream keyfos = new FileOutputStream("suepk");
            keyfos.write(key);

            keyfos.close();

        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }

}

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);// w w w .j  av  a 2  s.  co  m
    kgen.init(256, sr); // 192 and 256 bits may not be available
    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);//from   w  ww  .ja  v a 2  s. c om
    kgen.init(128, sr);
    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);//  w  w w  .  j a va2  s.  c  o  m
    kgen.init(128, sr);
    SecretKey sKey = kgen.generateKey();
    byte[] raw = sKey.getEncoded();

    return raw;
}

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: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);// w  ww  . ja v  a  2 s  .  c  o m
    kgen.init(128, sr);
    SecretKey skey = kgen.generateKey();
    return skey.getEncoded();
}