List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:com.fortmoon.utils.SymmetricCypher.java
public String decypher(String cipher) { try {//from ww w .j a v a 2 s . co m Cipher jceCipher = Cipher.getInstance(cipherTransformation); jceCipher.init(2, secretKeySpec); byte ciphertext[] = cipher.getBytes(); byte decodedText[] = Base64.decodeBase64(ciphertext); byte cleartext[] = jceCipher.doFinal(decodedText); String res = new String(cleartext); return res; } catch (GeneralSecurityException gse) { throw new EncryptionRuntimeException((new StringBuilder("Failed to decrypt: ")).append(cipher) .append(" exception: ").append(gse.getMessage()).toString(), gse); } }
From source file:com.amazonaws.cognito.devauthsample.AESEncryption.java
private static byte[] encrypt(String clearText, String key, byte[] iv) { try {/*from w ww . j a va 2 s .c om*/ Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); AlgorithmParameters params = AlgorithmParameters.getInstance("AES"); params.init(new IvParameterSpec(iv)); cipher.init(Cipher.ENCRYPT_MODE, getKey(key), params); return cipher.doFinal(clearText.getBytes()); } catch (GeneralSecurityException e) { throw new RuntimeException("Failed to encrypt.", e); } }
From source file:de.thischwa.pmcms.tool.DESCryptor.java
public String encrypt(String plainTxt) throws CryptorException { if (plainTxt == null || plainTxt.trim().length() == 0) return null; if (key == null) key = buildKey();//from w ww . jav a 2s . com try { byte[] cleartext = plainTxt.getBytes(encoding); Cipher cipher = Cipher.getInstance(algorithm); // cipher is not thread safe cipher.init(Cipher.ENCRYPT_MODE, key); String encrypedPwd = Base64.encodeBase64String(cipher.doFinal(cleartext)); return encrypedPwd; } catch (Exception e) { throw new CryptorException(e); } }
From source file:com.rdonasco.security.utils.EncryptionUtil.java
public static String encryptWithPassword(String stringToEncrypt, String password) throws Exception { PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), SALT, COUNT); SecretKey pbeKey = getKeyFactory().generateSecret(pbeKeySpec); Cipher pbeCipher = Cipher.getInstance(CIPHER_KEY_SPEC); pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, PBE_PARAM_SPEC); byte[] encryptedBytes = pbeCipher.doFinal(stringToEncrypt.getBytes()); return Base64.encodeBase64String(encryptedBytes); }
From source file:Main.java
/** * More flexible AES encrypt that doesn't encode * @param key AES key typically 128, 192 or 256 bit * @param iv Initiation Vector//from www . j av a 2 s . c o m * @param message in bytes (assumed it's already been decoded) * @return Encrypted cipher text (not encoded) * @throws GeneralSecurityException if something goes wrong during encryption */ public static byte[] encrypt(final SecretKeySpec key, final byte[] iv, final byte[] message) throws GeneralSecurityException { final Cipher cipher = Cipher.getInstance(AES_MODE); //IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = cipher.doFinal(message); log("cipherText", cipherText); return cipherText; }
From source file:com.antonjohansson.elasticsearchshell.client.PasswordEncrypter.java
/** * Encrypts the given password, using the username as salt. * * @param username The username, used for salt. * @param password The password to encrypt. * @return Returns the encrypted password. *//*w w w .j av a2 s.c om*/ String encrypt(String username, String password) { try { String key = rightPad(username, KEY_SIZE).substring(0, KEY_SIZE); Key spec = new SecretKeySpec(key.getBytes(), algorithm); Cipher cipher = Cipher.getInstance(algorithm); cipher.init(ENCRYPT_MODE, spec); byte[] encrypted = cipher.doFinal(password.getBytes()); return Base64.getEncoder().encodeToString(encrypted); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:DesEncrypter.java
private DesEncrypter(String passPhrase) { try {// w w w .java 2 s . c o m // Create the key KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); ecipher = Cipher.getInstance(key.getAlgorithm()); dcipher = Cipher.getInstance(key.getAlgorithm()); // Prepare the parameter to the ciphers AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); // Create the ciphers ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); } catch (java.security.InvalidAlgorithmParameterException e) { } catch (java.security.spec.InvalidKeySpecException e) { } catch (javax.crypto.NoSuchPaddingException e) { } catch (java.security.NoSuchAlgorithmException e) { } catch (java.security.InvalidKeyException e) { } }
From source file:com.wso2telco.cryptosystem.AESencrp.java
/** * Decrypt./*from w ww . j av a2 s . com*/ * * @param encryptedData the encrypted data * @return the string * @throws Exception the exception */ public static String decrypt(String encryptedData) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.DECRYPT_MODE, key); //byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes()); byte[] decValue = c.doFinal(decordedValue); String decryptedValue = new String(decValue); return decryptedValue; }
From source file:com.myapp.common.AES4MEncrypt.java
/** * /*w w w .j av a2s. com*/ * * @param sSrc ? * @param sKey KEY * @return * @throws Exception * @author cdduqiang * @date 201443 */ public static String decrypt(String sSrc, String sKey) throws Exception { if (sKey == null) { log.error("Decrypt Key ??"); throw new Exception("Decrypt Key ??"); } byte[] raw = hex2byte(sKey); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] encrypted1 = hex2byte(sSrc); byte[] original = cipher.doFinal(encrypted1); return new String(original, ENCODING);// anslBytes2String(original); }
From source file:com.cloudant.sync.datastore.encryption.DPKEncryptionUtil.java
/** * AES Encrypt a byte array/*from ww w . j av a2 s . com*/ * * @param key The encryption key * @param iv The iv * @param unencryptedBytes The data to encrypt * @return The encrypted data * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public static byte[] encryptAES(SecretKey key, byte[] iv, byte[] unencryptedBytes) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec ivParameter = new IvParameterSpec(iv); // see http://stackoverflow.com/a/11506343 Key encryptionKey = new SecretKeySpec(key.getEncoded(), "AES"); aesCipher.init(Cipher.ENCRYPT_MODE, encryptionKey, ivParameter); return aesCipher.doFinal(unencryptedBytes); }