List of usage examples for javax.crypto SecretKey getAlgorithm
public String getAlgorithm();
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5"); SecretKey key = keyGen.generateKey(); Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key);// ww w .j a va 2 s . c o m String str = "This message will be digested"; byte[] utf8 = str.getBytes("UTF8"); byte[] digest = mac.doFinal(utf8); String digestB64 = new sun.misc.BASE64Encoder().encode(digest); System.out.println(digestB64); }
From source file:Main.java
public static String getBytesMacEncrypt(byte[] bytes, SecretKey key) { String algorithm = key.getAlgorithm(); if (algorithm.equals(HmacMD5) || algorithm.equals(HmacSHA1) || algorithm.equals(HmacSHA256) || algorithm.equals(HmacSHA384) || algorithm.equals(HmacSHA512)) { Mac mac = null;// www . j a v a 2 s . com try { mac = Mac.getInstance(algorithm); mac.init(key); return bytes2String(mac.doFinal(bytes)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } } return null; }
From source file:Main.java
private static Cipher getCipherFromPassphrase(String passphrase, byte[] salt, int opMode) throws GeneralSecurityException { SecretKey key = getKeyFromPassphrase(passphrase, salt); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(opMode, key, new PBEParameterSpec(salt, 100)); return cipher; }
From source file:Main.java
public static String getFileMacEncrypt(File file, SecretKey key) throws IOException { String algorithm = key.getAlgorithm(); Mac mac = null;//from w w w. ja v a 2 s. c om try { mac = Mac.getInstance(algorithm); mac.init(key); FileInputStream in = new FileInputStream(file); byte[] buffer = new byte[1024 * 1024]; int len = 0; while ((len = in.read(buffer)) > 0) { mac.update(buffer, 0, len); } in.close(); return bytes2String(mac.doFinal()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return null; }
From source file:Main.java
private static byte[] encryptHMAC(String data, String secret) throws IOException { byte[] bytes = null; try {//from w ww . ja v a2 s . c o m SecretKey secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacMD5"); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); bytes = mac.doFinal(data.getBytes("UTF-8")); } catch (GeneralSecurityException gse) { String msg = getStringFromException(gse); throw new IOException(msg); } return bytes; }
From source file:Main.java
private static Cipher getCipherFromPassphrase(String passphrase, byte[] salt, int iterations, int opMode) throws GeneralSecurityException { SecretKey key = getKeyFromPassphrase(passphrase, salt, iterations); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(opMode, key, new PBEParameterSpec(salt, iterations)); return cipher; }
From source file:com.konakart.bl.modules.payment.barclaycardsmartpayhosted.BarclaycardSmartPayHostedHMACTools.java
/** * Get a HMAC signature using the specified secret * @param secret/* w w w . j av a 2 s . co m*/ * @param signingData * @return a Base64 encoded signature */ public static String getBase64EncodedSignature(String secret, String signingData) { SecretKey key = getMacKey(secret); try { Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(getMacKey(secret)); byte[] digest = mac.doFinal(signingData.getBytes("UTF8")); return new String(Base64.encodeBase64(digest), "ASCII"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return null; }
From source file:com.konakart.bl.modules.payment.barclaycardsmartpayhosted.BarclaycardSmartPayHostedHMACTools.java
/** * Verify the signature/* w w w. jav a2 s .co m*/ * @param secret * @param sig * @param signedData * @return true if the signature is verified */ public static boolean verifyBase64EncodedSignature(String secret, String sig, String signedData) { if (secret == null || sig == null || signedData == null) return false; SecretKey key = getMacKey(secret); try { Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(getMacKey(secret)); byte[] digest = mac.doFinal(signedData.getBytes("UTF8")); return sig.equals(new String(Base64.encodeBase64(digest), "ASCII")); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return false; }
From source file:com.janrain.backplane.common.HmacHashUtils.java
private static String hmacSign(SecretKey key, String password) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException { Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key);//from www .j a va 2 s .c o m return new String(Base64.encodeBase64(mac.doFinal(password.getBytes())), UTF8_STRING_ENCODING); }
From source file:com.tesora.dve.common.PECryptoUtils.java
private static Cipher createCrypter(int mode) throws Exception { // Create the key KeySpec keySpec = new PBEKeySpec(settings.getPassword().toCharArray(), settings.getSalt(), settings.getIterations());//from w w w . ja v a 2s .c om SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(mode, key, new PBEParameterSpec(settings.getSalt(), settings.getIterations())); return cipher; }