List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
From source file:com.redsqirl.workflow.utils.FileStream.java
private static byte[] encrypt(byte[] plaintext) throws Exception { SecretKey key = generateKey(); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 42); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec); return cipher.doFinal(plaintext); }
From source file:com.redsqirl.workflow.utils.FileStream.java
private static byte[] decrypt(byte[] ciphertext) throws Exception { SecretKey key = generateKey(); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 42); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec); return cipher.doFinal(ciphertext); }
From source file:de.scrubstudios.srvmon.agent.classes.Crypt.java
/** * This function is used to encrypt the outgoing data. * @param key Encryption key/* ww w. j a v a 2 s .c om*/ * @param data Data which will be encrypted. * @return The encrypted data string. */ public static String encrypt(String key, String data) { byte[] encryptedData = null; SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); encryptedData = cipher.doFinal(data.getBytes()); return new String(Base64.encodeBase64(encryptedData)); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:de.scrubstudios.srvmon.agent.classes.Crypt.java
/** * This function is used to decrypt incoming data. * @param key Encryption key/*from ww w. jav a 2 s. co m*/ * @param data Data string which should be decrypted. * @return The decrypted data string. */ public static String decrypt(String key, String data) { byte[] decryptedData = null; SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, keySpec); decryptedData = cipher.doFinal(Base64.decodeBase64(data)); return new String(decryptedData); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:edu.wright.cs.sp16.ceg3120.util.PasswordEncryptionUtility.java
/** * Encrypts a given string using AES.// w ww . j a v a2s .c om * * @param value * // String to encrypt. * @return // Returns encrypted string. */ public static String encrypt(String value) { try { 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("UTF-8")); System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (RuntimeException e) { throw e; } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.fpt.crypto.CipherDemo.java
public static String decrypt(String in, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, DecoderException { Cipher cipher = Cipher.getInstance("AES"); byte[] inputBytes = Hex.decodeHex(in.toCharArray()); SecretKey secKey = new SecretKeySpec(key.getBytes(), "AES"); cipher.init(Cipher.DECRYPT_MODE, secKey); byte[] outputBytes = cipher.doFinal(inputBytes); return new String(outputBytes); }
From source file:com.thoughtworks.go.server.util.EncryptionHelper.java
public static String decryptUsingRSA(String cipherText, String privateKeyContent) throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, InvalidKeySpecException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException { Cipher decryptCipher = Cipher.getInstance("RSA"); decryptCipher.init(Cipher.DECRYPT_MODE, getRSAPrivateKeyFrom(privateKeyContent)); return new String(decryptCipher.doFinal(Base64.getDecoder().decode(cipherText)), UTF_8); }
From source file:Main.java
public static byte[] encipherAes256(byte[] clearText, String keyString) throws NullPointerException { if (keyString == null || keyString.length() == 0) { throw new NullPointerException("Please give Password"); }/*ww w. ja v a 2 s. c o m*/ if (clearText == null || clearText.length <= 0) { throw new NullPointerException("Please give clearText"); } try { SecretKeySpec skeySpec = getKey(keyString); // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID final byte[] iv = new byte[16]; Arrays.fill(iv, (byte) 0x00); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); // Cipher is not thread safe Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); return cipher.doFinal(clearText); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } return null; }
From source file:de.hybris.platform.b2b.punchout.services.impl.SymmetricManager.java
public static String encrypt(final String unsecureText, final String key) throws PunchOutCipherException { String encrypted = null;/*from ww w . j ava2 s . c o m*/ try { final Key skeySpec = new SecretKeySpec(new Base64().decode(key), ALGORITHM); final Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); final byte[] encryptedValue = cipher.doFinal(unsecureText.getBytes()); encrypted = new Base64().encodeAsString(encryptedValue); } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) { // should never happen LOG.error("System was unable instantiate Cipher.", e); } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) { final String msg = "Error occured during encryption." + e.getMessage(); LOG.error(msg); throw new PunchOutCipherException(msg, e); } return encrypted; }
From source file:com.basp.trabajo_al_minuto.model.business.BusinessSecurity.java
/** * Se encarga de encriptar la contrasea ingresada por el usuario * *//* w w w . jav a2 s . c o m*/ public static String encrypt(String value) throws BusinessException { String secretKey = "e-business"; String base64EncryptedString = ""; try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); SecretKey key = new SecretKeySpec(keyBytes, "DESede"); Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] plainTextBytes = value.getBytes("utf-8"); byte[] buf = cipher.doFinal(plainTextBytes); byte[] base64Bytes = Base64.encodeBase64(buf); base64EncryptedString = new String(base64Bytes); } catch (Exception ex) { throw new BusinessException(ex); } return base64EncryptedString; }