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.znsx.util.licence.LicenceUtil.java

/**
 * ?DSA??p,q,g,j,x,y//from  ww  w . jav a 2s.c  o  m
 * 
 * @param seed
 *            ??
 * @throws Exception
 * @author huangbuji
 *         <p />
 *         Create at 2014-2-8 ?4:45:26
 */
@SuppressWarnings("restriction")
public static void genKey(String seed) throws Exception {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = new SecureRandom();
    random.setSeed(seed.getBytes("utf8"));
    keygen.initialize(1024, random);

    KeyPair keyPair = keygen.generateKeyPair();
    DSAPublicKeyImpl publicKey = (DSAPublicKeyImpl) keyPair.getPublic();
    DSAPrivateKey privateKey = (DSAPrivateKey) keyPair.getPrivate();
    DSAParams dsaParams = privateKey.getParams();
    Base64 base64 = new Base64();
    String p = new String(base64.encode(dsaParams.getP().toByteArray()), "utf8");
    String q = new String(base64.encode(dsaParams.getQ().toByteArray()), "utf8");
    String g = new String(base64.encode(dsaParams.getG().toByteArray()), "utf8");
    String x = new String(base64.encode(privateKey.getX().toByteArray()), "utf8");
    String y = new String(base64.encode(publicKey.getY().toByteArray()), "utf8");
    System.out.println("P: " + p);
    System.out.println("Q: " + q);
    System.out.println("G: " + g);
    System.out.println("X: " + x);
    System.out.println("Y: " + y);

    String publicKeyString = new String(base64.encode(publicKey.getEncoded()), "utf8");
    String privateKeyString = new String(base64.encode(privateKey.getEncoded()), "utf8");
    System.err.println("public: " + publicKeyString);
    System.err.println("private: " + privateKeyString);

    File publicFile = new File("D:/binPublic.ky");
    File privateFile = new File("D:/binPrivate.ky");
    FileOutputStream out = new FileOutputStream(publicFile);
    out.write(publicKey.getEncoded());
    out.flush();
    out.close();
    out = new FileOutputStream(privateFile);
    out.write(privateKey.getEncoded());
    out.flush();
    out.close();
}

From source file:org.linagora.linshare.core.utils.HashUtils.java

/**
 * generate salt (32 bits/4 octets)/*w  w w .  ja  v a  2  s .  c  o m*/
 * @return
 */
public static byte[] getSalt() {
    // Get 32 random bits
    byte[] mybytes;
    try {
        // Create a secure random number generator
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

        // Create secure number generator with seed
        int seedByteCount = 10;
        byte[] seed = sr.generateSeed(seedByteCount);

        sr = SecureRandom.getInstance("SHA1PRNG");
        sr.setSeed(seed);

        mybytes = new byte[32 / 8];
        sr.nextBytes(mybytes);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    return mybytes;
}

From source file:utils.Hash.java

/**
 * Creates a psedorandom string//from w w  w. j a va  2 s .com
 * @return Random String
 */
public static String randomString() {
    String result = new String();
    try {
        byte byteArray[] = new byte[16];
        SecureRandom psn1 = SecureRandom.getInstance("SHA1PRNG");
        psn1.setSeed(psn1.nextLong());
        psn1.nextBytes(byteArray);
        BigInteger bigInt = new BigInteger(byteArray);
        result = bigInt.toString();
        log.debug("Generated String = " + result);

    } catch (Exception e) {
        log.error("Random Number Error : " + e.toString());
    }
    return result;
}

From source file:utils.Hash.java

/**
 * Generates a small psedorandom string//from   w  w w .  jav a2  s  . c  o  m
 * @return Random String
 */
public static String smallRandomString() {
    String result = new String();
    try {
        byte byteArray[] = new byte[4];
        SecureRandom psn1 = SecureRandom.getInstance("SHA1PRNG");
        psn1.setSeed(psn1.nextLong());
        psn1.nextBytes(byteArray);
        BigInteger bigInt = new BigInteger(byteArray);
        result = bigInt.toString();
        log.debug("Generated String = " + result);

    } catch (Exception e) {
        log.error("Random Number Error : " + e.toString());
    }
    return result;
}

From source file:Main.java

public static String generateNonce() {
    try {//from   ww w .  jav a  2s  .  c  om
        // Create a secure random number generator
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

        // Get 1024 random bits
        byte[] bytes = new byte[1024 / 8];
        sr.nextBytes(bytes);

        // Create two secure number generators with the same seed
        int seedByteCount = 10;
        byte[] seed = sr.generateSeed(seedByteCount);

        sr = SecureRandom.getInstance("SHA1PRNG");
        sr.setSeed(seed);
        SecureRandom sr2 = SecureRandom.getInstance("SHA1PRNG");
        sr2.setSeed(seed);

        String nonce = Long.toHexString(sr2.nextLong());
        nonce = nonce.substring(0, 7);

        return nonce;

    } catch (NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:servlets.module.challenge.BrokenCryptoHomeMade.java

public static String randomKeyLengthString() {
    String result = new String();
    try {//from   w  w w .jav a2  s  . c o m
        byte byteArray[] = new byte[16];
        SecureRandom psn1 = SecureRandom.getInstance("SHA1PRNG");
        psn1.setSeed(psn1.nextLong());
        psn1.nextBytes(byteArray);
        result = new String(byteArray, Charset.forName("US-ASCII"));
        //log.debug("Generated Key = " + result);
        if (result.length() != 16) {
            log.error("Generated Key is the incorrect Length: Shortening ");
            result = result.substring(0, 15);
            if (result.length() != 16)
                log.fatal("Encryption key length is Still not Right");
        }
    } catch (Exception e) {
        log.error("Random Number Error : " + e.toString());
    }
    return result;
}

From source file:utils.Hash.java

/**
 * Creates a psedorandom base64 string/*  w  w  w  .j a  va  2 s .co  m*/
 * @return Random String
 */
public static String randomBase64String() {
    String result = new String();
    try {
        byte byteArray[] = new byte[256];
        SecureRandom psn1 = SecureRandom.getInstance("SHA1PRNG");

        Base64 base64 = new Base64();
        psn1.setSeed(psn1.nextLong());
        psn1.nextBytes(byteArray);
        result = new String(byteArray, Charset.forName("US-ASCII"));
        result = base64.encode(thisString(thisString(byteArray.toString())).getBytes()).toString();
        log.debug("Generated String = " + result);
    } catch (Exception e) {
        log.error("Random Number Error : " + e.toString());
    }
    return result;
}

From source file:org.aon.esolutions.appconfig.client.util.RSAEncryptUtil.java

/**
 * Generate key which contains a pair of privae and public key using 1024 bytes
 * @return key pair/*from w  w  w . ja  va 2  s  .  c  o  m*/
 * @throws NoSuchAlgorithmException
 */
public static KeyPair generateKey(String keyPhrase) throws GeneralSecurityException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);

    SecureRandom randomAlg = SecureRandom.getInstance("SHA1PRNG", "SUN");
    randomAlg.setSeed(keyPhrase.getBytes());

    keyGen.initialize(1024, randomAlg);
    KeyPair key = keyGen.generateKeyPair();
    return key;
}

From source file:com.aimluck.eip.util.ALCommonUtils.java

/**
 * ID??SecureRandom????//from  w  w w  . j a v  a 2 s .c o m
 * 
 * @return random ID??SecureRandom
 */
public static SecureRandom getSecureRandom() {
    SecureRandom random = null;
    try {
        random = SecureRandom.getInstance(DEF_RANDOM_ALGORITHM);
        byte seed[] = random.generateSeed(DEF_RANDOM_LENGTH);
        random.setSeed(seed);
    } catch (Exception e) {
        logger.error("ALCommonUtils.getSecureRandom", e);
        return null;
    }
    return random;
}

From source file:com.wandrell.example.swss.test.util.factory.SecureSoapMessages.java

/**
 * Generates a nonce value for the SOAP secure header.
 *
 * @return the nonce value//from  w w  w . j a v a2s .  c om
 * @throws Exception
 *             if any error occurs while generating the nonce
 */
private static final String getNonce() throws Exception {
    final SecureRandom random; // Random value generator
    final byte[] nonceBytes; // Bytes to generate the nonce

    random = SecureRandom.getInstance("SHA1PRNG");
    random.setSeed(System.currentTimeMillis());
    nonceBytes = new byte[16];
    random.nextBytes(nonceBytes);

    return new String(Base64.encodeBase64(nonceBytes), "UTF-8");
}