List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate, SecureRandom random) throws InvalidKeyException
From source file:com.aast.encrypt.EncryptManager.java
public static String decryptAES(String key, String encrypted) { try {//from www . jav a 2s.c om byte[] keyBytes = getKeyFromString(key); IvParameterSpec iv = new IvParameterSpec(keyBytes); SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted)); return new String(original); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.aast.encrypt.EncryptManager.java
public static String encryptAES(String key, String value) { try {/*w ww .j a v a 2 s . c om*/ byte[] keyBytes = getKeyFromString(key); IvParameterSpec iv = new IvParameterSpec(keyBytes); SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "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 byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception { Key deskey = null;//from w w w . j a v a 2s . c om DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(keyiv); cipher.init(Cipher.DECRYPT_MODE, deskey, ips); byte[] bOut = cipher.doFinal(data); return bOut; }
From source file:com.elle.analyster.admissions.AESCrypt.java
public static String decrypt(String key, String initVector, String encrypted) { try {/*from w w w . j av a2s . c om*/ 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.DECRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted)); return new String(original); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.elle.analyster.admissions.AESCrypt.java
public static String encrypt(String key, String initVector, String value) { try {// ww w . jav a 2s. c o m 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()); System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:edu.wright.cs.sp16.ceg3120.util.PasswordEncryptionUtility.java
/** * Decrypts a given string using AES.// ww w . j a va 2 s . c o m * * @param encrypted * // Encrypted string. * @return // Returns decrypted string. */ public static String decrypt(String encrypted) { 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.DECRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted)); System.out.println("Decrypted Password: " + new String(original, "UTF-8")); return new String(original, "UTF-8"); } catch (RuntimeException e) { throw e; } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:edu.wright.cs.sp16.ceg3120.util.PasswordEncryptionUtility.java
/** * Encrypts a given string using AES.// www . ja v a 2 s .c o m * * @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
public static byte[] encipherAes256(byte[] clearText, String keyString) throws NullPointerException { if (keyString == null || keyString.length() == 0) { throw new NullPointerException("Please give Password"); }/*from ww w .j av a 2 s. c o m*/ if (clearText == null || clearText.length <= 0) { throw new NullPointerException("Please give clearText"); } try { SecretKeySpec skeySpec = getKey(keyString); // 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.ENCRYPT_MODE, skeySpec, ivParameterSpec); return cipher.doFinal(clearText); } 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.tesora.dve.common.PECryptoUtils.java
private static Cipher createCrypter(int mode) throws Exception { // Create the key KeySpec keySpec = new PBEKeySpec(settings.getPassword().toCharArray(), settings.getSalt(), settings.getIterations());/* www . ja va2s .co m*/ SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(mode, key, new PBEParameterSpec(settings.getSalt(), settings.getIterations())); return cipher; }
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 ww w .ja v a2 s .co m 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; }