List of usage examples for javax.crypto KeyGenerator getInstance
public static final KeyGenerator getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:TripleDES.java
/** Generate a secret TripleDES encryption/decryption key */ public static SecretKey generateKey() throws NoSuchAlgorithmException { // Get a key generator for Triple DES (a.k.a DESede) KeyGenerator keygen = KeyGenerator.getInstance("DESede"); // Use it to generate a key return keygen.generateKey(); }
From source file:com.gfw.press.encrypt.Encrypt.java
public Encrypt() { super();//from w w w . ja va 2s. c o m secureRandom = new SecureRandom(); try { cipher = Cipher.getInstance("AES/CFB/NoPadding"); keyGenerator = KeyGenerator.getInstance("AES"); } catch (NoSuchAlgorithmException | NoSuchPaddingException ex) { throw new RuntimeException(ex); } }
From source file:tagtime.random.RandomSequenceGenerator.java
/** * Creates a new <code>RandomSequenceGenerator</code> with a * user-provided key. Use this if you want consistent pseudo-random * numbers across multiple executions or generators. * @param keyString The encryption key to use. No safety checking is * performed, and weak keys will be accepted. If this is * null or empty, an automatically-generated key will be * used instead./* ww w . j a v a2s . c om*/ * @throws NoSuchAlgorithmException If Java has dropped support for * AES (not likely). * @throws NoSuchPaddingException If Java has dropped support for AES * (not likely). * @throws InvalidKeyException If the user modifies their RNG_KEY * setting to a value that cannot be used as an AES key. * This may or may not be possible (untested). */ public RandomSequenceGenerator(String keyString) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { //generate or parse the key if (keyString == null || keyString.equals("")) { key = KeyGenerator.getInstance("AES").generateKey(); } else { key = new SecretKeySpec(Base64.decodeBase64(keyString), "AES"); } //set up the cipher that will be used to generate pseudo-random numbers cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); }
From source file:org.lightjason.agentspeak.action.buildin.crypto.EAlgorithm.java
/** * generates a key// ww w . jav a 2s .com * * @return key pair object (public key / private key or null) * * @throws NoSuchAlgorithmException on algorithm error */ public final Pair<Key, Key> generateKey() throws NoSuchAlgorithmException { switch (this) { case AES: case DES: return new ImmutablePair<>(KeyGenerator.getInstance(m_key).generateKey(), null); case RSA: final KeyPair l_key = KeyPairGenerator.getInstance(m_key).generateKeyPair(); return new ImmutablePair<>(l_key.getPublic(), l_key.getPrivate()); default: throw new CIllegalStateException(CCommon.languagestring(this, "unknown", this)); } }
From source file:org.apache.ws.security.message.SymmetricSignatureTest.java
/** * Setup method/*from ww w . j a va 2 s .c o m*/ * <p/> * * @throws Exception Thrown when there is a problem in setup */ @org.junit.Before public void setUp() throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey key = keyGen.generateKey(); keyData = key.getEncoded(); }
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();// w w w. j a v a2s . c o m Cipher c = Cipher.getInstance("AES"); String plain = "plain"; byte[] plainBytes = plain.getBytes(); c.init(Cipher.ENCRYPT_MODE, k); c.update(plainBytes); 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:org.lightjason.agentspeak.action.builtin.crypto.EAlgorithm.java
/** * generates a key//from ww w.ja v a2s. c o m * * @return key pair object (public key / private key or null) * * @throws NoSuchAlgorithmException on algorithm error */ @Nonnull public final Pair<Key, Key> generateKey() throws NoSuchAlgorithmException { switch (this) { case AES: case DES: return new ImmutablePair<>(KeyGenerator.getInstance(m_key).generateKey(), null); case RSA: final KeyPair l_key = KeyPairGenerator.getInstance(m_key).generateKeyPair(); return new ImmutablePair<>(l_key.getPublic(), l_key.getPrivate()); default: throw new CIllegalStateException(CCommon.languagestring(this, "unknown", this)); } }
From source file:org.paxml.util.CryptoUtils.java
public static String decrypt(byte[] data, String password) { SecretKey SecKey = getSecretKey(password); try {/*from w w w .j a v a 2 s. c o m*/ KeyGenerator KeyGen = KeyGenerator.getInstance(KEY_TYPE); KeyGen.init(KEY_LENGTH_BITS); Cipher cipher = Cipher.getInstance(KEY_TYPE); cipher.init(Cipher.DECRYPT_MODE, SecKey); byte[] clear = cipher.doFinal(data); return new String(clear, KEY_VALUE_ENCODING); } catch (Exception e) { throw new PaxmlRuntimeException(e); } }
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 w ww .ja v a 2 s.c om*/ */ private SecretKey generateKey() throws NoSuchAlgorithmException { KeyGenerator keyGen = KeyGenerator.getInstance(this.keyType); return keyGen.generateKey(); }
From source file:ezbake.security.service.sync.LocksmithKeySupplierTest.java
String getAESKey() throws NoSuchAlgorithmException { KeyGenerator keygenerator = KeyGenerator.getInstance("AES"); keygenerator.init(256);//from w ww . j av a 2 s .c om return Base64.encodeBase64String(keygenerator.generateKey().getEncoded()); }