List of usage examples for javax.crypto.spec IvParameterSpec IvParameterSpec
public IvParameterSpec(byte[] iv)
iv
as the IV. From source file:Main.java
private static byte[] decrypt(byte[] key, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); return cipher.doFinal(encrypted); }
From source file:Main.java
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; }
From source file:Main.java
public static byte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) { try {/*from w ww.j a va 2 s . c o m*/ AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec); return cipher.doFinal(textBytes); } catch (Exception e) { return errorbyte; } }
From source file:Main.java
public static String decode(String key, String ciphertext) throws Exception { byte[] bs = parseHexStr2Byte(ciphertext); IvParameterSpec ivSpec = new IvParameterSpec(HEX.getBytes()); SecretKeySpec secretKeySpec = createKey(key); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); c.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec); return new String(c.doFinal(bs), "UTF-8"); }
From source file:Main.java
public static String encode(String key, String cleartext) throws Exception { SecretKeySpec secretKeySpec = createKey(key); IvParameterSpec ivSpec = new IvParameterSpec(HEX.getBytes()); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); c.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec); String result = parseByte2HexStr(c.doFinal(cleartext.getBytes("UTF-8"))); return result; }
From source file:Main.java
public static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) { try {//from w w w . j av a2 s . c o m 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:licenceexecuter.Encryptor.java
public static String encrypt(String key, String initVector, String value) { try {/* w w w .j a va 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()); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:Main.java
public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception { Key deskey = null;//from ww w.j a v a 2 s .c o m 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.ENCRYPT_MODE, deskey, ips); byte[] bOut = cipher.doFinal(data); return bOut; }
From source file:Main.java
public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception { Key deskey = null;/*from ww w . ja va 2 s. 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:Main.java
public static byte[] getDecCode(byte[] byteD, String seed) { byte[] byteFina = null; Cipher cipher = null;/*from www .ja v a 2 s. c om*/ try { SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES"); cipher = Cipher.getInstance("AES/CFB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); byteFina = cipher.doFinal(byteD); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; }