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:eap.util.EDcodeUtil.java

private static byte[] aes(byte[] data, byte[] key, int keyLen, int opMode) {
    try {//  w ww . j  a  v  a2s .  com
        KeyGenerator kgen = KeyGenerator.getInstance("AES", provider);
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); // provider
        secureRandom.setSeed(key);
        kgen.init(keyLen, secureRandom);
        SecretKey secretKey = kgen.generateKey();
        SecretKeySpec keySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");

        /* mode:   ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128/OFB/OBF8 to OFB128<br/> 
        * padding: Nopadding/PKCS5Padding/ISO10126Padding
        */
        Cipher cipher = Cipher.getInstance("AES", provider); // ECB/PKCS5Padding
        cipher.init(opMode, keySpec);

        return cipher.doFinal(data);
    } catch (Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }

    //      // we're using Bouncy Castle
    //       Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider())
    //
    //       // create our key specification
    //       val secretKeySpec = new SecretKeySpec(hexStringToByteArray(hexEncodedKey), "AES")
    //        
    //       // create an AES engine in CTR mode (no padding)
    //       val aes = Cipher.getInstance("AES/CTR/NoPadding", BouncyCastleProvider.PROVIDER_NAME)
    //        
    //       // initialize the AES engine in encrypt mode with the key and IV
    //       aes.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(hexStringToByteArray(hexEncodedIv)))
    //        
    //       // encrypt the message and return the encrypted byte array
    //       aes.doFinal(hexStringToByteArray(hexEncodedMessage))
}

From source file:com.data.pack.Util.java

public static void copyFile(InputStream in, OutputStream out, int flag) throws IOException {
    byte[] buffer = new byte[1024];
    int read;//from  w w  w.  j a  v  a  2 s .  c om

    try {

        Cipher encipher = null;
        try {
            encipher = Cipher.getInstance("AES");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Cipher decipher = null;
        try {
            decipher = Cipher.getInstance("AES");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        KeyGenerator kgen = null;
        try {
            kgen = KeyGenerator.getInstance("AES");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        byte[] keyStart = "fitnesSbridge".getBytes();
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        sr.setSeed(keyStart);
        kgen.init(128, sr); // 192 and 256 bits may not be available
        SecretKey skey = kgen.generateKey();

        // byte key[] =
        // {0x00,0x32,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x23,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
        skey = kgen.generateKey();
        // Lgo
        try {
            encipher.init(Cipher.ENCRYPT_MODE, skey);
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        CipherInputStream cis = new CipherInputStream(in, encipher);
        try {
            decipher.init(Cipher.DECRYPT_MODE, skey);
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        CipherOutputStream cos = new CipherOutputStream(out, decipher);

        try {

            if (flag == 2) {
                cos = new CipherOutputStream(out, encipher);
            } else {
                cos = new CipherOutputStream(out, decipher);
            }
            while ((read = in.read()) != -1) {
                cos.write(read);
                cos.flush();
            }

            cos.flush();
            cos.close();
            in.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (Exception e) {
        // TODO: handle exception
    }

    //
    // byte[] keyStart = "this is a key".getBytes();
    // KeyGenerator kgen = KeyGenerator.getInstance("AES");
    // SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    // sr.setSeed(keyStart);
    // kgen.init(128, sr); // 192 and 256 bits may not be available
    // SecretKey skey = kgen.generateKey();
    // byte[] key = skey.getEncoded();
    //
    //
    // byte[] b = baos.toByteArray();
    // while ((read = in.read(buffer)) != -1) {
    //
    // // decrypt
    // byte[] decryptedData = Util.decrypt(key,buffer);
    // out.write(decryptedData, 0, read);
    // }
    // } catch (NoSuchAlgorithmException e) {
    // // TODO Auto-generated catch block
    // e.printStackTrace();
    // }
    // catch (Exception e) {
    // // TODO: handle exception
    // }
    //
}

From source file:RandomUtil.java

/**
 * Creates a new secure random number generator. The following secure random
 * algorithm names are tried:// ww  w. jav  a  2s  .c o  m
 * <ul>
 * <li>The value of system property "boticelli.securerandom", if set. </li>
 * <li> "SHA1PRNG" </li>
 * <li> "IBMSecureRandom" (available if running in the IBM JRE) </li>
 * </ul>
 */
private static SecureRandom createSecureRandom() {
    SecureRandom secureRnd = null;
    try {

        for (int i = 0; i < secureRndNames.length; i++) {
            try {
                if (secureRndNames[i] != null) {
                    secureRnd = SecureRandom.getInstance(secureRndNames[i]);
                    break;
                }
            } catch (NoSuchAlgorithmException nsae) {
                //  log.debug("no secure random algorithm named \"" + secureRndNames[i] + "\"",
                //      nsae);
            }
        }
        if (secureRnd == null) {
            throw new IllegalStateException(
                    "no secure random algorithm found. (tried " + Arrays.asList(secureRndNames) + ")");
        }
        secureRnd.setSeed(System.currentTimeMillis());
    } catch (Exception e) {
        // log.fatal("error initializing secure random", e);
    }

    return secureRnd;
}

From source file:jef.tools.security.EncrypterUtil.java

/**
 * ?KEY/*www  .ja  v a 2 s .  co m*/
 * 
 * @param algom
 *            ? DSA RSA
 * @return
 */
public static final KeyPair generateKeyPair(String algom) {
    try {
        java.security.KeyPairGenerator keygen = java.security.KeyPairGenerator.getInstance(algom);
        SecureRandom secrand = new SecureRandom();
        secrand.setSeed("\n".getBytes()); // ??
        //  512  1024  64 ?
        keygen.initialize(1024, secrand); // ??
        // keygen.initialize(512);
        KeyPair keys = keygen.generateKeyPair(); // ?
        return keys;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}

From source file:pt.aptoide.backupapps.data.webservices.ManagerUploads.java

public static String generateBoundary() {
    try {/*from  w ww  .  j  a v  a 2  s. c o  m*/
        // Create a secure random number generator
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

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

        int seedByteCount = 10;
        byte[] seed = sr.generateSeed(seedByteCount);

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

        return "***" + Long.toString(sr.nextLong()) + "***";

    } catch (NoSuchAlgorithmException e) {
    }
    return "*********";
}

From source file:MainClass.java

public KeyPair generateKeyPair(long seed) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
    SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
    rng.setSeed(seed);
    keyGenerator.initialize(1024, rng);//from   w  w  w  .  jav  a 2  s . co  m

    return (keyGenerator.generateKeyPair());
}

From source file:com.thoughtworks.go.security.CipherProvider.java

private byte[] generateKey() {
    SecureRandom random = new SecureRandom();
    random.setSeed("go-server".getBytes());
    KeyGenerationParameters generationParameters = new KeyGenerationParameters(random,
            DESParameters.DES_KEY_LENGTH * 8);
    DESKeyGenerator generator = new DESKeyGenerator();
    generator.init(generationParameters);
    return Hex.encode(generator.generateKey());
}

From source file:com.thoughtworks.go.security.DESCipherProvider.java

private byte[] generateKey() {
    SecureRandom random = new SecureRandom();
    random.setSeed(UUID.randomUUID().toString().getBytes());
    KeyGenerationParameters generationParameters = new KeyGenerationParameters(random,
            DESParameters.DES_KEY_LENGTH * 8);
    DESKeyGenerator generator = new DESKeyGenerator();
    generator.init(generationParameters);
    return generator.generateKey();
}

From source file:org.talend.utils.security.AES.java

public AES() {
    try {/*from   w w  w.  j a va2  s. c o  m*/
        // TDI-28380: Database password in tac db configuration page becomes empty once restart tomcat on Solaris.
        // TDI-30348: Whole tac configuration lost for the passwords.

        Provider p = Security.getProvider("BC");
        KeyGenerator keyGen = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM, p);

        SecureRandom random = SecureRandom.getInstance(RANDOM_SHA1PRNG);
        random.setSeed(KeyValues);
        keyGen.init(128, random);

        Key key = keyGen.generateKey();

        ecipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, p);
        dcipher = Cipher.getInstance(ENCRYPTION_ALGORITHM, p);

        ecipher.init(Cipher.ENCRYPT_MODE, key);
        dcipher.init(Cipher.DECRYPT_MODE, key);
    } catch (Exception e) {
        // log the error to avoid that break GWT service
        log.error(e.getMessage(), e);
    }
}

From source file:com.streamsets.lib.security.util.DataSignature.java

public KeyPair generateKeyPair() throws GeneralSecurityException {
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
    SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
    rng.setSeed(System.currentTimeMillis());
    keyGenerator.initialize(1024, rng);//  w  ww  . j av  a 2 s .c  o m
    return keyGenerator.generateKeyPair();
}