List of usage examples for javax.crypto.spec IvParameterSpec IvParameterSpec
public IvParameterSpec(byte[] iv)
iv
as the IV. From source file:edu.wright.cs.sp16.ceg3120.util.PasswordEncryptionUtility.java
/** * Decrypts a given string using AES.//w w w .j a v a 2 s . c om * * @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:com.CardPaymentGateway.Decrypt.java
public String DecrypData(String MyDecrypData) { String DecryptedData = ""; try {//from w ww.j a va 2 s.co m AlgorithmParameterSpec paramSpec = new IvParameterSpec(strPassword.getBytes()); //Whatever you want to encrypt/decrypt using AES /CBC padding Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //You can use ENCRYPT_MODE or DECRYPT_MODE cipher.init(Cipher.DECRYPT_MODE, key, paramSpec); //decode data using standard decoder byte[] output = new BASE64Decoder().decodeBuffer(MyDecrypData); // Decrypt the data byte[] decrypted = cipher.doFinal(output); // System.out.println("Original string: " + // new String(input)); // // decryptedData .; //System.out.println("Decrypted string: " + new String(decrypted)); DecryptedData = new String(decrypted); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchPaddingException ex) { Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidKeyException ex) { Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidAlgorithmParameterException ex) { Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalBlockSizeException ex) { Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex); } catch (BadPaddingException ex) { Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Decrypt.class.getName()).log(Level.SEVERE, null, ex); } return DecryptedData; }
From source file:tk.playerforcehd.networklib.shared.utils.StringCryptionUtils.java
/** * Encrypt a String with AES | You MUST use a 128 bit key! * * @param value The String to encrypt//from w ww .j a v a2 s . c om * @param key The key for the encrypted String * @return The encrypted String * @throws InvalidAlgorithmParameterException - * @throws InvalidKeyException - * @throws NoSuchPaddingException - * @throws NoSuchAlgorithmException - * @throws UnsupportedEncodingException - * @throws BadPaddingException - * @throws IllegalBlockSizeException - */ public static String encrypt(String value, String key) throws InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException { IvParameterSpec iv = new IvParameterSpec("9rh5os4n8m24gu9e".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); }
From source file:com.scorpio4.util.io.IOStreamCrypto.java
public CipherOutputStream encrypt(OutputStream out) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException { final SecretKey key = new SecretKeySpec(bytePassword, cipherSpec); final IvParameterSpec IV = new IvParameterSpec(ivBytes); final Cipher cipher = Cipher.getInstance(cipherTransformation); cipher.init(Cipher.ENCRYPT_MODE, key, IV); return new CipherOutputStream(new Base64OutputStream(out), cipher); }
From source file:org.nuclos.common.CryptUtil.java
public static byte[] encryptAES(String string, byte[] key) { if (key.length != 16) throw new IllegalArgumentException(WRONG_PASSLEN_MESSAGE); try {//from ww w.java 2 s .co m Cipher cipher = Cipher.getInstance(AES_CBC_PAD); IvParameterSpec ivSpec = new IvParameterSpec(key); SecretKeySpec skeySpec = new SecretKeySpec(key, AES); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec); return cipher.doFinal(string.getBytes("UTF-8")); } catch (Exception e) { throw new RuntimeException("encyption failure", e); } }
From source file:com.rr.familyPlanning.ui.security.encryptObject.java
/** * Encrypts and encodes the Object and IV for url inclusion * * @param input// w w w. j a v a 2 s . c o m * @return * @throws Exception */ public String[] encryptObject(Object obj) throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(stream); try { // Serialize the object out.writeObject(obj); byte[] serialized = stream.toByteArray(); // Setup the cipher and Init Vector Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[cipher.getBlockSize()]; new SecureRandom().nextBytes(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv); // Hash the key with SHA-256 and trim the output to 128-bit for the key MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(keyString.getBytes()); byte[] key = new byte[16]; System.arraycopy(digest.digest(), 0, key, 0, key.length); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); // encrypt cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); // Encrypt & Encode the input byte[] encrypted = cipher.doFinal(serialized); byte[] base64Encoded = Base64.encodeBase64(encrypted); String base64String = new String(base64Encoded); String urlEncodedData = URLEncoder.encode(base64String, "UTF-8"); // Encode the Init Vector byte[] base64IV = Base64.encodeBase64(iv); String base64IVString = new String(base64IV); String urlEncodedIV = URLEncoder.encode(base64IVString, "UTF-8"); return new String[] { urlEncodedData, urlEncodedIV }; } finally { stream.close(); out.close(); } }
From source file:com.sv.udb.controlador.CtrlContras.java
public String decrypt(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:com.hp.application.automation.tools.EncryptionUtils.java
public static String Encrypt(String text, String key) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] keyBytes = new byte[16]; byte[] b = key.getBytes("UTF-8"); int len = b.length; if (len > keyBytes.length) len = keyBytes.length;/*w w w .jav a 2 s. co m*/ System.arraycopy(b, 0, keyBytes, 0, len); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); IvParameterSpec ivSpec = new IvParameterSpec(keyBytes); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); byte[] results = cipher.doFinal(text.getBytes("UTF-8")); return Base64.encodeBase64String(results); }
From source file:Main.java
/** * More flexible AES encrypt that doesn't encode * * @param key AES key typically 128, 192 or 256 bit * @param iv Initiation Vector/*from w w w .j a v a2s . c om*/ * @param message in bytes (assumed it's already been decoded) * @return Encrypted cipher text (not encoded) * @throws GeneralSecurityException if something goes wrong during encryption */ public static byte[] encrypt(final SecretKeySpec key, final byte[] iv, final byte[] message) throws GeneralSecurityException { final Cipher cipher = Cipher.getInstance(AES_MODE); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] cipherText = cipher.doFinal(message); log("cipherText", cipherText); return cipherText; }
From source file:Main.java
/** * More flexible AES encrypt that doesn't encode * * @param key AES key typically 128, 192 or 256 bit * @param iv Initiation Vector//from ww w. j av a 2 s. co m * @param message in bytes (assumed it's already been decoded) * @return Encrypted cipher text (not encoded) * @throws GeneralSecurityException if something goes wrong during encryption */ public static byte[] encrypt(final SecretKeySpec key, final byte[] iv, final byte[] message) throws GeneralSecurityException { final Cipher cipher = Cipher.getInstance(AES_MODE); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); // cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = cipher.doFinal(message); log("cipherText", cipherText); return cipherText; }