List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:com.jsmartframework.web.manager.AuthEncrypter.java
private static Cipher getEncryptCipher(HttpServletRequest request, String key) throws Exception { Cipher encryptCipher = (Cipher) request.getAttribute(REQUEST_AUTH_ENCRYPT_CIPHER); if (encryptCipher == null) { encryptCipher = Cipher.getInstance("AES"); SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF8"), "AES"); encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey); request.setAttribute(REQUEST_AUTH_ENCRYPT_CIPHER, encryptCipher); }// w w w . ja v a 2 s. c o m return encryptCipher; }
From source file:com.liferay.sync.engine.util.Encryptor.java
public static String encrypt(String value) throws Exception { if (value == null) { return ""; }//from ww w .j a v a 2 s.c om SecretKey secretKey = new SecretKeySpec(_PASSWORD, _ALGORITHM); Cipher cipher = Cipher.getInstance(_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey); String salt = getSalt(); String encryptedValue = value; for (int i = 0; i < _ITERATIONS; i++) { encryptedValue = salt.concat(encryptedValue); byte[] encryptedBytes = cipher.doFinal(encryptedValue.getBytes(_UTF8_CHARSET)); encryptedValue = Base64.encodeBase64String(encryptedBytes); } return encryptedValue; }
From source file:aajavafx.Kripto.java
public String encrypt(String plainText) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8"))); byte[] byteCipher = cipher.doFinal(plainText.getBytes("UTF-8")); Base64 base64 = new Base64(); String stringToStore = new String(base64.encode(byteCipher)); //byte[] restoredBytes = Base64.decode(stringToStore.getBytes()); return stringToStore; }
From source file:Main.java
/** * Compute the HMAC with SHA-256 of data, as defined in * http://tools.ietf.org/html/rfc2104#section-2 . * @param key The key byte array.//from w ww. j av a 2 s .c o m * @param data The input byte buffer. This does not change the position. * @return The HMAC result. */ public static byte[] computeHmacWithSha256(byte[] key, ByteBuffer data) { final String algorithm = "HmacSHA256"; Mac mac; try { mac = Mac.getInstance(algorithm); } catch (NoSuchAlgorithmException ex) { // Don't expect this to happen. throw new Error("computeHmac: " + algorithm + " is not supported: " + ex.getMessage()); } try { mac.init(new SecretKeySpec(key, algorithm)); } catch (InvalidKeyException ex) { // Don't expect this to happen. throw new Error("computeHmac: Can't init " + algorithm + " with key: " + ex.getMessage()); } int savePosition = data.position(); mac.update(data); data.position(savePosition); return mac.doFinal(); }
From source file:de.marx_labs.utilities.common.util.HashUtil.java
public static String hmacSha1(String value, String key) { try {/* w w w .j ava 2s . co m*/ // Get an hmac_sha1 key from the raw key bytes byte[] keyBytes = key.getBytes(); SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1"); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(value.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String return new String(hexBytes, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.ai.smart.bottom.helper.MacUtils.java
public static String hmacsha256(String secret, String data) { Mac mac = null;/* w w w .j a v a 2s .c om*/ byte[] doFinal = null; try { mac = Mac.getInstance(HMAC_ALGORITHM); //??MD5 byte[] dataBytes = DigestUtils.md5(data); //sourcekeyMD5, SecretKey secretkey = new SecretKeySpec(DigestUtils.md5(secret), HMAC_ALGORITHM); mac.init(secretkey); //HmacSHA256 doFinal = mac.doFinal(dataBytes); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { } String checksum = Hex.encodeHexString(doFinal).toLowerCase(); return checksum; }
From source file:Main.java
public static SecretKey generateHmacKey(String keyString, String algorithm) { SecretKey key = new SecretKeySpec(keyString.getBytes(), algorithm); return key;/* w ww .j a v a 2 s. c o m*/ }
From source file:Main.java
/** * * @param key/*from w w w .j a v a2s. c om*/ * @param algo * @return * @throws UnsupportedEncodingException * @throws NoSuchAlgorithmException * @throws InvalidKeyException */ static Mac getMac(String key, String algo) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(KEY_ENCODING), algo); Mac mac = Mac.getInstance(algo); mac.init(secretKeySpec); return mac; }
From source file:de.scrubstudios.srvmon.notificator.classes.Crypt.java
public static String decrypt(String key, String data) { byte[] decryptedData = null; SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); try {//from w w w .j a v a 2 s.co m Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, keySpec); //decryptedData = cipher.doFinal(Base64.decode(data)); decryptedData = cipher.doFinal(Base64.decodeBase64(data)); //return new String(decryptedData); return new String(decryptedData); } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:com.chumbok.aauth.otp.TOTP.java
/** * This method uses the JCE to provide the crypto algorithm. HMAC computes a * Hashed Message Authentication Code with the crypto hash algorithm as a * parameter./*from w w w . j av a2 s .co m*/ * * @param crypto * : the crypto algorithm (HmacSHA1, HmacSHA256, HmacSHA512) * @param keyBytes * : the bytes to use for the HMAC key * @param text * : the message or text to be authenticated */ private static byte[] hmac_sha(String crypto, byte[] keyBytes, byte[] text) { try { Mac hmac; hmac = Mac.getInstance(crypto); SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW"); hmac.init(macKey); return hmac.doFinal(text); } catch (GeneralSecurityException gse) { throw new UndeclaredThrowableException(gse); } }