List of usage examples for javax.crypto KeyGenerator generateKey
public final SecretKey generateKey()
From source file:Main.java
public static String encryption(String string) throws Exception { // TODO Auto-generated method stub //generate symmetric key KeyGenerator keygt = KeyGenerator.getInstance("AES"); keygt.init(128);/*from ww w. java2s . com*/ SecretKey symkey = keygt.generateKey(); //get it encoded byte[] aes_ba = symkey.getEncoded(); //create cipher SecretKeySpec skeySpec = new SecretKeySpec(aes_ba, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); //encryption byte[] EncSymbyteArray = cipher.doFinal(string.getBytes()); //encrypt symKey with PublicKey // Key pubKey = getPublicKey(); Key pubKey = publicKey; //RSA cipher Cipher cipherAsm = Cipher.getInstance("RSA", "BC"); cipherAsm.init(Cipher.ENCRYPT_MODE, pubKey); //RSA encryption byte[] asymEncsymKey = cipherAsm.doFinal(aes_ba); // File f3 = new File(BASE_PATH,"enc.txt"); // File f3key = new File(BASE_PATH,"enckey.txt"); // File f3file = new File(BASE_PATH,"encfile.txt"); // writeToFile2(f3,f3key,f3file, asymEncsymKey, EncSymbyteArray); //byte != new String //return new String(byteMerger(asymEncsymKey, EncSymbyteArray)); return Base64.encodeToString(byteMerger(asymEncsymKey, EncSymbyteArray), Base64.DEFAULT); }
From source file:org.vosao.utils.CipherUtils.java
public static String generateKey() { try {// w w w . jav a2 s . co m KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); return new Base64().encodeToString(kgen.generateKey().getEncoded()); } catch (NoSuchAlgorithmException e) { logger.error("Error generating secret key", e); return ""; } }
From source file:Main.java
private static Key getKey(Context mContext) { if (key == null) { try {//from ww w. jav a 2s.co m KeyGenerator _generator = KeyGenerator.getInstance("DES"); _generator.init(new SecureRandom(mContext.getPackageName().getBytes())); key = _generator.generateKey(); _generator = null; } catch (Exception e) { e.printStackTrace(); } } return key; }
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);//from w w w . j a v a 2s . c o m kgen.init(128, sr); 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 = SecureRandom.getInstance("SHA1PRNG", "Crypto"); sr.setSeed(seed);/*from w w w . j a va 2s . c o m*/ kgen.init(128, sr); 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 = SecureRandom.getInstance("SHA1PRNG", "Crypto"); sr.setSeed(seed);// w w w . j a va 2s .co m kgen.init(128, sr); SecretKey sKey = kgen.generateKey(); byte[] raw = sKey.getEncoded(); return raw; }
From source file:models.logic.CipherDecipher.java
public static SecretKey generateKey() throws NoSuchAlgorithmException { KeyGenerator keyGenS = KeyGenerator.getInstance("AES"); keyGenS.init(128);//w ww .j a v a 2 s. co m SecretKey sKey = keyGenS.generateKey(); return sKey; }
From source file:Main.java
public static byte[] generateKey(String password) throws Exception { byte[] keyStart = password.getBytes("UTF-8"); KeyGenerator kgen = KeyGenerator.getInstance("AES"); SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto"); sr.setSeed(keyStart);//from w w w .j ava 2 s . c om kgen.init(128, sr); SecretKey skey = kgen.generateKey(); return skey.getEncoded(); }
From source file:com.thoughtworks.go.server.util.EncryptionHelper.java
public static SecretKey generateAESKey() throws NoSuchAlgorithmException { KeyGenerator generator = KeyGenerator.getInstance("AES"); generator.init(128); // The AES key size in number of bits return generator.generateKey(); }
From source file:org.apache.xml.security.samples.encryption.Encrypter.java
private static SecretKey GenerateAndStoreKeyEncryptionKey() throws Exception { String jceAlgorithmName = "DESede"; KeyGenerator keyGenerator = KeyGenerator.getInstance(jceAlgorithmName); SecretKey kek = keyGenerator.generateKey(); byte[] keyBytes = kek.getEncoded(); File kekFile = new File("kek"); FileOutputStream f = new FileOutputStream(kekFile); f.write(keyBytes);//from w ww . ja va2 s. co m f.close(); System.out.println("Key encryption key stored in " + kekFile.toURL().toString()); return kek; }