List of usage examples for javax.crypto KeyGenerator generateKey
public final SecretKey generateKey()
From source file:cn.ctyun.amazonaws.services.s3.internal.crypto.EncryptionUtils.java
/** * Generates a one-time use Symmetric Key on-the-fly for use in envelope encryption. *///w w w.j a v a 2 s . c o m public static SecretKey generateOneTimeUseSymmetricKey() { KeyGenerator generator; try { generator = KeyGenerator.getInstance(JceEncryptionConstants.SYMMETRIC_KEY_ALGORITHM); generator.init(JceEncryptionConstants.SYMMETRIC_KEY_LENGTH, new SecureRandom()); return generator.generateKey(); } catch (NoSuchAlgorithmException e) { throw new AmazonClientException("Unable to generate envelope symmetric key:" + e.getMessage(), e); } }
From source file:pt.lunacloud.services.storage.internal.crypto.EncryptionUtils.java
/** * Generates a one-time use Symmetric Key on-the-fly for use in envelope encryption. *//*from w ww . ja va 2 s . co m*/ public static SecretKey generateOneTimeUseSymmetricKey() { KeyGenerator generator; try { generator = KeyGenerator.getInstance(JceEncryptionConstants.SYMMETRIC_KEY_ALGORITHM); generator.init(JceEncryptionConstants.SYMMETRIC_KEY_LENGTH, new SecureRandom()); return generator.generateKey(); } catch (NoSuchAlgorithmException e) { throw new LunacloudClientException("Unable to generate envelope symmetric key:" + e.getMessage(), e); } }
From source file:ezbake.security.service.sync.LocksmithKeySupplierTest.java
String getAESKey() throws NoSuchAlgorithmException { KeyGenerator keygenerator = KeyGenerator.getInstance("AES"); keygenerator.init(256);/*from w w w . jav a 2s.c o m*/ return Base64.encodeBase64String(keygenerator.generateKey().getEncoded()); }
From source file:com.demandware.vulnapp.challenge.impl.ECBOracleChallenge.java
/** * Generate some key such that we don't know the key value so noone can just * use the key/*from www .j a va2 s. c o m*/ */ private SecretKey generateKey() throws NoSuchAlgorithmException { KeyGenerator keyGen = KeyGenerator.getInstance(this.keyType); return keyGen.generateKey(); }
From source file:info.fcrp.keepitsafe.bean.CryptBeanTest.java
@Test public void symetric() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(256, new SecureRandom()); Key k = kg.generateKey(); Cipher c = Cipher.getInstance("AES"); String plain = "plain"; byte[] plainBytes = plain.getBytes(); c.init(Cipher.ENCRYPT_MODE, k); c.update(plainBytes);//from w ww . j a v a2 s. co m byte[] encBytes = c.doFinal(); String enc = Base64.encodeBase64String(encBytes); assertNotSame(plain, enc); c.init(Cipher.DECRYPT_MODE, k); c.update(encBytes); byte[] decBytes = c.doFinal(); String dec = new String(decBytes); assertEquals(plain, dec); }
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 . ja va 2 s. c om } 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:org.wildfly.security.keystore.ModifyTrackingKeyStoreTest.java
private SecretKey getSecretKey() throws GeneralSecurityException { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128);//from w ww. jav a 2 s . c o m return keyGen.generateKey(); }
From source file:io.kodokojo.config.module.SecurityModule.java
private SecretKey generateAesKey() { try {/*w ww . j a v a 2 s. c o m*/ KeyGenerator kg = KeyGenerator.getInstance("AES"); return kg.generateKey(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Unable to get key generator for AES", e); } }
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 ww w. j a va 2 s. c o m*/ 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:test.unit.org.owasp.webscarab.plugin.saml.SamlTest.java
@Test public void testEncryptionAES() throws Exception { KeyGenerator keygen = KeyGenerator.getInstance("AES"); keygen.init(128);//w w w . ja v a2 s . c o m SecretKey secretKey = keygen.generateKey(); LOG.debug("secret key algo: " + secretKey.getAlgorithm()); LOG.debug("secret key format: " + secretKey.getFormat()); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); LOG.debug("cipher provider: " + cipher.getProvider().getName()); byte[] result = cipher.doFinal("hello world".getBytes()); assertNotNull(result); byte[] encodedSecretKey = secretKey.getEncoded(); LOG.debug("encoded secret key size: " + encodedSecretKey.length * 8); // decrypt cipher = Cipher.getInstance("AES"); SecretKeySpec secretKeySpec = new SecretKeySpec(encodedSecretKey, "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] decryptedResult = cipher.doFinal(result); assertEquals("hello world", new String(decryptedResult)); }