List of usage examples for java.security SecureRandom nextInt
public int nextInt()
From source file:org.dogtagpki.server.rest.SystemConfigService.java
public void initializeDatabase(ConfigurationRequest data) throws EBaseException { if (data.isClone() && data.getSetupReplication()) { String masterhost = ""; String masterport = ""; String masterbasedn = ""; String realhostname = ""; try {/*from www . ja v a2 s. c o m*/ masterhost = cs.getString("preop.internaldb.master.ldapconn.host", ""); masterport = cs.getString("preop.internaldb.master.ldapconn.port", ""); masterbasedn = cs.getString("preop.internaldb.master.basedn", ""); realhostname = cs.getString("machineName", ""); } catch (Exception e) { } if (masterhost.equals(realhostname) && masterport.equals(data.getDsPort())) { throw new BadRequestException("Master and clone must not share the same internal database"); } if (!masterbasedn.equals(data.getBaseDN())) { throw new BadRequestException("Master and clone should have the same base DN"); } String masterReplicationPort = data.getMasterReplicationPort(); if ((masterReplicationPort != null) && (!masterReplicationPort.equals(""))) { cs.putString("internaldb.ldapconn.masterReplicationPort", masterReplicationPort); } else { cs.putString("internaldb.ldapconn.masterReplicationPort", masterport); } String cloneReplicationPort = data.getCloneReplicationPort(); if ((cloneReplicationPort == null) || (cloneReplicationPort.length() == 0)) { cloneReplicationPort = data.getDsPort(); } cs.putString("internaldb.ldapconn.cloneReplicationPort", cloneReplicationPort); String replicationSecurity = data.getReplicationSecurity(); if ((cloneReplicationPort == data.getDsPort()) && (data.getSecureConn().equals("true"))) { replicationSecurity = "SSL"; } else if (replicationSecurity == null) { replicationSecurity = "None"; } cs.putString("internaldb.ldapconn.replicationSecurity", replicationSecurity); cs.putString("preop.internaldb.replicateSchema", data.getReplicateSchema()); } try { /* BZ 430745 create password for replication manager */ // use user-provided password if specified String replicationPassword = data.getReplicationPassword(); if (StringUtils.isEmpty(replicationPassword)) { // generate random password JssSubsystem jssSubsystem = (JssSubsystem) CMS.getSubsystem(JssSubsystem.ID); SecureRandom random = jssSubsystem.getRandomNumberGenerator(); replicationPassword = Integer.toString(random.nextInt()); } IConfigStore psStore = null; String passwordFile = null; passwordFile = cs.getString("passwordFile"); psStore = CMS.createFileConfigStore(passwordFile); psStore.putString("internaldb", data.getBindpwd()); if (StringUtils.isEmpty(psStore.getString("replicationdb", null))) { psStore.putString("replicationdb", replicationPassword); } psStore.commit(false); ConfigurationUtils.enableUSNPlugin(); ConfigurationUtils.populateDB(); cs.putString("preop.internaldb.replicationpwd", replicationPassword); cs.putString("preop.database.removeData", "false"); if (data.getSharedDB()) { cs.putString("preop.internaldb.dbuser", data.getSharedDBUserDN()); } cs.commit(false); if (data.isClone() && data.getSetupReplication()) { ReplicationUtil.setupReplication(); } ConfigurationUtils.populateDBManager(); ConfigurationUtils.populateVLVIndexes(); } catch (Exception e) { logger.error("Unable to populate database: " + e.getMessage(), e); throw new PKIException("Unable to populate database: " + e.getMessage(), e); } }
From source file:org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.java
private void prepareEncryptionDictRev6(String ownerPassword, String userPassword, PDEncryption encryptionDictionary, int permissionInt) throws IOException { try {// ww w. j av a2 s . c om SecureRandom rnd = new SecureRandom(); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); // make a random 256-bit file encryption key encryptionKey = new byte[32]; rnd.nextBytes(encryptionKey); // Algorithm 8a: Compute U byte[] userPasswordBytes = truncate127(userPassword.getBytes(Charsets.UTF_8)); byte[] userValidationSalt = new byte[8]; byte[] userKeySalt = new byte[8]; rnd.nextBytes(userValidationSalt); rnd.nextBytes(userKeySalt); byte[] hashU = computeHash2B(concat(userPasswordBytes, userValidationSalt), userPasswordBytes, null); byte[] u = concat(hashU, userValidationSalt, userKeySalt); // Algorithm 8b: Compute UE byte[] hashUE = computeHash2B(concat(userPasswordBytes, userKeySalt), userPasswordBytes, null); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(hashUE, "AES"), new IvParameterSpec(new byte[16])); byte[] ue = cipher.doFinal(encryptionKey); // Algorithm 9a: Compute O byte[] ownerPasswordBytes = truncate127(ownerPassword.getBytes(Charsets.UTF_8)); byte[] ownerValidationSalt = new byte[8]; byte[] ownerKeySalt = new byte[8]; rnd.nextBytes(ownerValidationSalt); rnd.nextBytes(ownerKeySalt); byte[] hashO = computeHash2B(concat(ownerPasswordBytes, ownerValidationSalt, u), ownerPasswordBytes, u); byte[] o = concat(hashO, ownerValidationSalt, ownerKeySalt); // Algorithm 9b: Compute OE byte[] hashOE = computeHash2B(concat(ownerPasswordBytes, ownerKeySalt, u), ownerPasswordBytes, u); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(hashOE, "AES"), new IvParameterSpec(new byte[16])); byte[] oe = cipher.doFinal(encryptionKey); // Set keys and other required constants in encryption dictionary encryptionDictionary.setUserKey(u); encryptionDictionary.setUserEncryptionKey(ue); encryptionDictionary.setOwnerKey(o); encryptionDictionary.setOwnerEncryptionKey(oe); PDCryptFilterDictionary cryptFilterDictionary = new PDCryptFilterDictionary(); cryptFilterDictionary.setCryptFilterMethod(COSName.AESV3); cryptFilterDictionary.setLength(keyLength); encryptionDictionary.setStdCryptFilterDictionary(cryptFilterDictionary); encryptionDictionary.setStreamFilterName(COSName.STD_CF); encryptionDictionary.setStringFilterName(COSName.STD_CF); setAES(true); // Algorithm 10: compute "Perms" value byte[] perms = new byte[16]; perms[0] = (byte) permissionInt; perms[1] = (byte) (permissionInt >>> 8); perms[2] = (byte) (permissionInt >>> 16); perms[3] = (byte) (permissionInt >>> 24); perms[4] = (byte) 0xFF; perms[5] = (byte) 0xFF; perms[6] = (byte) 0xFF; perms[7] = (byte) 0xFF; perms[8] = 'T'; // we always encrypt Metadata perms[9] = 'a'; perms[10] = 'd'; perms[11] = 'b'; for (int i = 12; i <= 15; i++) { perms[i] = (byte) rnd.nextInt(); } cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptionKey, "AES"), new IvParameterSpec(new byte[16])); byte[] permsEnc = cipher.doFinal(perms); encryptionDictionary.setPerms(permsEnc); } catch (GeneralSecurityException e) { logIfStrongEncryptionMissing(); throw new IOException(e); } }