List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate, SecureRandom random) throws InvalidKeyException
From source file:Main.java
private static PKCS8EncodedKeySpec decryptPrivateKey(byte[] encryptedPrivateKey) throws GeneralSecurityException { EncryptedPrivateKeyInfo epkInfo; try {// ww w . j a v a2 s .com epkInfo = new EncryptedPrivateKeyInfo(encryptedPrivateKey); } catch (IOException ex) { // Probably not an encrypted key. return null; } char[] password = System.console().readPassword("Password for the private key file: "); SecretKeyFactory skFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName()); Key key = skFactory.generateSecret(new PBEKeySpec(password)); Arrays.fill(password, '\0'); Cipher cipher = Cipher.getInstance(epkInfo.getAlgName()); cipher.init(Cipher.DECRYPT_MODE, key, epkInfo.getAlgParameters()); try { return epkInfo.getKeySpec(cipher); } catch (InvalidKeySpecException ex) { System.err.println("Password may be bad."); throw ex; } }
From source file:licenceexecuter.Encryptor.java
public static String encrypt(String key, String initVector, String value) { try {/*from w ww . j av a 2s .c om*/ 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:com.rdonasco.security.utils.EncryptionUtil.java
public static String encryptWithPassword(String stringToEncrypt, String password) throws Exception { PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), SALT, COUNT); SecretKey pbeKey = getKeyFactory().generateSecret(pbeKeySpec); Cipher pbeCipher = Cipher.getInstance(CIPHER_KEY_SPEC); pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, PBE_PARAM_SPEC); byte[] encryptedBytes = pbeCipher.doFinal(stringToEncrypt.getBytes()); return Base64.encodeBase64String(encryptedBytes); }
From source file:com.rdonasco.security.utils.EncryptionUtil.java
public static String decryptWithPassword(String encryptedString, String password) throws Exception { PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), SALT, COUNT); SecretKey pbeKey = getKeyFactory().generateSecret(pbeKeySpec); Cipher pbeCipher = Cipher.getInstance(CIPHER_KEY_SPEC); pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, PBE_PARAM_SPEC); byte[] decryptedBytes = pbeCipher.doFinal(Base64.decodeBase64(encryptedString)); return new String(decryptedBytes); }
From source file:it.latraccia.pkcs11.reader.util.AESUtil.java
public static String decryptString(String encryptedText, String password) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { String decrypted = null;//from w ww.j ava2 s .c o m byte[] key = password.getBytes(); if (key.length != 16) { throw new IllegalArgumentException("Invalid key size."); } byte[] value = Base64.decodeBase64(encryptedText); // Decrypt with AES/CBC/PKCS5Padding SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16])); byte[] original = cipher.doFinal(value); decrypted = new String(original); return decrypted; }
From source file:it.latraccia.pkcs11.reader.util.AESUtil.java
public static String encryptString(String clearText, String password) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { String encrypted;//from w ww .j a v a2s. co m byte[] key = password.getBytes(); // Check the length of the password if (key.length != 16) { throw new IllegalArgumentException("Invalid password length."); } byte[] value = clearText.getBytes(); // Encrypt with AES/CBC/PKCS5Padding SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16])); byte[] encryptedBytes = cipher.doFinal(value); // Encode the bytes in base64 encrypted = Base64.encodeBase64String(encryptedBytes); return encrypted; }
From source file:cr.ac.uia.SistemaGC.utils.AES.java
public static String encrypt(Long cedula, String usuario, String contrasena) { //<editor-fold defaultstate="collapsed" desc="Mtodo para cifrar contraseas"> /*/*from ww w.j av a 2 s. c o m*/ * Inspirado en: * http://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example */ try { IvParameterSpec iv = new IvParameterSpec(fitString(usuario, 16).getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(fitString(cedula.toString(), 16).getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(contrasena.getBytes()); return Base64.encodeBase64String(encrypted); } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) { e.printStackTrace(); System.err.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } return null; //</editor-fold> }
From source file:Main.java
public static byte[] DESTemplet(byte[] data, byte[] key, String algorithm, String transformation, boolean isEncrypt) { try {/*from ww w. j av a2 s. c om*/ SecretKeySpec keySpec = new SecretKeySpec(key, algorithm); Cipher cipher = Cipher.getInstance(transformation); SecureRandom random = new SecureRandom(); cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random); return cipher.doFinal(data); } catch (Throwable e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static byte[] desTemplate(byte[] data, byte[] key, String algorithm, String transformation, boolean isEncrypt) { if (data == null || data.length == 0 || key == null || key.length == 0) return null; try {/*from w w w .j a v a 2 s. co m*/ SecretKeySpec keySpec = new SecretKeySpec(key, algorithm); Cipher cipher = Cipher.getInstance(transformation); SecureRandom random = new SecureRandom(); cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random); return cipher.doFinal(data); } catch (Throwable e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception { Key deskey = null;/*from www.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; }