List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
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;/*www . java 2 s. c om*/ 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:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java
private static byte[] encryptData(byte[] content) { byte[] outBytes = null; try {//from www. j a v a 2s . c om Key key = new SecretKeySpec(secret_key, "DESede"); Cipher cipher = Cipher.getInstance("DESede", "SunJCE"); cipher.init(Cipher.ENCRYPT_MODE, key, cipher.getParameters()); outBytes = cipher.doFinal(content); } catch (Exception ex) { //log.error("PacketUtil",ex.getMessage(), ex); return outBytes; } return outBytes; }
From source file:Logi.GSeries.Libraries.Encryption.java
public static String decrypt(String encryptedString, String password) { try {// w w w .j a v a2 s . c om byte[] encryptedWithIV = Base64.decodeBase64(encryptedString); byte initialVector[] = new byte[16]; byte[] encrypted = new byte[encryptedWithIV.length - initialVector.length]; System.arraycopy(encryptedWithIV, 0, encrypted, 0, encrypted.length); System.arraycopy(encryptedWithIV, encrypted.length, initialVector, 0, initialVector.length); IvParameterSpec ivspec = new IvParameterSpec(initialVector); SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec); byte[] original = cipher.doFinal(encrypted); return new String(original); } catch (Exception ex) { Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex); return "Error"; } }
From source file:com.miyue.util.Cryptos.java
/** * AES?, ?.// w w w. ja va 2 s . c om * * @param input * @param key ?AES? * @param mode Cipher.ENCRYPT_MODE Cipher.DECRYPT_MODE */ private static byte[] aes(byte[] input, byte[] key, int mode) { try { SecretKey secretKey = new SecretKeySpec(key, AES); Cipher cipher = Cipher.getInstance(AES); cipher.init(mode, secretKey); return cipher.doFinal(input); } catch (GeneralSecurityException e) { throw Exceptions.unchecked(e); } }
From source file:license.regist.ReadProjectInfo.java
private static byte[] rsa(byte[] key) throws Exception { PublicKey pubKey = readPublicKeyFromFile(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(2, pubKey);//w ww . j a v a2s . c o m return cipher.doFinal(key); }
From source file:com.ro.ssc.app.client.licensing.TrialKeyGenerator.java
public static String generateKey(String toEncode) { String encoded = ""; try {//from w w w .j av a 2 s. com byte[] saltEncrypt = SALT_ENCRYPT.getBytes(); SecretKeyFactory factoryKeyEncrypt = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY); SecretKey tmp = factoryKeyEncrypt.generateSecret( new PBEKeySpec(PASS_ENCRYPT.toCharArray(), saltEncrypt, ITERATIONS_ENCRYPT, KEY_LENGTH)); SecretKeySpec encryptKey = new SecretKeySpec(tmp.getEncoded(), ALGORITHM); Cipher aesCipherEncrypt = Cipher.getInstance(CIPHER); aesCipherEncrypt.init(Cipher.ENCRYPT_MODE, encryptKey); byte[] bytes = StringUtils.getBytesUtf8(toEncode); byte[] encryptBytes = aesCipherEncrypt.doFinal(bytes); encoded = Base64.encodeBase64URLSafeString(encryptBytes); } catch (Exception e) { e.printStackTrace(); } return encoded; }
From source file:Logi.GSeries.Libraries.Encryption.java
public static String encrypt(String decryptedString, String password) { try {/*from ww w. j ava2 s . c om*/ // build the initialization vector (randomly). SecureRandom random = new SecureRandom(); byte initialVector[] = new byte[16]; //generate random 16 byte IV AES is always 16bytes random.nextBytes(initialVector); IvParameterSpec ivspec = new IvParameterSpec(initialVector); SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec); byte[] encrypted = cipher.doFinal(decryptedString.getBytes()); byte[] encryptedWithIV = new byte[encrypted.length + initialVector.length]; System.arraycopy(encrypted, 0, encryptedWithIV, 0, encrypted.length); System.arraycopy(initialVector, 0, encryptedWithIV, encrypted.length, initialVector.length); return Base64.encodeBase64String(encryptedWithIV); } catch (Exception ex) { Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex); return "Error"; } }
From source file:Main.java
public static byte[] encrypt(String clearText) { SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = null; byte[] cipherText = null; try {//from w w w . j a v a 2 s .com // init cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); cipherText = cipher.doFinal(clearText.getBytes()); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } return Base64.encode(cipherText, 10); }
From source file:Main.java
public static String decryptData(String encryptBase64Data, String keyBase64) { SecretKeySpec key = new SecretKeySpec(Base64.decode(keyBase64, Base64.DEFAULT), "DES"); Cipher decipher = null; try {/* w ww. j a v a 2s. c o m*/ decipher = Cipher.getInstance("DES"); decipher.init(Cipher.DECRYPT_MODE, key); byte[] encryptData = Base64.decode(encryptBase64Data, Base64.DEFAULT); return new String(decipher.doFinal(encryptData), "UTF8"); } catch (Exception e) { } return null; }
From source file:net.ftb.util.CryptoUtils.java
/** * Method to AES decrypt string if fails, will attempt to use {@link #decryptLegacy(String str, byte[] key)} * @param str string to decrypt//from w w w . j a va2 s .com * @param key decryption key key * @return decrypted string or "" if legacy fails */ public static String decrypt(String str, byte[] key) { try { Cipher aes = Cipher.getInstance("AES"); aes.init(Cipher.DECRYPT_MODE, new SecretKeySpec(pad(key), "AES")); String s = new String(aes.doFinal(Base64.decodeBase64(str)), "utf8"); if (s.startsWith("FDT:") && s.length() > 4) return s.split(":", 2)[1];//we don't want the decryption test else return decryptLegacy(str, key); } catch (Exception e) { Logger.logError("Error Decrypting information, attempting legacy decryption", e); return decryptLegacy(str, key); } }