List of usage examples for javax.crypto.spec IvParameterSpec IvParameterSpec
public IvParameterSpec(byte[] iv)
iv
as the IV. From source file:Main.java
public static byte[] decipherAes256(byte[] encrypedPwdBytes, String password) throws NullPointerException { if (password == null || password.length() == 0) { throw new NullPointerException("Please give Password"); }//from www . j a v a 2s .com if (encrypedPwdBytes == null || encrypedPwdBytes.length <= 0) { throw new NullPointerException("Please give encrypedPwdBytes"); } try { SecretKey key = getKey(password); // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID final byte[] iv = new byte[16]; Arrays.fill(iv, (byte) 0x00); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); // cipher is not thread safe Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec); byte[] decryptedValueBytes = (cipher.doFinal(encrypedPwdBytes)); return decryptedValueBytes; } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } return null; }
From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java
public static String encrypt(String key1, String iv1, String value) { try {// w ww . j a v a 2s . com IvParameterSpec iv = new IvParameterSpec(iv1.getBytes(Constants.BASE_ENCODING)); SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes(Constants.BASE_ENCODING), "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:Main.java
public static String decryptData(String ciphertext, String password) throws Exception { int iterationCount = 100; //because polaroid int keyLength = 256; String[] fields = ciphertext.split("]"); byte[] iv = Base64.decode(fields[0], 0); byte[] salt = Base64.decode(fields[1], 0); byte[] cipherBytes = Base64.decode(fields[2], 0); Log.d(TAG, "ciphertext: " + ciphertext + "\n" + "iv length is " + "\n" + iv.length); 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"); IvParameterSpec ivParams = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, key, ivParams); byte[] plaintext = cipher.doFinal(cipherBytes); String plainStr = new String(plaintext, "UTF-8"); return plainStr; }
From source file:org.gumtree.security.EncryptionUtils.java
public static String encryptBase64(String input, String key, String iv) throws Exception { byte[] inputBytes = input.getBytes(); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "DES"); IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes()); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); byte[] encrypted = new byte[cipher.getOutputSize(inputBytes.length)]; int enc_len = cipher.update(inputBytes, 0, inputBytes.length, encrypted, 0); enc_len += cipher.doFinal(encrypted, enc_len); return new String(Base64.encodeBase64(encrypted)); }
From source file:Main.java
/** * Encrypts message string using a given symmetric key. * @param msg Message string to encrypt. * @param key Key to encrypt message with. * @return Byte array of encrypted and encoded message. * @throws NoSuchAlgorithmException/*from ww w .j a v a 2 s. c om*/ * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidAlgorithmParameterException */ public static byte[] encryptMessage(String msg, SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { Cipher cipher = Cipher.getInstance("AES"); 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.ENCRYPT_MODE, key, new IvParameterSpec(init)); byte[] msgBytes = msg.getBytes(); //System.out.println(android.util.Base64.encode(msgBytes, Base64.DEFAULT)); byte[] msgCipherBytes = cipher.doFinal(msgBytes); return msgCipherBytes; }
From source file:authentication.AES.java
public static String decrypt(String textoencriptado) throws Exception { Cipher decripta = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(chaveencriptacao.getBytes("UTF-8"), "AES"); decripta.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8"))); return new String(decripta.doFinal(Base64.decodeBase64(textoencriptado)), "UTF-8"); }
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:ec.edu.uce.medicina.seguimiento.util.EncryptionUtility.java
/** *Mtodo que permite la encriptacin de la contrasea. * @param cleartext/* w w w. jav a 2s .c o m*/ * @return * @throws java.lang.Exception */ public static String encrypt(String cleartext) throws Exception { String key = "02AE31B79CCCB2A3"; //llave String iv = "0123456789ABCDEF"; Cipher cipher = Cipher.getInstance(cI); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); byte[] encrypted = cipher.doFinal(cleartext.getBytes()); return new String(encodeBase64(encrypted)); }
From source file:clases.Seguridad.java
public static String desencriptar(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:Encrypt.java
private static String encrypt(String message) throws Exception { Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); return toHexString(cipher.doFinal(message.getBytes("UTF-8"))); }