List of usage examples for javax.crypto KeyGenerator init
public final void init(int keysize, SecureRandom random)
From source file:Main.java
private static byte[] getRawKey(byte[] seed) throws NoSuchAlgorithmException, NoSuchProviderException { KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = null;//w ww. j a v a2s . c o m if (android.os.Build.VERSION.SDK_INT >= 17) { sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); } else { sr = SecureRandom.getInstance("SHA1PRNG"); } sr.setSeed(seed); kgen.init(128, sr); //256 bits or 128 bits,192bits SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; }
From source file:Main.java
private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kGen = KeyGenerator.getInstance("AES"); SecureRandom sr;/*from w ww. j a va 2s.c o m*/ if (android.os.Build.VERSION.SDK_INT >= 17) { sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); } else { sr = SecureRandom.getInstance("SHA1PRNG"); } sr.setSeed(seed); kGen.init(128, sr); SecretKey sKey = kGen.generateKey(); return sKey.getEncoded(); }
From source file:pro.hirooka.streaming_server_for_multiple_platforms.Encrypter.java
static Key makeKey(int keyBit) throws NoSuchAlgorithmException { KeyGenerator kg = KeyGenerator.getInstance("AES"); SecureRandom rd = SecureRandom.getInstance("SHA1PRNG"); kg.init(keyBit, rd); Key key = kg.generateKey();/*from w w w. ja v a 2 s . c o m*/ return key; }
From source file:com.zacwolf.commons.crypto.Crypter_AES.java
/** * @param keyfile/* ww w . ja v a2 s . c o m*/ * @param keysize * @param salter * @throws InvalidKeySpecException * @throws IOException * @throws NoSuchAlgorithmException */ public final static void generateNewKeyFile(final File keyfile, final int keysize, final SecureRandom salter) throws InvalidKeySpecException, IOException, NoSuchAlgorithmException { if (keysize > Cipher.getMaxAllowedKeyLength(mytype)) throw new InvalidKeySpecException( "You specified a key size not supported by your current cryptographic libraries, which currently have a max key size of " + Cipher.getMaxAllowedKeyLength(mytype) + " for " + mytype); final FileOutputStream fos = new FileOutputStream(keyfile); try { final KeyGenerator kgen = KeyGenerator.getInstance(mytype); kgen.init(keysize > mykeysizemax ? mykeysizemax : keysize, salter); // 192 and 256 bits may not be available final SecretKey sk = kgen.generateKey(); fos.write(sk.getEncoded()); } finally { fos.flush(); fos.close(); ; } }
From source file:com.zacwolf.commons.crypto.Crypter_Blowfish.java
/** * //from ww w . j a v a 2s . co m * Create a new key with a custom keysize less-than equal to mykeysizemax, * specifying a custom salter * * @param keyfile * @param keysize * @param salter * @throws NoSuchAlgorithmException * @throws IOException * @throws InvalidKeySpecException */ public final static void generateNewKeyFile(final File keyfile, final int keysize, final SecureRandom salter) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException { if (keysize > Cipher.getMaxAllowedKeyLength(mytype)) throw new InvalidKeySpecException( "You specified a key size not supported by your current cryptographic libraries, which currently have a max key size of " + Cipher.getMaxAllowedKeyLength(mytype) + " for " + mytype); final FileOutputStream fos = new FileOutputStream(keyfile); try { final KeyGenerator kgen = KeyGenerator.getInstance(mytype); kgen.init(keysize > mykeysizemax ? mykeysizemax : keysize, salter); // 192 and 256 bits may not be available final SecretKey sk = kgen.generateKey(); fos.write(sk.getEncoded()); } finally { if (fos != null) { fos.flush(); fos.close(); } } }
From source file:com.glaf.core.security.SecurityUtils.java
/** * ?/*w w w . java2 s. c o m*/ * * @param ctx * * @return key */ public static Key generateSecretKey(SecurityContext ctx) { try { KeyGenerator skg = KeyGenerator.getInstance(ctx.getSymmetryKeyAlgorithm(), ctx.getJceProvider()); SecureRandom secureRandom = SecureRandom.getInstance(ctx.getSecureRandomAlgorithm()); skg.init(ctx.getSymmetryKeySize(), secureRandom); SecretKey key = skg.generateKey(); return key; } catch (Exception ex) { throw new SecurityException(ex); } }
From source file:eap.util.EDcodeUtil.java
private static byte[] aes(byte[] data, byte[] key, int keyLen, int opMode) { try {//www . j av a 2 s .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:cn.ctyun.amazonaws.services.s3.internal.crypto.EncryptionUtils.java
/** * Generates a one-time use Symmetric Key on-the-fly for use in envelope encryption. *//*www . 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 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.j a v a 2s . 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 LunacloudClientException("Unable to generate envelope symmetric key:" + e.getMessage(), 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;// www. ja v a 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 // } // }