List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:ar.gob.ambiente.servicios.gestionpersonas.entidades.util.CriptPass.java
/** * Mtodo para encriptar las contraseas//www . j ava2 s . c om * @param texto * @return */ public static String encriptar(String texto) { String secretKey = "zorbazorbas"; //llave para encriptar datos 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 = texto.getBytes("utf-8"); byte[] buf = cipher.doFinal(plainTextBytes); byte[] base64Bytes = Base64.encodeBase64(buf); base64EncryptedString = new String(base64Bytes); } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { System.out.println(ex.getMessage()); } return base64EncryptedString; }
From source file:Clases.cCifrado.java
public String Encriptar(String texto) { String secretKey = "MARSOFT"; //llave para encriptar datos String base64EncryptedString = ""; try {//from w w w .jav a 2 s . com 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 = texto.getBytes("utf-8"); byte[] buf = cipher.doFinal(plainTextBytes); byte[] base64Bytes = Base64.encodeBase64(buf); base64EncryptedString = new String(base64Bytes); } catch (Exception ex) { return "Ha habido un problema enviando datos"; } return base64EncryptedString; }
From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java
public static String decrypt(String key1, String iv1, String encrypted) { try {//from w w w .j a va 2 s . c om 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.DECRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted)); return new String(original); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.basp.trabajo_al_minuto.model.business.BusinessSecurity.java
/** * Se encarga de desencriptar la contrasea ingresada por el usuario * *//*www . j a va 2 s . co m*/ public static String decrypt(String encryptValue) throws BusinessException { String secretKey = "e-business"; String base64EncryptedString = ""; try { byte[] message = Base64.decodeBase64(encryptValue.getBytes("utf-8")); 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 decipher = Cipher.getInstance("DESede"); decipher.init(Cipher.DECRYPT_MODE, key); byte[] plainText = decipher.doFinal(message); base64EncryptedString = new String(plainText, "UTF-8"); } catch (Exception ex) { throw new BusinessException(ex); } return base64EncryptedString; }
From source file:com.elle.analyster.admissions.AESCrypt.java
public static String encrypt(String key, String initVector, String value) { try {//from w w w . jav a 2 s . co m 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()); System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted)); return Base64.encodeBase64String(encrypted); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.boulmier.machinelearning.jobexecutor.encrypted.AES.java
private void setKey(String myKey) { MessageDigest sha;// w ww . ja va 2 s. c om try { key = myKey.getBytes("UTF-8"); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); // use only first 128 bit secretKey = new SecretKeySpec(key, "AES"); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { } }
From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java
/** * Encrypts a byte[].// ww w . j a v a 2 s . c o m * * @param c The byte[] to encrypt. * @param key The key. * @return The encrypted array as a HEX string. */ public static String encrypt(byte[] c, String key) { try { SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encoded = cipher.doFinal(c); return new String(Hex.encodeHex(encoded)); } catch (Exception e) { logger.warn("Could not encrypt byte[]", e); return null; } }
From source file:Main.java
private static Key AESkey(String key) throws UnsupportedEncodingException { String aesKey;//from w w w . j a va 2s . c o m aesKey = key.substring(0, 16); byte[] keyBytes = new byte[16]; byte[] b = aesKey.getBytes("UTF-8"); int len = b.length; if (len > keyBytes.length) { len = keyBytes.length; } System.arraycopy(b, 0, keyBytes, 0, len); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); return keySpec; }
From source file:com.spectralogic.ds3client.utils.Signature.java
/** * Computes RFC 2104-compliant HMAC signature. * * @param data/* www . java 2 s. c o m*/ * The data to be signed. * @param key * The signing key. * @return * The Base64-encoded RFC 2104-compliant HMAC signature. * @throws * java.security.SignatureException when signature generation fails */ public static String calculateRFC2104HMAC(final String data, final String key) throws java.security.SignatureException { LOG.debug("String to sign: {}", data.replace("\n", "\\n")); final String result; try { // get an hmac_sha1 key from the raw key bytes final SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), HMAC_SHA1_ALGORITHM); // get an hmac_sha1 Mac instance and initialize with the signing key final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); // compute the hmac on input data bytes final byte[] rawHmac = mac.doFinal(data.getBytes(Charset.forName("UTF-8"))); result = Base64.encodeBase64String(rawHmac); } catch (final Exception e) { throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); } return result.trim(); }
From source file:com.predic8.membrane.core.interceptor.authentication.session.totp.OtpProvider.java
static Signer getSigningOracle(String secret) { try {/* w w w.j a v a 2s . c o m*/ byte[] keyBytes = decodeKey(secret); final Mac mac = Mac.getInstance("HMACSHA1"); mac.init(new SecretKeySpec(keyBytes, "")); // Create a signer object out of the standard Java MAC // implementation. return new Signer() { @Override public byte[] sign(byte[] data) { return mac.doFinal(data); } }; } catch (NoSuchAlgorithmException error) { log.error("", error); } catch (InvalidKeyException error) { log.error("", error); } return null; }