List of usage examples for javax.crypto.spec IvParameterSpec IvParameterSpec
public IvParameterSpec(byte[] iv)
iv
as the IV. From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java
License:asdf
@Test public void testDES() throws Exception { SecretKey key = KeyGenerator.getInstance("DES").generateKey(); // for CBC; must be 8 bytes byte[] initVector = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C }; AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector); Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding"); Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding"); m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec); m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec); byte[] clearText = "?????;(*)*$(R%*PDSJF>XJIPUFIWE(*#*&$)@#*" .getBytes();//from w ww.j a v a 2 s . co m System.out.println(clearText.length); byte[] encryptedText = m_encrypter.doFinal(clearText); System.out.println(encryptedText.length); byte[] decryptedText = m_decrypter.doFinal(encryptedText); System.out.println(decryptedText.length); System.out.println(new String(clearText)); System.out.println(new String(encryptedText)); System.out.println(new String(decryptedText)); }
From source file:com.github.woki.payments.adyen.action.CSEUtil.java
static String encrypt(final Cipher aesCipher, final Cipher rsaCipher, final String plainText) throws BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException { SecretKey aesKey = aesKey(256); byte[] iv = iv(CSE_RANDOM, 12); aesCipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(iv)); byte[] encrypted = aesCipher.doFinal(plainText.getBytes()); byte[] result = new byte[iv.length + encrypted.length]; System.arraycopy(iv, 0, result, 0, iv.length); System.arraycopy(encrypted, 0, result, iv.length, encrypted.length); byte[] encryptedAESKey; try {/*w w w. j av a 2 s . c o m*/ encryptedAESKey = rsaCipher.doFinal(aesKey.getEncoded()); } catch (ArrayIndexOutOfBoundsException e) { throw new InvalidKeyException(e.getMessage()); } return String.format("%s%s%s%s%s%s", CSE_PREFIX, CSE_VERSION, CSE_SEPARATOR, Base64.encodeBase64String(encryptedAESKey), CSE_SEPARATOR, Base64.encodeBase64String(result)); }
From source file:org.sakuli.services.cipher.AesCbcCipher.java
public static IvParameterSpec readIV(final int ivSizeBytes, final InputStream is) throws IOException { final byte[] iv = new byte[ivSizeBytes]; int offset = 0; while (offset < ivSizeBytes) { final int read = is.read(iv, offset, ivSizeBytes - offset); if (read == -1) { throw new IOException("Too few bytes for IV in input stream"); }//from ww w .j ava 2s. c om offset += read; } return new IvParameterSpec(iv); }
From source file:org.gumtree.security.EncryptionUtils.java
public static String decryptBase64(String encrytedBase64String, String key, String iv) throws Exception { byte[] data = Base64.decodeBase64(encrytedBase64String.getBytes()); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "DES"); IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes()); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] decrypted = new byte[cipher.getOutputSize(data.length)]; int dec_len = cipher.update(data, 0, data.length, decrypted, 0); dec_len += cipher.doFinal(decrypted, dec_len); return new String(decrypted).trim(); }
From source file:Main.java
private static Cipher initCipher(int mode, byte[] key, byte[] iv, String cipherAlgotirhm) { try {/*from w w w . j a va 2 s. c o m*/ Key k = toKey(key); Cipher cipher = Cipher.getInstance(cipherAlgotirhm); String CipherAlgotirhm = cipherAlgotirhm.toUpperCase(); if (CipherAlgotirhm.contains("CFB") || CipherAlgotirhm.contains("CBC")) cipher.init(mode, k, new IvParameterSpec(iv)); else cipher.init(mode, k); return cipher; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:MainClass.java
public static IvParameterSpec createCtrIvForAES() { return new IvParameterSpec("1234567812345678".getBytes()); }
From source file:ren.hankai.cordwood.core.util.EncryptionUtil.java
/** * AES /?128?256?256??JRE??// w w w.j av a 2s. c o m * * @param value * @param sk 16?128? * @param encrypt truefalse * @return base64 ?'+''/'?'-''_' * @author hankai * @since Jun 28, 2016 1:21:55 PM */ public static String aes(String value, String sk, boolean encrypt) { try { final IvParameterSpec iv = new IvParameterSpec("RandomInitVector".getBytes("UTF-8")); final SecretKeySpec skeySpec = new SecretKeySpec(sk.getBytes("UTF-8"), "AES"); final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); if (encrypt) { cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); final byte[] encrypted = cipher.doFinal(value.getBytes()); String result = Base64.encodeBase64String(encrypted); result = result.replaceAll("\\+", "-"); result = result.replaceAll("/", "_"); return result; } else { cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); value = value.replaceAll("-", "+"); value = value.replaceAll("_", "/"); final byte[] original = cipher.doFinal(Base64.decodeBase64(value)); return new String(original); } } catch (final Exception ex) { logger.error(String.format("AES encryption failed! %s\n{ Value: %s, sk: %s, encrypt:%s }\n", ex.getMessage(), value, sk, encrypt)); } return null; }
From source file:com.haulmont.cuba.web.test.PasswordEncryptionTest.java
protected String encryptPassword(String password) { SecretKeySpec key = new SecretKeySpec(PASSWORD_KEY.getBytes(), "DES"); IvParameterSpec ivSpec = new IvParameterSpec(PASSWORD_KEY.getBytes()); String result;/*w w w.ja v a2 s . com*/ try { Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); result = new String(Hex.encodeHex(cipher.doFinal(password.getBytes()))); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:com.amazonaws.cognito.sync.devauth.client.AESEncryption.java
/** * Decrypt a cipher in bytes using the specified key * /*w w w . j ava 2 s .c om*/ * @param cipherBytes encrypted bytes * @param key the key used in decryption * @param iv * @return * @throws Exception */ public static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) throws Exception { Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); AlgorithmParameters params = AlgorithmParameters.getInstance("AES"); params.init(new IvParameterSpec(iv)); cipher.init(Cipher.DECRYPT_MODE, getKey(key), params); return cipher.doFinal(cipherBytes); }
From source file:com.aast.encrypt.EncryptManager.java
public static String decryptAES(String key, String encrypted) { try {//from ww w .j av a 2s . c o m 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; }