List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
From source file:com.imaginary.home.cloud.Configuration.java
static public @Nonnull String decrypt(@Nonnull String keySalt, @Nonnull String value) { try {/*www .ja va 2 s. com*/ SecretKeySpec spec = new SecretKeySpec(getConfiguration().getCustomSalt(keySalt), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, spec); byte[] b64 = value.getBytes("utf-8"); byte[] raw = Base64.decodeBase64(b64); byte[] decrypted = cipher.doFinal(raw); return new String(decrypted, "utf-8"); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.aurel.track.report.query.ReportQueryBL.java
private static String encrypt(String clearText, char[] password) { // Create PBE parameter set PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count); byte[] ciphertext = { 0 }; PBEKeySpec pbeKeySpec = new PBEKeySpec(password); try {//from ww w. j a va 2s .c om SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); // Create PBE Cipher Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); // Initialize PBE Cipher with key and parameters pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); // Encrypt the cleartext ciphertext = pbeCipher.doFinal(clearText.getBytes()); } catch (Exception e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); } return new String(Base64.encodeBase64(ciphertext)); }
From source file:com.aurel.track.admin.customize.category.filter.execute.ReportQueryBL.java
private static String encrypt(String clearText, char[] password) { // Create PBE parameter set PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count); byte[] ciphertext = { 0 }; PBEKeySpec pbeKeySpec = new PBEKeySpec(password); try {/*from ww w . j av a 2 s.com*/ SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); // Create PBE Cipher Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); // Initialize PBE Cipher with key and parameters pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); // Encrypt the cleartext ciphertext = pbeCipher.doFinal(clearText.getBytes()); } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e), e); } return new String(Base64.encodeBase64(ciphertext)); }
From source file:com.myapp.common.AES4MEncrypt.java
/** * /*from www . j a va 2 s . c o m*/ * * @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.qubit.solution.fenixedu.bennu.webservices.services.client.WebServiceClientHandler.java
public static String cypher(byte[] sessionKey, String informationToCipher) { Cipher cipher; try {/*from w w w .ja v a 2 s . c o m*/ cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, "AES")); byte[] cipherData = cipher.doFinal(informationToCipher.getBytes("UTF-8")); return Base64.encodeBase64String(cipherData); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:edu.hku.sdb.udf.util.UDFHandler.java
private static boolean search(byte[] encrypted, String keyword, SecretKey prkey) { byte[] keywordbyte = keyword.getBytes(); if (keywordbyte.length != encrypted.length) { return false; }// w w w . j a v a 2s .com byte[] diff1 = new byte[keywordbyte.length - 1]; for (int i = 0; i < diff1.length; i++) { diff1[i] = (byte) (encrypted[i] ^ keywordbyte[i]); } byte diff2 = (byte) (encrypted[diff1.length] ^ keywordbyte[diff1.length]); // get diff = word xor encrypted // if it is a match, first part of diff generates second part of diff using pseudo random function try { // pseudorandom cipher Cipher prCipher = Cipher.getInstance(SCHEME); prCipher.init(Cipher.ENCRYPT_MODE, prkey); // si is chopped with the desired length byte[] fksi = prCipher.doFinal(diff1); // fksi is F_k(S_i) in the searchable encryption // we use the same AES cipher for simplicity return fksi[0] == diff2; } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; }
From source file:com.jk.security.JKEncDec.java
/** * Encrypt./*ww w. j a v a 2 s .c om*/ * * @param plainText * the plain text * @return the string */ public static String encrypt(String plainText) { try { plainText = fixText(plainText); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8"))); return toString(cipher.doFinal(plainText.getBytes("UTF-8"))); } catch (Exception e) { throw new JKSecurityException(e); } }
From source file:com.screenslicer.common.Crypto.java
private static String encodeHelper(String plainText, String encryptionKey) { if (plainText == null || encryptionKey == null) { return null; }// w ww . ja v a2 s .c o m try { Cipher aesCipher = Cipher.getInstance("AES"); aesCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(DigestUtils.sha256(encryptionKey), "AES")); return Base64.encodeBase64String(aesCipher.doFinal(plainText.getBytes("utf-8"))); } catch (Exception e) { return null; } }
From source file:com.jk.security.JKEncDec.java
/** * Decrypt.//ww w. ja va 2s.c o m * * @param cipherText * the cipher text * @return the string */ public static String decrypt(String cipherText) { try { byte[] cipherBytes = toBytes(cipherText); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8"))); return new String(cipher.doFinal(cipherBytes), "UTF-8").trim(); } catch (Exception e) { throw new JKSecurityException(e); } }
From source file:com.liferay.sync.engine.util.Encryptor.java
public static String decrypt(String value) throws Exception { if (value == null) { return ""; }// ww w. ja va 2 s . c om SecretKey secretKey = new SecretKeySpec(_PASSWORD, _ALGORITHM); Cipher cipher = Cipher.getInstance(_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey); String decryptedValue = value; for (int i = 0; i < _ITERATIONS; i++) { byte[] decodedBytes = Base64.decodeBase64(decryptedValue); byte[] decryptedBytes = cipher.doFinal(decodedBytes); decryptedValue = new String(decryptedBytes, _UTF8_CHARSET).substring(_SALT_LENGTH); } return decryptedValue; }