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.aurel.track.admin.customize.category.filter.execute.ReportQueryBL.java
private static String dcl(String encryptedText, char[] password) { byte[] clearText = { ' ' }; int count = 20; PBEKeySpec pbeKeySpec;/*from www . j a v a 2 s . c o m*/ PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); pbeKeySpec = new PBEKeySpec(password); try { keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); // Create PBE Cipher Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); // Initialize PBE Cipher with key and parameters pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); byte[] ciphertext = Base64.decodeBase64(encryptedText); //Decrypt the cleartext clearText = pbeCipher.doFinal(ciphertext); } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e), e); } return new String(clearText); }
From source file:com.avbravo.avbravoutils.crypto.Encriptador.java
public static String decrypt(String key, 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.myapp.common.AES4MEncrypt.java
/** * /*from w w w . j a va 2 s .c om*/ * * @param sSrc ? * @param sKey KEY * @return * @throws Exception * @author cdduqiang * @date 201443 */ public static String decrypt(String sSrc, String sKey) throws Exception { if (sKey == null) { log.error("Decrypt Key ??"); throw new Exception("Decrypt Key ??"); } byte[] raw = hex2byte(sKey); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] encrypted1 = hex2byte(sSrc); byte[] original = cipher.doFinal(encrypted1); return new String(original, ENCODING);// anslBytes2String(original); }
From source file:com.tremolosecurity.unison.u2f.util.U2fUtil.java
public static List<SecurityKeyData> loadUserKeys(AuthInfo userData, String challengeStoreAttribute, String encyrptionKeyName) throws Exception, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { Attribute challengeAttr = userData.getAttribs().get(challengeStoreAttribute); Type t = new TypeToken<List<KeyHolder>>() { }.getType();//from w w w.ja v a 2s . co m ArrayList<SecurityKeyData> devices = new ArrayList<SecurityKeyData>(); if (challengeAttr != null) { SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(encyrptionKeyName); if (key == null) { throw new Exception("Queue message encryption key not found"); } EncryptedMessage msg = gson.fromJson(inflate(challengeAttr.getValues().get(0)), EncryptedMessage.class); IvParameterSpec spec = new IvParameterSpec(msg.getIv()); Cipher cipher; cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, spec); byte[] bytes = cipher.doFinal(msg.getMsg()); String json = new String(bytes); java.util.List<KeyHolder> fromJSON = gson.fromJson(json, t); for (KeyHolder kh : fromJSON) { devices.add(new SecurityKeyData(kh.getEnrollmentTime(), kh.getKeyHandle(), kh.getPublicKey(), null, kh.getCounter())); } } return devices; }
From source file:Main.java
/** * Encrypts message string using a given symmetric key. * @param msg Message string to encrypt. * @param key Key to encrypt message with. * @return Byte array of encrypted and encoded message. * @throws NoSuchAlgorithmException//w w w . ja v a 2s . co m * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidAlgorithmParameterException */ public static byte[] encryptMessage(String msg, SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { Cipher cipher = Cipher.getInstance("AES"); byte[] init = new byte[128 / 8]; //SecureRandom secureRandom = new SecureRandom(); //secureRandom.nextBytes(init); for (int i = 0; i < 16; i++) init[i] = 0; cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(init)); byte[] msgBytes = msg.getBytes(); //System.out.println(android.util.Base64.encode(msgBytes, Base64.DEFAULT)); byte[] msgCipherBytes = cipher.doFinal(msgBytes); return msgCipherBytes; }
From source file:com.jk.security.JKEncDec.java
/** * Encrypt./*from w w w . j a v a 2 s . c o m*/ * * @param plainText * the plain text * @return the string */ public static String encrypt(String plainText) { try { plainText = fixText(plainText); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8"))); return toString(cipher.doFinal(plainText.getBytes("UTF-8"))); } catch (Exception e) { throw new JKSecurityException(e); } }
From source file:com.jk.security.JKEncDec.java
/** * Decrypt.//www . j a v a 2 s . c o m * * @param cipherText * the cipher text * @return the string */ public static String decrypt(String cipherText) { try { byte[] cipherBytes = toBytes(cipherText); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8"))); return new String(cipher.doFinal(cipherBytes), "UTF-8").trim(); } catch (Exception e) { throw new JKSecurityException(e); } }
From source file:com.app.utils.StringEncrypt.java
/** * Funcin de tipo String que recibe una llave (key), un vector de inicializacin (iv) * y el texto que se desea descifrar/* w ww.ja va 2s . c o m*/ * @param key la llave en tipo String a utilizar * @param iv el vector de inicializacin a utilizar * @param encrypted el texto cifrado en modo String * @return el texto desencriptado en modo String * @throws Exception puede devolver excepciones de los siguientes tipos: NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException */ public static String decrypt(String key, String iv, 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:Controller.StringEncrypt.java
public final static String encrypt(String cleartext) { byte[] encrypted = null; try {//from w w w . j a va2 s .co m Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec skeySpec = new SecretKeySpec("92AE31A79FEEB2A3".getBytes(), "AES"); IvParameterSpec ivParameterSpec = new IvParameterSpec("0123456789ABCDEF".getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); encrypted = cipher.doFinal(cleartext.getBytes()); } catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) { JOptionPane.showMessageDialog(null, "Error encriptando contrasea"); } return new String(encodeBase64(encrypted)); }
From source file:Main.java
private static Cipher initCipher(int mode, byte[] key, byte[] iv, String cipherAlgotirhm) { try {// ww w . j a v a 2s.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; }