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.vmware.fdmsecprotomgmt.PasswdEncrypter.java
/** * Encrypt the value with key provided/*from w w w . ja v a 2s. co m*/ */ private static String encrypt(String key, String value) { String encryptedString = null; try { IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.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()); encryptedString = Base64.encodeBase64String(encrypted); } catch (Exception ex) { System.out.println("Caught exception while encrypting string : " + value); ex.printStackTrace(); } return encryptedString; }
From source file:com.vmware.fdmsecprotomgmt.PasswdEncrypter.java
/** * Decrypt the encrypted value with provided key *///from w w w .ja v a2 s. c o m public static String decrypt(String key, String encryptedValue) { String decryptedString = null; try { IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.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); decryptedString = new String(cipher.doFinal(Base64.decodeBase64(encryptedValue))); } catch (Exception ex) { System.out.println("Caught exception while decrypting string"); ex.printStackTrace(); } return decryptedString; }
From source file:io.stallion.utils.Encrypter.java
private static String doDecryptString(String password, String encryptedBase32) throws Exception { encryptedBase32 = StringUtils.strip(encryptedBase32, "="); String salt = encryptedBase32.substring(0, 16); String ivString = encryptedBase32.substring(16, 48); byte[] iv = new byte[0]; try {/*from www. j a v a 2s .c o m*/ iv = Hex.decodeHex(ivString.toCharArray()); } catch (DecoderException e) { throw new RuntimeException(e); } encryptedBase32 = encryptedBase32.substring(48); Base32 decoder = new Base32(); byte[] encrypted = decoder.decode(encryptedBase32.toUpperCase()); SecretKeySpec skeySpec = makeKeySpec(password, salt); /* Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv)); */ Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new GCMParameterSpec(128, iv)); byte[] original = cipher.doFinal(encrypted); return new String(original, Charset.forName("UTF-8")); }
From source file:com.amazonaws.tvm.AESEncryption.java
public static byte[] encrypt(String clearText, 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.ENCRYPT_MODE, getKey(key), params); return cipher.doFinal(clearText.getBytes()); }
From source file:io.stallion.utils.Encrypter.java
public static String encryptString(String password, String value) { String salt = KeyGenerators.string().generateKey(); SecretKeySpec skeySpec = makeKeySpec(password, salt); byte[] iv = KeyGenerators.secureRandom(16).generateKey(); String ivString = Hex.encodeHexString(iv); try {/* ww w. java2 s. c om*/ Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new GCMParameterSpec(128, iv)); /* Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv)); */ byte[] encrypted = cipher.doFinal(value.getBytes(Charset.forName("UTF-8"))); String s = StringUtils.strip(new Base32().encodeAsString(encrypted), "=").toLowerCase(); // Strip line breaks s = salt + ivString + s.replaceAll("(\\n|\\r)", ""); return s; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
static String encryptRsaB64(byte[] bytes, Key pubRsaKey) { try {//from www . j a v a2s . c o m Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC"); // cipher.init(Cipher.ENCRYPT_MODE, pubRsaKey, new SecureRandom()); SecureRandom sc = null; cipher.init(Cipher.ENCRYPT_MODE, pubRsaKey, sc); byte[] cipherText = cipher.doFinal(bytes); return encodeB64(cipherText); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.ad.mediasharing.tvmclient.AESEncryption.java
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.liusoft.dlog4j.upgrade.StringUtils.java
/** * /* w w w . j a v a 2 s . co m*/ * @param src ?? * @param key 8? * @return ?? * @throws Exception */ public static byte[] encrypt(byte[] src, byte[] key) throws Exception { // DES???? SecureRandom sr = new SecureRandom(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec?? // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance(DES); // ?Cipher cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); // ?? // ?? return cipher.doFinal(src); }
From source file:com.liusoft.dlog4j.upgrade.StringUtils.java
/** * /*w w w .j a v a2 s . co m*/ * @param src ?? * @param key 8? * @return ?? * @throws Exception */ public static byte[] decrypt(byte[] src, byte[] key) throws Exception { // DES???? SecureRandom sr = new SecureRandom(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec?? // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance(DES); // ?Cipher cipher.init(Cipher.DECRYPT_MODE, securekey, sr); // ?? // ?? return cipher.doFinal(src); }
From source file:com.vmware.bdd.security.EncryptionGuard.java
/** * Get a cipher instance when need it, not share between multiple threads * because cipher is not thread safe.//w w w. j a v a2 s. c o m * * @param opmode * the operation mode * @param key * the key * @return initialized cipher instance */ private static Cipher getCiperInternal(int opmode, Key key) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance(TRANSFORMATION); IvParameterSpec ips = new IvParameterSpec(IV_PARAMETER, 0, 16); cipher.init(opmode, key, ips); return cipher; }