List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:com.jopss.logico.negocio.util.CriptoUtils.java
public static String desEncode(String texto, String chave) { Cipher ecipher;/*w ww .ja v a 2 s . co m*/ SecretKey key; String encod = null; try { key = new SecretKeySpec(chave.getBytes("UTF-8"), 0, 8, "DES"); ecipher = Cipher.getInstance("DES"); ecipher.init(Cipher.ENCRYPT_MODE, key); byte[] utf8 = texto.getBytes("UTF8"); byte[] crip = ecipher.doFinal(utf8); encod = new String(Hex.encodeHex(crip)); } catch (Exception e) { log.error(e); } return encod; }
From source file:Main.java
public static byte[] getRSAEncryptedData(byte[] dataWithHash) { BigInteger modulus = new BigInteger( "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F", 16);/*from w ww . jav a 2s .co m*/ BigInteger pubExp = new BigInteger("010001", 16); KeyFactory keyFactory = null; try { keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp); RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherData = cipher.doFinal(dataWithHash); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (IllegalBlockSizeException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (InvalidKeyException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (BadPaddingException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (InvalidKeySpecException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (NoSuchPaddingException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } return null; }
From source file:com.app.utils.StringEncrypt.java
/** * Funcin de tipo String que recibe una llave (key), un vector de inicializacin (iv) * y el texto que se desea cifrar//w w w .jav a 2 s.co m * @param key la llave en tipo String a utilizar * @param iv el vector de inicializacin a utilizar * @param cleartext el texto sin cifrar a encriptar * @return el texto cifrado en modo String * @throws Exception puede devolver excepciones de los siguientes tipos: NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException */ public static String encrypt(String key, String iv, String cleartext) throws Exception { Cipher cipher = Cipher.getInstance(cI); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); byte[] encrypted = cipher.doFinal(cleartext.getBytes()); return new String(encodeBase64(encrypted)); }
From source file:com.formatAdmin.utils.UtilsSecurity.java
public static String cifrarMD5(String texto) { String base64EncryptedString = ""; try {//from w ww .j av a 2s . c om MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digestOfPassword = md.digest(SECRET_MD5_KEY.getBytes("utf-8")); byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); SecretKey key = new SecretKeySpec(keyBytes, ENCRYPT_ALGORITHM); Cipher cipher = Cipher.getInstance(ENCRYPT_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] plainTextBytes = texto.getBytes("utf-8"); byte[] buf = cipher.doFinal(plainTextBytes); byte[] base64Bytes = Base64.encodeBase64(buf); base64EncryptedString = new String(base64Bytes); } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException ex) { } return base64EncryptedString; }
From source file:Main.java
public static String decryp(String key, String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { String aesKey;/*from www . ja va2s . c om*/ aesKey = key.substring(0, 16); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); c.init(Cipher.DECRYPT_MODE, AESkey(key), new IvParameterSpec(aesKey.getBytes("UTF-8"))); byte[] byteStr = Base64.decode(str.getBytes(), 0); String deStr = new String(c.doFinal(byteStr), "UTF-8"); return deStr; }
From source file:Main.java
public static String encrypt(String key, String str) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { String aesKey;/*w ww. j av a 2 s . co m*/ aesKey = key.substring(0, 16); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); c.init(Cipher.ENCRYPT_MODE, AESkey(key), new IvParameterSpec(aesKey.getBytes())); byte[] encrypted = c.doFinal(str.getBytes("UTF-8")); String enStr = new String(Base64.encodeToString(encrypted, 0)); return enStr; }
From source file:com.lwr.software.reporter.utils.EncryptionUtil.java
public static String encrypt(String value) { try {//from ww w . ja va 2s. c om IvParameterSpec iv = new IvParameterSpec( DashboardConstants.INIT_VECTOR.getBytes(DashboardConstants.ENCODING)); SecretKeySpec skeySpec = new SecretKeySpec( DashboardConstants.INIT_KEY.getBytes(DashboardConstants.ENCODING), DashboardConstants.ALGORITHM); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(value.getBytes()); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:Logic.security.java
public static String symmetricDecrypt(String text, String secretKey) { Cipher cipher;//from w w w. ja va2 s .c o m String encryptedString; byte[] encryptText = null; byte[] raw; SecretKeySpec skeySpec; try { raw = Base64.decodeBase64(secretKey); skeySpec = new SecretKeySpec(raw, "AES"); encryptText = Base64.decodeBase64(text); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); encryptedString = new String(cipher.doFinal(encryptText)); } catch (Exception e) { e.printStackTrace(); return "Error"; } return encryptedString; }
From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java
public static String encrypt(String key1, String iv1, String value) { try {//from w w w . j a v a 2s .co m IvParameterSpec iv = new IvParameterSpec(iv1.getBytes(Constants.BASE_ENCODING)); SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes(Constants.BASE_ENCODING), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(value.getBytes()); //System.out.println("encrypted string:" + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; }
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 .co 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"); }