List of usage examples for java.security SecureRandom getInstance
public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:com.clustercontrol.util.KeyCheck.java
/** * ????????//from w w w . j a va2s . c o m * * @param args */ public static void main(String[] args) { PrivateKey privateKey = null; PublicKey publicKey = null; /// ??????? true /// ???????? false (?) boolean flag = false; if (flag) { try { // ? privateKey = getPrivateKey( "???????privateKey.txt??"); // ? publicKey = getPublicKey("???????"); // publicKey = getPublicKey(publicKeyStr); } catch (Exception e) { System.out.println("hoge" + e.getMessage()); } } else { KeyPairGenerator generator; try { generator = KeyPairGenerator.getInstance(ALGORITHM); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); // ?? 1024 generator.initialize(1024, random); KeyPair keyPair = generator.generateKeyPair(); privateKey = keyPair.getPrivate(); publicKey = keyPair.getPublic(); } catch (NoSuchAlgorithmException ex) { System.out.println(ex.getMessage()); } } // // ? System.out.println("?"); System.out.println(byte2String(privateKey.getEncoded())); System.out.println("?"); System.out.println(byte2String(publicKey.getEncoded())); // ??????? String string = "20140701_nttdata"; byte[] src = string.getBytes(); System.out.println("??String"); System.out.println(string); System.out.println("??byte"); System.out.println(byte2String(src)); // ? try { String encStr = encrypt(string, privateKey); System.out.println("?"); System.out.println(encStr); // ? String decStr = decrypt(encStr, publicKey); System.out.println("?"); System.out.println(decStr); } catch (Exception e) { System.out.println(e.getMessage()); } }
From source file:org.scantegrity.scanner.Scanner.java
/** * Main Logic loop.//ww w . j av a 2 s. co m * * @param args CLI flags */ public static void main(String[] args) { processCmdLine(args); //Create the locations we should output files too createOutputDirectories(); //register logging handlers if any c_myId = c_config.getPollID(); c_log = new Logging(c_outDirs, c_myId, Level.FINEST); c_log.log(Level.INFO, "Logging Intialized"); c_log.log(Level.INFO, "Scanner ID Number: " + c_myId); c_log.log(Level.INFO, "Initializing Cryptographic hash and rng."); try { c_hash = MessageDigest.getInstance("SHA1"); } catch (NoSuchAlgorithmException l_e) { l_e.printStackTrace(); c_hash = null; c_log.log(Level.SEVERE, "Unable to initialize hash. Reason: " + l_e.getMessage()); } try { c_csprng = SecureRandom.getInstance("SHA1PRNG"); } catch (NoSuchAlgorithmException l_e) { l_e.printStackTrace(); c_csprng = null; c_log.log(Level.SEVERE, "Unable to initialize RNG. Reason: " + l_e.getMessage()); } //init counters c_scanCount = new Integer(0); c_ballotCount = new Integer(0); c_errorCount = new Integer(0); checkForPreviousCounters(); writeCounters(); //init ballot storage c_ballotIds = new Vector<Integer>(); //TODO: Change this size to be variable... c_store = initializeBallotStore(c_outDirs, 100 * 1024 * 1024); //start the election c_log.log(Level.SEVERE, "Election Started"); playAudioClip(2); //main loop //TODO: terminating condition, button, or special ballot??? while (true) { BufferedImage l_ballotImg[] = null; Ballot l_ballot = null; //process image into ballot l_ballotImg = getBallotImages(); if (l_ballotImg == null || (l_ballotImg[0] == null && l_ballotImg[1] == null)) continue; //scan count c_scanCount++; writeCounters(); playAudioClip(0); for (int l_c = 0; l_c < l_ballotImg.length; l_c++) { //Ignore empties if (l_ballotImg[l_c] == null) { c_log.log(Level.WARNING, "Only 1 ballot object returned." + " Make sure the scanner supports duplex"); continue; } //Ignore blank pages if (DrunkDriver.isDrunk(l_ballotImg[l_c], 10)) continue; l_ballot = getBallot(l_ballotImg[l_c]); if (l_ballot == null) continue; //update ballot counter c_ballotCount++; writeCounters(); l_ballot.setScannerId(c_myId); if (isDuplicate(l_ballot)) { c_log.log(Level.WARNING, "Duplicate Ballot detected. ID : " + l_ballot.getId()); l_ballot.setCounted(false); l_ballot.addNote("Duplicate Ballot"); } //check if the ballot is a "starting ballot" //check if the ballot is a "closing ballot" //else saveBallot(l_ballot); } //resume scanning } //end election (ballot handler) //turn off storage //disconnect devices //turn off log //quit //endElection(); }
From source file:Main.java
private static byte[] randomBytes(int length) throws Exception { SecureRandom random = SecureRandom.getInstance(RANDOM_ALGORITHM); byte[] b = new byte[length]; random.nextBytes(b);/*www . j a v a2s . co m*/ return b; }
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);/* ww w . java2 s .c o m*/ kgen.init(128, sr); SecretKey sKey = kgen.generateKey(); byte[] raw = sKey.getEncoded(); return raw; }
From source file:Main.java
public static String generateNonce() { try {/*from ww w .j av 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); // 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: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);/* w w w. j a va 2 s . c om*/ kg.init(128, sr); SecretKey secretKey = kg.generateKey(); return secretKey.getEncoded(); }
From source file:Main.java
public static KeyPair generateKeyPair(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException { KeyPairGenerator generator = KeyPairGenerator.getInstance(KEYGEN_ALGORITHM/* , PROVIDER_NAME */); SecureRandom secureRandom = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM/* , PROVIDER_NAME */); generator.initialize(keySize, secureRandom); return generator.generateKeyPair(); }
From source file:Main.java
public static SecureRandom getSecureRandom() { try {/*w ww . j a v a2 s . c o m*/ return SecureRandom.getInstance("SHA1PRNG"); } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); } }
From source file:Main.java
private static byte[] generateSalt() throws NoSuchAlgorithmException { SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] salt = new byte[8]; random.nextBytes(salt);/*from w w w .ja va 2 s . c om*/ return salt; }
From source file:Main.java
private static byte[] generateSalt() throws NoSuchAlgorithmException { SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] salt = new byte[16]; random.nextBytes(salt);// ww w . ja v a 2 s . c om return salt; }