List of usage examples for javax.crypto KeyGenerator init
public final void init(int keysize)
From source file:net.sf.xfd.provider.PublicProvider.java
private static @Nullable Key getSalt(Context c) { if (cookieSalt == null) { synchronized (PublicProvider.class) { if (cookieSalt == null) { try { try (ObjectInputStream oos = new ObjectInputStream(c.openFileInput(COOKIE_FILE))) { cookieSalt = (Key) oos.readObject(); } catch (ClassNotFoundException | IOException e) { LogUtil.logCautiously("Unable to read key file, probably corrupted or missing", e); final File corrupted = c.getFileStreamPath(COOKIE_FILE); //noinspection ResultOfMethodCallIgnored corrupted.delete(); }/*from w ww . j a va 2 s .co m*/ if (cookieSalt != null) { return cookieSalt; } final KeyGenerator keygen = KeyGenerator.getInstance("HmacSHA1"); keygen.init(COOKIE_SIZE * Byte.SIZE); cookieSalt = keygen.generateKey(); try (ObjectOutputStream oos = new ObjectOutputStream( c.openFileOutput(COOKIE_FILE, Context.MODE_PRIVATE))) { oos.writeObject(cookieSalt); } catch (IOException e) { LogUtil.logCautiously("Failed to save key file", e); return null; } } catch (NoSuchAlgorithmException e) { throw new AssertionError("failed to initialize hash functions", e); } } } } return cookieSalt; }
From source file:jef.tools.security.EncrypterUtil.java
/** * ?KEY/* w w w. j a v a2s. com*/ * * @param value * @param algom * * @return */ public static final SecretKey generateKey(String algom, int keylength) { try { KeyGenerator keygen = KeyGenerator.getInstance(algom); keygen.init(keylength); SecretKey deskey = keygen.generateKey(); return deskey; } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
From source file:org.openintents.safe.CryptoHelper.java
/** * @return null if failure, otherwise hex string version of key * @author Isaac Potoczny-Jones/*from w w w .ja v a 2s . co m*/ */ public static String generateMasterKey() throws NoSuchAlgorithmException { try { KeyGenerator keygen; keygen = KeyGenerator.getInstance("AES"); keygen.init(256); SecretKey genDesKey = keygen.generateKey(); return toHexString(genDesKey.getEncoded()); } catch (NoSuchAlgorithmException e) { Log.e(TAG, "generateMasterKey(): " + e.toString()); throw e; } }
From source file:com.javiermoreno.springboot.rest.CryptographyServiceImplBlowfish.java
public CryptographyServiceImplBlowfish() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128); key = keyGenerator.generateKey();/*from www. ja va2s.c o m*/ cipherEncrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); cipherEncrypt.init(Cipher.ENCRYPT_MODE, key); cipherDecrypt = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); cipherDecrypt.init(Cipher.DECRYPT_MODE, key); }
From source file:com.thoughtworks.go.security.AESCipherProvider.java
private byte[] generateKey() throws NoSuchAlgorithmException { KeyGenerator keygen = KeyGenerator.getInstance("AES"); keygen.init(128); byte[] key = keygen.generateKey().getEncoded(); return key;//from ww w . j ava2 s .co m }
From source file:ezbake.security.service.sync.LocksmithKeySupplierTest.java
String getAESKey() throws NoSuchAlgorithmException { KeyGenerator keygenerator = KeyGenerator.getInstance("AES"); keygenerator.init(256); return Base64.encodeBase64String(keygenerator.generateKey().getEncoded()); }
From source file:org.edeoliveira.oauth2.dropwizard.oauth2.auth.CookieEncrypter.java
public CookieEncrypter() throws Exception { // Get the KeyGenerator KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM); kgen.init(BIT_LENGTH); // 192 and 256 bits may not be available // Generate the secret key specs SecretKey skey = kgen.generateKey(); byte[] secretKey = skey.getEncoded(); keySpec = new SecretKeySpec(secretKey, ALGORITHM); }
From source file:com.evolveum.midpoint.init.ConfigurableProtectorFactory.java
public void init() { Configuration config = configuration.getConfiguration(PROTECTOR_CONFIGURATION); protectorConfig = new ProtectorConfiguration(config); //Extract file if not exists if (config.getString("midpoint.home") == null) { return;//from w w w.j a v a 2 s .com } File ks = new File(protectorConfig.getKeyStorePath()); if (ks.exists()) { return; } //todo improve FileOutputStream fos = null; try { KeyStore keystore = KeyStore.getInstance("jceks"); char[] password = "changeit".toCharArray(); keystore.load(null, password); KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey secretKey = keyGen.generateKey(); keystore.setKeyEntry("default", secretKey, "midpoint".toCharArray(), null); fos = new FileOutputStream(protectorConfig.getKeyStorePath()); keystore.store(fos, password); fos.close(); } catch (Exception ex) { throw new SystemException("Couldn't generate keystore, reason: " + ex.getMessage(), ex); } finally { IOUtils.closeQuietly(fos); } }
From source file:pl.mwaleria.safecommunicator.core.cipher.CipherManager.java
public byte[] encrypt(byte[] input, PublicKey publicKey) throws CryptoException { try {//w w w . j a va 2 s .c om KeyGenerator generator = KeyGenerator.getInstance(Constants.CIPHER_SYMETRIC_ALGORITHM); generator.init(Constants.CIPHER_SYMETRIC_SIZE); SecretKey key = generator.generateKey(); byte[] aesKey = key.getEncoded(); byte[] cryptedAesKey = this.encryptAsymetric(aesKey, publicKey); Cipher cipher = Cipher.getInstance(Constants.CIPHER_SYMETRIC_INSTANCE); cipher.init(Cipher.ENCRYPT_MODE, key); return ArrayUtils.addAll(cryptedAesKey, cipher.doFinal(input)); } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException ex) { Logger.getLogger(CipherManager.class.getName()).log(Level.SEVERE, null, ex); throw new CryptoException(ex); } }
From source file:org.wildfly.security.keystore.ModifyTrackingKeyStoreTest.java
private SecretKey getSecretKey() throws GeneralSecurityException { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); return keyGen.generateKey(); }