List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:Main.java
public static byte[] encrypt(byte[] input, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); return cipher.doFinal(input); }
From source file:Main.java
public static byte[] decrypt(byte[] input, byte[] key) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec); return cipher.doFinal(input); }
From source file:Main.java
public static String hmacSha256Encode(String key, String data) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException { Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("utf-8"), "HmacSHA256"); sha256_HMAC.init(secret_key);/* ww w. j av a 2 s . c o m*/ return asHex(sha256_HMAC.doFinal(data.getBytes("utf-8"))); }
From source file:Main.java
private static SecretKeySpec createKey(String key) { byte[] data = null; if (key == null) { key = ""; }/*w w w .j ava 2 s.c om*/ StringBuffer sb = new StringBuffer(16); sb.append(key); while (sb.length() < 16) { sb.append("0"); } if (sb.length() > 16) { sb.setLength(16); } try { data = sb.toString().getBytes("UTF-8"); } catch (Exception e) { e.printStackTrace(); } return new SecretKeySpec(data, "AES"); }
From source file:Main.java
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); return cipher.doFinal(encrypted); }
From source file:Main.java
/** * This method generates a byte array corresponding to the * signed secret using value a signing operator * //ww w .ja va 2 s. c o m * @param value the value used to sign the key * @param secretKey the key to sign * @return a byte array corresponding to the raw HMAC-SHA1 hash */ public static byte[] hmacSha1(byte[] value, byte[] secretKey) throws RuntimeException { try { SecretKeySpec signingKey = new SecretKeySpec(secretKey, HMAC_ALGORITHM); Mac mac = Mac.getInstance(HMAC_ALGORITHM); mac.init(signingKey); byte[] rawHmac = mac.doFinal(value); return (rawHmac); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.jeet.s3.util.HashUtil.java
public static String generateFileHash(File file) { String hash = ""; try {// www . j av a2s . co m Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(Constants.HASH_SECRET.getBytes(), "HmacSHA256"); sha256_HMAC.init(secret_key); hash = Base64.encodeBase64String(sha256_HMAC.doFinal(IOUtils.toByteArray(new FileInputStream(file)))); } catch (Exception ex) { System.out.println("Error in generating hash."); } return hash; }
From source file:Main.java
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; }
From source file:Main.java
public static byte[] encrypt(String clearText) { SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = null;//from ww w.ja v a 2 s . c om byte[] cipherText = null; try { // init cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); cipherText = cipher.doFinal(clearText.getBytes()); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } return Base64.encode(cipherText, 10); }
From source file:Main.java
public static String decrypt(byte[] cipherText) { SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = null;/*from w w w . ja va2s . c o m*/ byte[] clearText = null; try { // init cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec); clearText = cipher.doFinal(Base64.decode(new String(cipherText), 10)); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) {// TODO Auto-generated catch block e.printStackTrace(); } return new String(clearText); }