List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:Main.java
public static byte[] cipher(int mode, byte[] data, byte[] secret) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { SecretKeySpec secretKeySpec = new SecretKeySpec(sha256(secret), "AES"); Cipher c = Cipher.getInstance("AES"); c.init(mode, secretKeySpec);/*from w ww.j a v a 2s . co m*/ return c.doFinal(data); }
From source file:com.juicioenlinea.application.secutiry.Security.java
public static String encriptar(String texto) { final String secretKey = "hunter"; //llave para encriptar datos String encryptedString = null; try {/* w w w . j a v a2 s. c o m*/ MessageDigest md = MessageDigest.getInstance("SHA"); 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 = texto.getBytes("UTF-8"); byte[] buf = cipher.doFinal(plainTextBytes); byte[] base64Bytes = Base64.encodeBase64(buf); encryptedString = new String(base64Bytes); } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex); } return encryptedString; }
From source file:com.doculibre.constellio.utils.aes.SimpleProtector.java
public static String encrypt(String valueToEnc) throws Exception { Key key = generateKey();/*w ww .j a v a2 s. c o m*/ Cipher c = Cipher.getInstance(ALGORITHM); c.init(Cipher.ENCRYPT_MODE, key); byte[] encValue = c.doFinal(valueToEnc.getBytes()); String encryptedValue = Base64.encodeBase64String(encValue); return encryptedValue; }
From source file:com.avbravo.avbravoutils.crypto.Encriptador.java
public static String decrypt(String key, String encrypted) throws Exception { Cipher cipher = Cipher.getInstance(cI); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes()); byte[] enc = decodeBase64(encrypted); cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec); byte[] decrypted = cipher.doFinal(enc); return new String(decrypted); }
From source file:Main.java
public static byte[] decipherAes256(byte[] encrypedPwdBytes, String password) throws NullPointerException { if (password == null || password.length() == 0) { throw new NullPointerException("Please give Password"); }/*from w w w.j a v a 2 s. c o m*/ if (encrypedPwdBytes == null || encrypedPwdBytes.length <= 0) { throw new NullPointerException("Please give encrypedPwdBytes"); } try { SecretKey key = getKey(password); // 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.DECRYPT_MODE, key, ivParameterSpec); byte[] decryptedValueBytes = (cipher.doFinal(encrypedPwdBytes)); return decryptedValueBytes; } 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: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.java.demo.DesDemo.java
public static byte[] Encrypt(String str) { try {//from w ww .j a v a 2s .c o m Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey); byte[] result = cipher.doFinal(str.getBytes()); return result; } catch (Exception e) { return null; } }
From source file:de.fhdo.helper.DES.java
public static String encrypt(String Text) { try {/*from w ww. ja v a2 s . co m*/ DESKeySpec keySpec = new DESKeySpec("schluessel_stdrepository15".getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); // ENCODE plainTextPassword String byte[] cleartext = Text.getBytes("UTF8"); Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe cipher.init(Cipher.ENCRYPT_MODE, key); return Base64.encodeBase64URLSafeString(cipher.doFinal(cleartext)); } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:D_common.E_ncript.java
public static byte[] encrypt(byte[] data, String keyStr) { try {//w w w.j a va 2 s .com Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); final SecretKeySpec secretKey = new SecretKeySpec(makeAESKey(keyStr), "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:edu.tufts.vue.util.Encryption.java
public static String encrypt(String input) { try {//from w w w. j av a 2s .co m if (cipher == null) cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, getKey()); byte[] inputBytes = input.getBytes(); byte[] outputBytes = cipher.doFinal(inputBytes); String output = new String(hex.encode(outputBytes)); return output; } catch (Exception ex) { ex.printStackTrace(); return input; } }