List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
From source file:com.vico.license.util.rsa.RSAdoDecrypt.java
public static String decrypt(String cryptograph) throws Exception { Key privateKey;/* w w w.ja va 2s.c o m*/ String path = Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath(); ObjectInputStream ois = null; try { /** ? */ ois = new ObjectInputStream(new FileInputStream(path + FileNames.PRIVATEKEY_NAME)); privateKey = (Key) ois.readObject(); } catch (Exception e) { throw e; } finally { ois.close(); } /** Cipher?RSA */ Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, privateKey); /** ? */ byte[] b1 = Base64.decodeBase64(cryptograph); /** ? */ byte[] b = cipher.doFinal(b1); return new String(b); }
From source file:Main.java
/** * More flexible AES encrypt that doesn't encode * * @param key AES key typically 128, 192 or 256 bit * @param iv Initiation Vector//from w w w .j a va 2 s . co m * @param message in bytes (assumed it's already been decoded) * @return Encrypted cipher text (not encoded) * @throws GeneralSecurityException if something goes wrong during encryption */ public static byte[] encrypt(final SecretKeySpec key, final byte[] iv, final byte[] message) throws GeneralSecurityException { final Cipher cipher = Cipher.getInstance(AES_MODE); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] cipherText = cipher.doFinal(message); log("cipherText", cipherText); return cipherText; }
From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java
/** * Encrypts a string.//from ww w.j av a 2 s . com * * @param c The string to encrypt. * @return The encrypted string in HEX. */ public static String encrypt(String c, String key) { try { SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encoded = cipher.doFinal(c.getBytes()); return new String(Hex.encodeHex(encoded)); } catch (Exception e) { logger.warn("Could not encrypt string", e); return null; } }
From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java
/** * Encrypts a byte[].//from w w w. ja v a 2 s.c o m * * @param c The byte[] to encrypt. * @param key The key. * @return The encrypted array as a HEX string. */ public static String encrypt(byte[] c, String key) { try { SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encoded = cipher.doFinal(c); return new String(Hex.encodeHex(encoded)); } catch (Exception e) { logger.warn("Could not encrypt byte[]", e); return null; } }
From source file:com.wms.studio.security.utils.Digests.java
public static byte[] desEncrypt(String pwd, byte[] key) throws Exception { // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher??//from w w w. ja v a 2s.co m Cipher cipher = Cipher.getInstance(DES); // ?Cipher cipher.init(Cipher.ENCRYPT_MODE, securekey, random); return cipher.doFinal(pwd.getBytes()); }
From source file:com.wms.studio.security.utils.Digests.java
public static byte[] desDecrypt(byte[] pwd, byte[] key) throws Exception { // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher??/*from w w w . j ava 2s . co m*/ Cipher cipher = Cipher.getInstance(DES); // ?Cipher cipher.init(Cipher.DECRYPT_MODE, securekey, random); return cipher.doFinal(pwd); }
From source file:eml.studio.shared.util.Aes.java
/** * Aes Encryption /*from ww w.j a v a2s. co m*/ * @param content content to be encrypted * @param encryptKey encryption key * @return * @throws Exception */ public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); Cipher cipher = Cipher.getInstance(ALGORITHMSTR); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES")); return cipher.doFinal(content.getBytes("utf-8")); }
From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.SendingUtils.java
private static byte[] encryptEncryptionKey(SecretKey senderSecretKey, PublicKey receiverOscarKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {//ww w . j a va 2s. c o m Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, receiverOscarKey); return (cipher.doFinal(senderSecretKey.getEncoded())); }
From source file:Main.java
public static String decryp(String key, String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { String aesKey;/*from www .j a va 2 s.c om*/ aesKey = key.substring(0, 16); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); c.init(Cipher.DECRYPT_MODE, AESkey(key), new IvParameterSpec(aesKey.getBytes("UTF-8"))); byte[] byteStr = Base64.decode(str.getBytes(), 0); String deStr = new String(c.doFinal(byteStr), "UTF-8"); return deStr; }
From source file:Main.java
public static byte[] desEncrypt(byte[] data, String key) throws Exception { try {//ww w . j a va2s. co m Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); byte[] base64Key = Base64.decode(key, Base64.DEFAULT); SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES"); cipher.init(Cipher.DECRYPT_MODE, keyspec); byte[] original = cipher.doFinal(data); return original; } catch (Exception e) { e.printStackTrace(); return null; } }