List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
From source file:com.aimluck.eip.util.ALCellularUtils.java
/** * Triple DES ?????//from w ww .j a v a 2s .c o m * * @param plain * ? * @return ? * @throws Exception * ?? */ public static String crypt3Des(String key, String plain) throws Exception { String KEY_CRPTY_ALGORITHM = "DESede"; // 24???? byte[] tripleDesKeyData = new byte[24]; byte[] kyebyte = key.getBytes(); int len = kyebyte.length; for (int i = 0; i < len; i++) { tripleDesKeyData[i] = kyebyte[i]; } SecretKey secretKey = new SecretKeySpec(tripleDesKeyData, KEY_CRPTY_ALGORITHM); Cipher cipher = Cipher.getInstance(KEY_CRPTY_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedMessage = cipher.doFinal(plain.getBytes()); return new String(Base64.encodeBase64(encryptedMessage)); }
From source file:com.mb.framework.util.SecurityUtil.java
/** * /*from ww w. ja va 2 s. c o m*/ * This method is used for decrypt by using Algorithm - AES/CBC/PKCS5Padding * * @param String * @return String * @throws Exception */ @SuppressWarnings("static-access") public static String decryptAESPBKDF2(String encryptedText) throws Exception { byte[] saltBytes = salt.getBytes("UTF-8"); byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText); // Derive the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), saltBytes, pswdIterations, keySize); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); // Decrypt the message Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING_ALGO); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes)); byte[] decryptedTextBytes = null; try { decryptedTextBytes = cipher.doFinal(encryptedTextBytes); } catch (IllegalBlockSizeException e) { LOGGER.error("error " + e.getMessage()); } catch (BadPaddingException e) { LOGGER.error("error " + e.getMessage()); } return new String(decryptedTextBytes); }
From source file:Main.java
public static byte[] getEncCode(byte[] byteE, String seed) { byte[] byteFina = null; Cipher cipher = null; try {//from ww w . ja v a 2 s . c om SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES"); cipher = Cipher.getInstance("AES/CFB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); //cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); byteFina = cipher.doFinal(byteE); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; }
From source file:im.whistle.crypt.Crypt.java
/** * Decrypts a message.// w w w . j ava 2 s . c o m * @param args Arguments: enc, privateKey, sig, publicKey * @param callback Callback */ public static void decrypt(JSONArray args, AsyncCallback<JSONArray> callback) { try { // Get the arguments String enc = args.getString(0); String key = args.getString(1); String sig = null; String pub = null; if (args.length() == 4) { sig = args.getString(2); pub = args.getString(3); } Boolean ver = null; // Convert everything into byte arrays byte[] encRaw = Base64.decode(enc, Base64.DEFAULT); byte[] keyRaw = Base64.decode(stripKey(key), Base64.DEFAULT); // Verify signature if (sig != null && pub != null) { try { byte[] sigRaw = Base64.decode(sig, Base64.DEFAULT); byte[] pubRaw = Base64.decode(stripKey(pub), Base64.DEFAULT); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubRaw); KeyFactory kf = KeyFactory.getInstance("RSA", "BC"); Signature s = Signature.getInstance("SHA1withRSA", "BC"); s.initVerify(kf.generatePublic(publicKeySpec)); s.update(encRaw); ver = s.verify(sigRaw); } catch (Exception ex) { Log.i("whistle", "Verification failed: " + ex.getMessage()); ver = false; } } // Split enc into encrypted aes data and remaining enc byte[] encSplit = encRaw; byte[] aesRaw = new byte[RSA_BYTES]; System.arraycopy(encSplit, 0, aesRaw, 0, aesRaw.length); encRaw = new byte[encSplit.length - RSA_BYTES]; System.arraycopy(encSplit, RSA_BYTES, encRaw, 0, encRaw.length); // Decrypt encrypted aes data using RSAES-OAEP PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyRaw); KeyFactory kf = KeyFactory.getInstance("RSA", "BC"); Cipher c = Cipher.getInstance("RSA/None/OAEPWithSHA-1AndMGF1Padding"); c.init(Cipher.DECRYPT_MODE, kf.generatePrivate(privateKeySpec)); aesRaw = c.doFinal(aesRaw); // Decrypted enc using AES-CBC byte[] aesKey = new byte[AES_BYTES]; byte[] aesIv = new byte[aesRaw.length - aesKey.length]; System.arraycopy(aesRaw, 0, aesKey, 0, aesKey.length); System.arraycopy(aesRaw, aesKey.length, aesIv, 0, aesIv.length); c = Cipher.getInstance("AES/CBC/PKCS7Padding"); c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesKey, "AES"), new IvParameterSpec(aesIv)); byte[] dec = c.doFinal(encRaw); JSONArray res = new JSONArray(); res.put(new String(dec, "utf-8")); res.put(ver); callback.success(res); } catch (Exception ex) { Log.w("whistle", "Decrypt error:" + ex.getMessage(), ex); callback.error(ex); } }
From source file:com.shenit.commons.codec.DesUtils.java
/** * /*w w w .ja v a2s . c om*/ * @param rawData * @param rawKey * @param mode */ private static byte[] crypt(byte[] rawData, KeySpec keySpec, int mode) { // ?DESKeySpec byte[] result = null; try { // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(CODEC_DES); Key key = keyFactory.generateSecret(keySpec); if (key == null) { if (LOG.isWarnEnabled()) LOG.warn("[crypt] No key generated!"); return null; } // Cipher?? Cipher cipher = Cipher.getInstance(CODEC_DES); // DES???? cipher.init(mode, key, new SecureRandom()); // ?? // ?? result = cipher.doFinal(rawData); } catch (Exception ex) { LOG.warn("[crypt] crypt with exceptions", ex); } return result; }
From source file:gsn.http.ac.Protector.java
public static String decrypt(String value) throws Exception { Key key = generateKey();/*from w w w. j av a2s .co m*/ String salt = getSalt(); Cipher c = Cipher.getInstance(ALGORITHM); c.init(Cipher.DECRYPT_MODE, key); String dValue = null; String valueToDecrypt = value; for (int i = 0; i < ITERATIONS; i++) { byte[] decordedValue = new sun.misc.BASE64Decoder().decodeBuffer(valueToDecrypt); //byte[] decordedValue = Base64.decodeBase64(valueToDecrypt); byte[] decValue = c.doFinal(decordedValue); dValue = new String(decValue).substring(salt.length()); valueToDecrypt = dValue; } return dValue; }
From source file:com.ligadata.EncryptUtils.EncryptionUtil.java
/** * Decrypt text using private key./*w w w. j a v a 2 s .c om*/ * * @param algorithm * : algorithm used * @param text * :encrypted text * @param privateKeyFile * :The private key * @return plain text * @throws java.lang.Exception and exception is thrown */ public static String decrypt(String algorithm, byte[] text, String privateKeyFile) throws Exception { byte[] dectyptedText = null; try { ObjectInputStream inputStream = null; // Decrypt the cipher text using the private key. inputStream = new ObjectInputStream(new FileInputStream(privateKeyFile)); final PrivateKey privateKey = (PrivateKey) inputStream.readObject(); // get a cipher object and print the provider final Cipher cipher = Cipher.getInstance(algorithm); // decrypt the text using the private key cipher.init(Cipher.DECRYPT_MODE, privateKey); dectyptedText = cipher.doFinal(text); } catch (Exception e) { logger.error("Failed to decrypt given password", e); throw e; } return new String(dectyptedText); }
From source file:com.ligadata.EncryptUtils.EncryptionUtil.java
/** * Encrypt the plain text using public key. * /*from ww w . j av a2 s . c om*/ * @param algorithm * : algorithm used * @param text * : original plain text * @param publicKeyFile * :The public key file * @return Encrypted text * @throws java.lang.Exception and exception is thrown */ public static byte[] encrypt(String algorithm, String text, String publicKeyFile) throws Exception { byte[] cipherText = null; try { ObjectInputStream inputStream = null; // Encrypt the string using the public key inputStream = new ObjectInputStream(new FileInputStream(publicKeyFile)); final PublicKey publicKey = (PublicKey) inputStream.readObject(); // get a cipher object and print the provider final Cipher cipher = Cipher.getInstance(algorithm); // encrypt the plain text using the public key cipher.init(Cipher.ENCRYPT_MODE, publicKey); cipherText = cipher.doFinal(text.getBytes()); } catch (Exception e) { logger.error("Failed to encrypt given password", e); throw e; } return cipherText; }
From source file:Encrypt.java
private static String encrypt(String message) throws Exception { Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); return toHexString(cipher.doFinal(message.getBytes("UTF-8"))); }
From source file:io.syndesis.rest.v1.state.ClientSideState.java
static byte[] decrypt(final String encryptionAlgorithm, final byte[] iv, final byte[] encrypted, final SecretKey encryptionKey) { try {/*from www . j a v a2 s . c o m*/ final Cipher cipher = Cipher.getInstance(encryptionAlgorithm); cipher.init(Cipher.DECRYPT_MODE, encryptionKey, new IvParameterSpec(iv)); return cipher.doFinal(encrypted); } catch (final GeneralSecurityException e) { throw new IllegalStateException("Unable to encrypt the given value", e); } }