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.lwr.software.reporter.utils.EncryptionUtil.java
public static String encrypt(String value) { try {/*from ww w.j a v a 2 s. c o m*/ IvParameterSpec iv = new IvParameterSpec( DashboardConstants.INIT_VECTOR.getBytes(DashboardConstants.ENCODING)); SecretKeySpec skeySpec = new SecretKeySpec( DashboardConstants.INIT_KEY.getBytes(DashboardConstants.ENCODING), DashboardConstants.ALGORITHM); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(value.getBytes()); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.lwr.software.reporter.utils.EncryptionUtil.java
public static String decrypt(String encrypted) { try {//from w w w. ja v a 2 s . com IvParameterSpec iv = new IvParameterSpec( DashboardConstants.INIT_VECTOR.getBytes(DashboardConstants.ENCODING)); SecretKeySpec skeySpec = new SecretKeySpec( DashboardConstants.INIT_KEY.getBytes(DashboardConstants.ENCODING), DashboardConstants.ALGORITHM); 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:Main.java
public static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) { try {/*from www.j a v a2s.c om*/ AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec); return cipher.doFinal(textBytes); } catch (Exception e) { Log.e(TAG, "Error during encryption: " + e.toString()); return errorbyte; } }
From source file:Main.java
private static Cipher genCipher(char[] pass, byte[] salt, String factory, int iterations, int mode) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { PBEKeySpec keySpec = new PBEKeySpec(pass, salt, iterations, 128); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(factory); SecretKey key = keyFactory.generateSecret(keySpec); AlgorithmParameterSpec spec = new PBEParameterSpec(salt, iterations); Cipher cipher = Cipher.getInstance(factory); cipher.init(mode, key, spec); return cipher; }
From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java
public static String decrypt(String key1, String iv1, String encrypted) { try {/*from www. ja v a2s . 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.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:Main.java
private static Cipher getCipherFromPassphrase(String passphrase, byte[] salt, int opMode) throws GeneralSecurityException { SecretKey key = getKeyFromPassphrase(passphrase, salt); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(opMode, key, new PBEParameterSpec(salt, 100)); return cipher; }
From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java
public static String encrypt(String key1, String iv1, String value) { try {/*from w w w. j av a2s . co m*/ 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
private static byte[] cryptBy3Des(byte input[], byte key[], int cryptModel, byte iv[]) throws Exception { try {// w w w . j av a 2s . c o m Key k = KeyGenerator(key); IvParameterSpec IVSpec = iv != null ? IvGenerator(iv) : IvGenerator(defaultIV); Cipher c = Cipher.getInstance(desAlgorithm); c.init(cryptModel, k, ((java.security.spec.AlgorithmParameterSpec) (IVSpec))); return c.doFinal(input); } catch (Exception e) { throw e; } }
From source file:com.bytecode.util.Crypto.java
private static byte[] decrypt(String keystring, byte[] message, int bits) throws Exception { byte[] decValue = null; byte[] nonceBytes = Arrays.copyOf(Arrays.copyOf(message, 8), 16); IvParameterSpec nonce = new IvParameterSpec(nonceBytes); Key key = generateKey(keystring, bits); Cipher c = Cipher.getInstance(ALGORITHM); c.init(Cipher.DECRYPT_MODE, key, nonce); decValue = c.doFinal(message, 8, message.length - 8); return decValue; }
From source file:Main.java
private static byte[] doFinal(String key, int opmode, byte[] input) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { key = checkNull(key) ? DEFAULT_KEY : key; if (checkNull(key)) { return null; }/*from w w w.ja v a 2 s. c o m*/ SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(MODE); cipher.init(opmode, securekey, sr); return cipher.doFinal(input); }