List of usage examples for javax.crypto.spec IvParameterSpec IvParameterSpec
public IvParameterSpec(byte[] iv)
iv
as the IV. From source file:org.bigmouth.nvwa.utils.degist.DesUtils.java
public static String encrypt(String content, String key, byte[] iv) { try {/*www . j av a2 s .c o m*/ if (StringUtils.length(key) != 8) { throw new IllegalArgumentException("Key must be 8 byte"); } SecretKeySpec secretkey = new SecretKeySpec(key.getBytes(), "DES"); IvParameterSpec zeroIv = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretkey, zeroIv); byte[] encryptedData = cipher.doFinal(content.getBytes()); return Base64.encodeBase64String(encryptedData); } catch (Exception e) { LOGGER.error("encrypt:", e); return null; } }
From source file:org.cifssynchronizer.core.Security.java
public static String decrypt(String key1, String key2, String encrypted) { String str = null;/*from ww w. j a va 2 s . c om*/ try { IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted)); str = new String(original); } catch (Exception ignored) { } return str; }
From source file:edu.wright.cs.sp16.ceg3120.util.PasswordEncryptionUtility.java
/** * Encrypts a given string using AES./* w w w .ja v a 2s.c om*/ * * @param value * // String to encrypt. * @return // Returns encrypted string. */ public static String encrypt(String value) { try { IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(value.getBytes("UTF-8")); System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (RuntimeException e) { throw e; } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:Main.java
/** * Decrypts a encrypted and encoded message given a key. * @param cipherBytes// w ww. j a v a2 s. co m * @param key * @return * @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidAlgorithmParameterException * @throws NoSuchPaddingException * @throws InvalidKeyException */ public static byte[] decryptMessage(byte[] cipherBytes, SecretKey key) throws NoSuchAlgorithmException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException { Cipher cipher = Cipher.getInstance("AES"); //ipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING"); byte[] init = new byte[128 / 8]; SecureRandom secureRandom = new SecureRandom(); //secureRandom.nextBytes(init); for (int i = 0; i < 16; i++) init[i] = 0; cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(init)); byte[] textBytes = cipher.doFinal(cipherBytes); return textBytes; }
From source file:Main.java
public static String encryptData(String password, String plaintextData) throws Exception { // Thank you Mr. Nelenkov String maybeThisHelps = "http://nelenkov.blogspot.com/2012/04/using-password-based-encryption-on.html"; Log.v(TAG, maybeThisHelps);// w w w . j av a 2 s . c o m int iterationCount = 100; //because Polaroid int keyLength = 256; int saltLength = keyLength; SecureRandom random = new SecureRandom(); byte[] salt = new byte[saltLength]; random.nextBytes(salt); KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded(); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[cipher.getBlockSize()]; random.nextBytes(iv); IvParameterSpec ivParams = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivParams); byte[] ciphertext = cipher.doFinal(plaintextData.getBytes("UTF-8")); String ivToString = new String(Base64.encode(iv, 0)); String saltToString = new String(Base64.encode(salt, 0)); String ciphertextToString = new String(Base64.encode(ciphertext, 0)); Log.d(TAG, ivToString + "]" + saltToString + "]" + ciphertextToString); return (ivToString + "]" + saltToString + "]" + ciphertextToString).replace("\n", ""); }
From source file:Logi.GSeries.Libraries.Encryption.java
public static String encrypt(String decryptedString, String password) { try {/*from ww w . jav a 2 s . c om*/ // build the initialization vector (randomly). SecureRandom random = new SecureRandom(); byte initialVector[] = new byte[16]; //generate random 16 byte IV AES is always 16bytes random.nextBytes(initialVector); IvParameterSpec ivspec = new IvParameterSpec(initialVector); SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec); byte[] encrypted = cipher.doFinal(decryptedString.getBytes()); byte[] encryptedWithIV = new byte[encrypted.length + initialVector.length]; System.arraycopy(encrypted, 0, encryptedWithIV, 0, encrypted.length); System.arraycopy(initialVector, 0, encryptedWithIV, encrypted.length, initialVector.length); return Base64.encodeBase64String(encryptedWithIV); } catch (Exception ex) { Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex); return "Error"; } }
From source file:encrypt.algorithms.AESCBC.java
public String encrypt(String key1, String key2, String value) { try {// ww w . j av a 2 s .co m IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(value.getBytes()); // System.out.println("encrypted string:" // + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.avbravo.avbravoutils.crypto.Encriptador.java
public static String decrypt(String key, String encrypted) throws Exception { Cipher cipher = Cipher.getInstance(cI); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes()); byte[] enc = decodeBase64(encrypted); cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec); byte[] decrypted = cipher.doFinal(enc); return new String(decrypted); }
From source file:cr.ac.uia.SistemaGC.utils.AES.java
public static String encrypt(Long cedula, String usuario, String contrasena) { //<editor-fold defaultstate="collapsed" desc="Mtodo para cifrar contraseas"> /*// w ww . ja v a 2 s. c o m * Inspirado en: * http://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example */ try { IvParameterSpec iv = new IvParameterSpec(fitString(usuario, 16).getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(fitString(cedula.toString(), 16).getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(contrasena.getBytes()); return Base64.encodeBase64String(encrypted); } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) { e.printStackTrace(); System.err.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } return null; //</editor-fold> }
From source file:Main.java
private static IvParameterSpec IvGenerator(byte[] b) { IvParameterSpec IV = new IvParameterSpec(b); return IV; }