List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
From source file:hudson.util.Protector.java
public static String protect(String secret) { try {/*from ww w . j a v a 2s.c om*/ Cipher cipher = Secret.getCipher(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, DES_KEY); return new String(Base64.encodeBase64(cipher.doFinal((secret + MAGIC).getBytes("UTF-8")))); } catch (GeneralSecurityException e) { throw new Error(e); // impossible } catch (UnsupportedEncodingException e) { throw new Error(e); // impossible } }
From source file:ch.helmchen.camlapse.user.control.Encryption.java
/** * Verschlsselt den bergebenen String./* ww w .j a va 2 s. c om*/ * * @param aString zu verschlsselnder String. */ public static String encrypt(final String aString) throws GeneralSecurityException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return base64Encode(pbeCipher.doFinal(aString.getBytes())); }
From source file:com.fpt.crypto.CipherDemo.java
public static String encrypt(String in, String key) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException { Cipher cipher = Cipher.getInstance("AES"); byte[] inputBytes = in.getBytes(); SecretKey secKey = new SecretKeySpec(key.getBytes(), "AES"); cipher.init(Cipher.ENCRYPT_MODE, secKey); byte[] outputBytes = cipher.doFinal(inputBytes); return Hex.encodeHexString(outputBytes); }
From source file:hudson.util.Protector.java
/** * Returns null if fails to decrypt properly. *//*w ww. j a v a 2 s .c om*/ public static String unprotect(String data) { if (data == null) { return null; } try { Cipher cipher = Secret.getCipher(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, DES_KEY); String plainText = new String(cipher.doFinal(Base64.decodeBase64(data)), "UTF-8"); if (plainText.endsWith(MAGIC)) { return plainText.substring(0, plainText.length() - 3); } return null; } catch (GeneralSecurityException e) { return null; } catch (UnsupportedEncodingException e) { throw new Error(e); // impossible } catch (IOException e) { return null; } }
From source file:com.drisoftie.cwdroid.util.CredentialUtils.java
private static byte[] encrypt(byte[] key, byte[] clear) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec skeySpec = new SecretKeySpec(key, AES); Cipher cipher = getDefaultCipher(); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); return cipher.doFinal(clear); }
From source file:com.drisoftie.cwdroid.util.CredentialUtils.java
private static byte[] decrypt(byte[] key, byte[] encrypted) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec skeySpec = new SecretKeySpec(key, AES); Cipher cipher = getDefaultCipher(); cipher.init(Cipher.DECRYPT_MODE, skeySpec); return cipher.doFinal(encrypted); }
From source file:com.yukthi.utils.CryptoUtils.java
/** * Encrypts/Decrypts input string using AES algorithm * @param cipherMode/*from www . ja v a 2 s .c o m*/ * @param secretKeyStr * @param input * @return */ private static byte[] doCrypto(int cipherMode, String secretKeyStr, byte input[]) { try { Key secretKey = new SecretKeySpec(secretKeyStr.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(cipherMode, secretKey); return cipher.doFinal(input); } catch (Exception ex) { throw new InvalidArgumentException(ex, "Error encrypting/decrypting input bytes"); } }
From source file:com.example.license.DESUtil.java
public static String encryptBase64(String data, String key) throws Exception { DESKeySpec desKey = new DESKeySpec(key.getBytes()); // ?DESKeySpec?? SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, securekey); byte[] results = cipher.doFinal(data.getBytes("UTF-8")); // Encrypt// w ww. jav a 2 s.co m // byte[] unencryptedByteArray = data.getBytes("UTF8"); // byte[] encryptedBytes = encryptCipher.doFinal(unencryptedByteArray); // Encode bytes to base64 to get a string byte[] encodedBytes = Base64.encodeBase64(results); return new String(encodedBytes); }
From source file:cr.ac.uia.SistemaGC.utils.AES.java
public static String encrypt(Long cedula, String usuario, String contrasena) { //<editor-fold defaultstate="collapsed" desc="Mtodo para cifrar contraseas"> /*/*from w ww . j a v a 2 s. com*/ * Inspirado en: * http://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example */ try { IvParameterSpec iv = new IvParameterSpec(fitString(usuario, 16).getBytes("UTF-8")); SecretKeySpec skeySpec = new SecretKeySpec(fitString(cedula.toString(), 16).getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(contrasena.getBytes()); return Base64.encodeBase64String(encrypted); } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) { e.printStackTrace(); System.err.println(e.getClass().getName() + ": " + e.getMessage()); System.exit(0); } return null; //</editor-fold> }
From source file:Main.java
public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception { Key deskey = null;//from www .j a va2 s . c o m DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(keyiv); cipher.init(Cipher.DECRYPT_MODE, deskey, ips); byte[] bOut = cipher.doFinal(data); return bOut; }