List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
From source file:com.glaf.core.security.SecurityUtils.java
/** * ?????,??//from w w w . j av a 2 s.c o m * * @param ctx * * @param symmetryKey * * @param pubKey * * @return String(?base64?) */ public static String generateDigitalEnvelope(SecurityContext ctx, Key symmetryKey, byte[] pubKey) { String result = null; InputStream inputStream = null; try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); inputStream = new ByteArrayInputStream(pubKey); java.security.cert.Certificate cert = cf.generateCertificate(inputStream); inputStream.close(); PublicKey publicKey = cert.getPublicKey(); Cipher cipher = Cipher.getInstance(ctx.getAsymmetryAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); result = Base64.encodeBase64String(cipher.doFinal(symmetryKey.getEncoded())); return result; } catch (Exception ex) { throw new SecurityException(ex); } finally { try { if (inputStream != null) { inputStream.close(); inputStream = null; } } catch (IOException ex) { } } }
From source file:com.hp.application.automation.tools.EncryptionUtils.java
public static String Decrypt(String text, String key) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] keyBytes = new byte[16]; byte[] b = key.getBytes("UTF-8"); int len = b.length; if (len > keyBytes.length) len = keyBytes.length;//from w w w.j a va2 s .c o m System.arraycopy(b, 0, keyBytes, 0, len); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); IvParameterSpec ivSpec = new IvParameterSpec(keyBytes); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] results = cipher.doFinal(Base64.decodeBase64(text)); return new String(results, "UTF-8"); }
From source file:com.hp.application.automation.tools.EncryptionUtils.java
public static String Encrypt(String text, String key) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] keyBytes = new byte[16]; byte[] b = key.getBytes("UTF-8"); int len = b.length; if (len > keyBytes.length) len = keyBytes.length;// w ww . j a v a2 s .com System.arraycopy(b, 0, keyBytes, 0, len); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); IvParameterSpec ivSpec = new IvParameterSpec(keyBytes); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); byte[] results = cipher.doFinal(text.getBytes("UTF-8")); return Base64.encodeBase64String(results); }
From source file:com.ibasco.agql.examples.base.BaseExample.java
/** * @see <a href="https://gist.github.com/bricef/2436364">https://gist.github.com/bricef/2436364</a> *///from w ww. ja v a 2 s . com public static String encrypt(String plainText) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE"); SecretKeySpec key = new SecretKeySpec(worldsMostSecureUnhackableKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(worldsMostSecureUnhackableIvKey.getBytes("UTF-8"))); return Base64.getEncoder().encodeToString((cipher.doFinal(padNullBytes(plainText)))); }
From source file:com.ligadata.Utils.EncryptDecryptUtils.java
/** * Decrypt text using private key./*from w w w . j a va2 s .c o m*/ * * @param algorithm * : algorithm used * @param text * :encrypted text * @param key * :The private key * @return plain text * @throws java.lang.Exception */ 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:io.zipi.common.util.AesEncrypter.java
/** * Decrypt./*from w w w. j a v a 2 s . c o m*/ * @param encryptedText the encrypted text * @param secretKey the secret key * @return the string */ public static final String decrypt(String secretKey, String encryptedText) { if (encryptedText == null) { return null; } if (encryptedText.startsWith("$CRYPT::")) { //$NON-NLS-1$ byte[] decoded = Base64.decodeBase64(encryptedText.substring(8)); Cipher cipher; try { SecretKeySpec skeySpec = keySpecFromSecretKey(secretKey); cipher = Cipher.getInstance("AES"); //$NON-NLS-1$ cipher.init(Cipher.DECRYPT_MODE, skeySpec); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) { throw new RuntimeException(e); } try { return new String(cipher.doFinal(decoded)); } catch (IllegalBlockSizeException | BadPaddingException e) { throw new RuntimeException(e); } } else { return encryptedText; } }
From source file:bioLockJ.module.agent.MailAgent.java
private static String encrypt(final String password) throws GeneralSecurityException, UnsupportedEncodingException { final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); final SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); final Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return base64Encode(pbeCipher.doFinal(password.getBytes("UTF-8"))); }
From source file:com.glaf.core.security.RSAUtils.java
/** * ?//from w ww.j av a2s . c o m * * @param publicKey * * @param data * ?? * @return ?? */ public static byte[] encrypt(PublicKey publicKey, byte[] data) throws Exception { Cipher ci = Cipher.getInstance(ALGORITHOM, DEFAULT_PROVIDER); ci.init(Cipher.ENCRYPT_MODE, publicKey); return ci.doFinal(data); }
From source file:com.cws.esolutions.security.utils.PasswordUtils.java
/** * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility * is required but encryption (obfuscation, technically) is required. * * @param value - The plain text data to encrypt * @param salt - The salt value to utilize for the request * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory * @param iterations - The number of times to loop through the keyspec * @param keyBits - The size of the key, in bits * @param algorithm - The algorithm to encrypt the data with * @param cipherInstance - The cipher instance to utilize * @param encoding - The text encoding//from www . ja v a 2 s. co m * @return The encrypted string in a reversible format * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing */ public static final String decryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException { final String methodName = PasswordUtils.CNAME + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", secretInstance); DEBUGGER.debug("Value: {}", iterations); DEBUGGER.debug("Value: {}", keyBits); DEBUGGER.debug("Value: {}", algorithm); DEBUGGER.debug("Value: {}", cipherInstance); DEBUGGER.debug("Value: {}", encoding); } String decPass = null; try { String decoded = new String(Base64.getDecoder().decode(value)); String iv = decoded.split(":")[0]; String property = decoded.split(":")[1]; SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance); PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits); SecretKey keyTmp = keyFactory.generateSecret(keySpec); SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm); Cipher pbeCipher = Cipher.getInstance(cipherInstance); pbeCipher.init(Cipher.DECRYPT_MODE, sks, new IvParameterSpec(Base64.getDecoder().decode(iv))); decPass = new String(pbeCipher.doFinal(Base64.getDecoder().decode(property)), encoding); } catch (InvalidKeyException ikx) { throw new SecurityException(ikx.getMessage(), ikx); } catch (NoSuchAlgorithmException nsx) { throw new SecurityException(nsx.getMessage(), nsx); } catch (NoSuchPaddingException npx) { throw new SecurityException(npx.getMessage(), npx); } catch (IllegalBlockSizeException ibx) { throw new SecurityException(ibx.getMessage(), ibx); } catch (BadPaddingException bpx) { throw new SecurityException(bpx.getMessage(), bpx); } catch (UnsupportedEncodingException uex) { throw new SecurityException(uex.getMessage(), uex); } catch (InvalidAlgorithmParameterException iapx) { throw new SecurityException(iapx.getMessage(), iapx); } catch (InvalidKeySpecException iksx) { throw new SecurityException(iksx.getMessage(), iksx); } return decPass; }
From source file:com.glaf.core.security.RSAUtils.java
/** * ??// w w w . j a v a2 s. c om * * @param privateKey * ? * @param data * ?? * @return ? */ public static byte[] decrypt(PrivateKey privateKey, byte[] data) throws Exception { Cipher ci = Cipher.getInstance(ALGORITHOM, DEFAULT_PROVIDER); ci.init(Cipher.DECRYPT_MODE, privateKey); return ci.doFinal(data); }