List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
From source file:com.liferay.sync.engine.util.Encryptor.java
public static String encrypt(String value) throws Exception { if (value == null) { return ""; }//from w w w.ja va 2s .com SecretKey secretKey = new SecretKeySpec(_PASSWORD, _ALGORITHM); Cipher cipher = Cipher.getInstance(_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey); String salt = getSalt(); String encryptedValue = value; for (int i = 0; i < _ITERATIONS; i++) { encryptedValue = salt.concat(encryptedValue); byte[] encryptedBytes = cipher.doFinal(encryptedValue.getBytes(_UTF8_CHARSET)); encryptedValue = Base64.encodeBase64String(encryptedBytes); } return encryptedValue; }
From source file:com.salesmanager.core.util.EncryptionUtil.java
public static String decryptFromExternal(String key, String value) throws Exception { if (value == null || value.equals("")) return ""; Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes()); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] outText; outText = cipher.doFinal(hexToBytes(value)); return new String(outText); }
From source file:com.myapp.common.AES4MEncrypt.java
/** * // w w w . j a v a 2s . co m * * @param sSrc * @param sKey KEY * @return * @throws Exception * @author cdduqiang * @date 201443 */ public static String encrypt(String sSrc, String sKey) throws Exception { if (sKey == null) { log.error("Encrypt Key ??"); throw new Exception("Encrypt 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.ENCRYPT_MODE, skeySpec, iv); byte[] anslBytes = sSrc.getBytes(ENCODING);// string2AnslBytes(sSrc); byte[] encrypted = cipher.doFinal(anslBytes); return byte2hex(encrypted).toUpperCase(); }
From source file:com.clustercontrol.util.KeyCheck.java
private static byte[] decrypt(byte[] source, PublicKey publicKey) throws HinemosUnknown { m_log.trace("decrypt=" + source.length); try {//from w ww.j a va 2 s . c o m Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, publicKey); return cipher.doFinal(source); } catch (IllegalBlockSizeException ex) { m_log.warn(ex.getMessage(), ex); } catch (BadPaddingException ex) { m_log.warn(ex.getMessage(), ex); } catch (InvalidKeyException ex) { m_log.warn(ex.getMessage(), ex); } catch (NoSuchAlgorithmException ex) { m_log.warn(ex.getMessage(), ex); } catch (NoSuchPaddingException ex) { m_log.warn(ex.getMessage(), ex); } throw new HinemosUnknown("decrypt error"); }
From source file:com.clustercontrol.util.KeyCheck.java
private static byte[] encrypt(byte[] source, PrivateKey privateKey) throws HinemosUnknown { try {/*from w w w . j ava 2 s. com*/ Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(source); } catch (IllegalBlockSizeException ex) { m_log.warn(ex.getMessage(), ex); } catch (BadPaddingException ex) { m_log.warn(ex.getMessage(), ex); } catch (InvalidKeyException ex) { m_log.warn(ex.getMessage(), ex); } catch (NoSuchAlgorithmException ex) { m_log.warn(ex.getMessage(), ex); } catch (NoSuchPaddingException ex) { m_log.warn(ex.getMessage(), ex); } throw new HinemosUnknown("encrypt error"); }
From source file:com.salesmanager.core.util.EncryptionUtil.java
public static String decrypt(String key, String value) throws Exception { if (value == null || value.equals("")) return ""; // NEED TO UNDERSTAND WHY PKCS5Padding DOES NOT WORK // Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes()); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] outText; outText = cipher.doFinal(hexToBytes(value)); return new String(outText); }
From source file:com.cloudant.sync.datastore.encryption.DPKEncryptionUtil.java
/** * AES Encrypt a byte array//www .j a v a 2s. c o m * * @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); }
From source file:com.cloudant.sync.datastore.encryption.DPKEncryptionUtil.java
/** * Decrypt an AES encrypted byte array//from ww w. j a v a2s .c o m * * @param key The encryption key * @param iv The iv * @param encryptedBytes The data to decrypt * @return The decrypted data * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public static byte[] decryptAES(SecretKey key, byte[] iv, byte[] encryptedBytes) 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.DECRYPT_MODE, encryptionKey, ivParameter); return aesCipher.doFinal(encryptedBytes); }
From source file:com.screenslicer.common.Crypto.java
private static String decodeHelper(String cipherText, String encryptionKey) { if (cipherText == null || encryptionKey == null) { return null; }//w w w . ja v a 2 s. c o m try { Cipher aesCipher = Cipher.getInstance("AES"); aesCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(DigestUtils.sha256(encryptionKey), "AES"), aesCipher.getParameters()); return new String(aesCipher.doFinal(Base64.decodeBase64(cipherText)), "utf-8"); } catch (Exception e) { return null; } }
From source file:Main.java
private static String des(String id, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { int index = 0; SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DES"); Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); cipher.init(1, secretKeySpec);//from w w w. j av a2s . c o m int i = id.length(); if (i < 8) { i = 8 - i; } else { i %= 8; i = i != 0 ? 8 - i : 0; } while (index < i) { id = String.valueOf(id) + "\u0000"; index++; } return a(cipher.doFinal(id.getBytes())); }