List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:Main.java
public static byte[] encrypt2(byte[] data, String key) throws Exception { try {/*from w w w .j a v a 2 s . c om*/ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); byte[] base64Key = Base64.decode(key, Base64.DEFAULT); SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES"); cipher.init(Cipher.ENCRYPT_MODE, keyspec); byte[] encrypted = cipher.doFinal(data); return encrypted; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static byte[] desEncrypt(byte[] data, String key) throws Exception { try {//from w w w . j av a2 s . c om Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); byte[] base64Key = Base64.decode(key, Base64.DEFAULT); SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES"); cipher.init(Cipher.DECRYPT_MODE, keyspec); byte[] original = cipher.doFinal(data); return original; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
private static void derivePKCS5S2Helper(Mac hMac, byte[] P, byte[] S, int c, byte[] iBuf, byte[] out, int outOff) throws GeneralSecurityException { byte[] state = new byte[hMac.getMacLength()]; SecretKeySpec param = new SecretKeySpec(P, "SHA1"); hMac.init(param);//from w w w.ja v a 2 s .co m if (S != null) { hMac.update(S, 0, S.length); } hMac.update(iBuf, 0, iBuf.length); hMac.doFinal(state, 0); System.arraycopy(state, 0, out, outOff, state.length); if (c == 0) { throw new IllegalArgumentException("iteration count must be at least 1."); } for (int count = 1; count < c; count++) { hMac.init(param); hMac.update(state, 0, state.length); hMac.doFinal(state, 0); for (int j = 0; j != state.length; j++) { out[outOff + j] ^= state[j]; } } }
From source file:Main.java
private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException { Mac mac = Mac.getInstance("HmacSHA256"); SecretKeySpec secret = new SecretKeySpec(keyString.getBytes("UTF-8"), "HmacSHA256"); mac.init(secret);//from w ww . ja v a 2 s. c o m byte[] byteData = mac.doFinal(baseString.getBytes("UTF-8")); BigInteger hash = new BigInteger(1, byteData); String hmac = hash.toString(16); if (hmac.length() % 2 != 0) { hmac = "0" + hmac; } return hmac; }
From source file:Main.java
public static String computeHmacSha1(String value, String keyString) throws InvalidKeyException, IllegalStateException, UnsupportedEncodingException, NoSuchAlgorithmException { SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(key);/* w ww.java2 s . c o m*/ byte[] bytes = mac.doFinal(value.getBytes("UTF-8")); return convToHex(bytes); }
From source file:Main.java
public static byte[] decodeFile(byte[] key, byte[] fileData, boolean isPlainText) throws Exception { byte[] decrypted; if (!isPlainText) { decrypted = new byte[fileData.length - key.length * 2]; System.arraycopy(fileData, key.length, decrypted, 0, decrypted.length); } else {/* www .j a v a2 s. c om*/ SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); decrypted = cipher.doFinal(fileData); } return decrypted; }
From source file:Main.java
public static String createHMACWithMD5(String source, String key) throws NoSuchAlgorithmException, InvalidKeyException { Mac hmac = Mac.getInstance("HmacMD5"); SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), hmac.getAlgorithm()); hmac.init(secretKey);//from w w w. j a v a 2s . c om byte[] signature = hmac.doFinal(source.getBytes()); return Base64.encodeToString(signature, Base64.DEFAULT); }
From source file:Main.java
public static byte[] TriDesDecryption(byte[] byteKey, byte[] dec) { try {//w w w. ja va 2 s. c o m byte[] en_key = new byte[24]; if (byteKey.length == 16) { System.arraycopy(byteKey, 0, en_key, 0, 16); System.arraycopy(byteKey, 0, en_key, 16, 8); } SecretKey key = new SecretKeySpec(en_key, "DESede"); Cipher dcipher = Cipher.getInstance("DESede/ECB/NoPadding"); dcipher.init(Cipher.DECRYPT_MODE, key); byte[] de_b = dcipher.doFinal(dec); return de_b; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static byte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) { try {/*from w w w .ja v a 2 s .c om*/ AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec); return cipher.doFinal(textBytes); } catch (Exception e) { return errorbyte; } }
From source file:Main.java
/** * function decrypt the string and return the result * @param stringToDecrypt the string against which the decryption to be performed * @return the decrypted String//from www.j av a2s . c o m */ public static final String decrypt(String stringToDecrypt) { try { Key aesKey = new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, aesKey); return new String(cipher.doFinal(stringToDecrypt.getBytes())); } catch (Exception e) { } return null; }