List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, String algorithm)
From source file:com.threatconnect.sdk.conn.ConnectionUtil.java
public static String getHmacSha256Signature(String signature, String apiSecretKey) { try {/*from w ww .j av a 2 s . c om*/ String calculatedSignature; SecretKeySpec spec = new SecretKeySpec(apiSecretKey.getBytes(), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(spec); byte[] rawSignature = mac.doFinal(signature.getBytes()); calculatedSignature = Base64.encodeBase64String(rawSignature); return calculatedSignature; } catch (NoSuchAlgorithmException | InvalidKeyException | IllegalStateException ex) { logger.error("Error creating HMAC SHA256 signature", ex); return null; } }
From source file:com.twosigma.beakerx.security.HashedMessageAuthenticationCode.java
public HashedMessageAuthenticationCode(String key) { checkNotNull(key);/*from ww w . j a va 2 s .com*/ logger.debug("Using signing hmac: {}", key); spec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), TYPE); }
From source file:com.ibm.sbt.security.encryption.HMACEncryptionUtility.java
public static String generateHMACSignature(String apiUrl, String method, String consumerSecret, String tokenSecret, Map<String, String> paramsSortedMap) throws OAuthException { try {/*from w ww . j a v a2s .c om*/ String parameterString = generateParameterString(paramsSortedMap); String signature_base_string = generateSignatureBaseString(method, apiUrl, parameterString); String signingKey = null; if (StringUtil.isEmpty(tokenSecret)) { // No token secret is available when call is made from getRequestToken, tokensecret is fetched // later in OADance signingKey = consumerSecret + "&"; } else { signingKey = consumerSecret + "&" + tokenSecret; } byte[] keyBytes = null; try { keyBytes = signingKey.getBytes(Configuration.ENCODING); } catch (UnsupportedEncodingException e) { throw new OAuthException(e, "HMACEncryptionUtility : generateHMACSignature caused UnsupportedEncodingException exception"); } SecretKey secretKey = new SecretKeySpec(keyBytes, "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(secretKey); byte[] text = null; try { text = signature_base_string.getBytes(Configuration.ENCODING); } catch (UnsupportedEncodingException e) { throw new OAuthException(e, "HMACEncryptionUtility : generateHMACSignature caused UnsupportedEncodingException exception"); } String signature = new String(Base64.encodeBase64(mac.doFinal(text))).trim(); return signature; } catch (NoSuchAlgorithmException e) { throw new OAuthException(e, "HMACEncryptionUtility : generateHMACSignature caused NoSuchAlgorithmException exception"); } catch (InvalidKeyException e) { throw new OAuthException(e, "HMACEncryptionUtility : generateHMACSignature caused InvalidKeyException exception"); } }
From source file:Main.java
private static Key toKey(byte[] key) { return new SecretKeySpec(key, KEY_ALGORITHM); }
From source file:com.antonjohansson.elasticsearchshell.client.PasswordEncrypter.java
/** * Encrypts the given password, using the username as salt. * * @param username The username, used for salt. * @param password The password to encrypt. * @return Returns the encrypted password. *///from w w w . j a v a 2s .co m String encrypt(String username, String password) { try { String key = rightPad(username, KEY_SIZE).substring(0, KEY_SIZE); Key spec = new SecretKeySpec(key.getBytes(), algorithm); Cipher cipher = Cipher.getInstance(algorithm); cipher.init(ENCRYPT_MODE, spec); byte[] encrypted = cipher.doFinal(password.getBytes()); return Base64.getEncoder().encodeToString(encrypted); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.amazonaws.cognito.devauthsample.Utilities.java
public static String sign(String content, String key) { try {// www . j a v a 2 s.c o m byte[] data = content.getBytes(ENCODING_FORMAT); Mac mac = Mac.getInstance(SIGNATURE_METHOD); mac.init(new SecretKeySpec(key.getBytes(ENCODING_FORMAT), SIGNATURE_METHOD)); char[] signature = Hex.encodeHex(mac.doFinal(data)); return new String(signature); } catch (Exception e) { log.log(Level.SEVERE, "Exception during sign", e); } return null; }
From source file:adminpassword.Decryption.java
@SuppressWarnings("static-access") public String decrypt(String encryptedText, String idKey) throws Exception { String password = idKey;/* w w w .j a va 2 s .c om*/ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //strip off the salt and iv ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText)); byte[] saltBytes = new byte[20]; buffer.get(saltBytes, 0, saltBytes.length); byte[] ivBytes1 = new byte[cipher.getBlockSize()]; buffer.get(ivBytes1, 0, ivBytes1.length); byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length]; buffer.get(encryptedTextBytes); // Deriving the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1)); byte[] decryptedTextBytes = null; try { decryptedTextBytes = cipher.doFinal(encryptedTextBytes); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return new String(decryptedTextBytes); }
From source file:com.microsoft.azure.iothub.auth.SignatureHelper.java
/** * Encrypts the signature using HMAC-SHA256. * * @param sig the unencrypted signature. * @param deviceKey the Base64-decoded device key. * * @return the HMAC-SHA256 encrypted signature. *//*w w w.java 2 s . c o m*/ public static byte[] encryptSignatureHmacSha256(byte[] sig, byte[] deviceKey) { String hmacSha256 = "HmacSHA256"; // Codes_SRS_SIGNATUREHELPER_11_005: [The function shall use the device key as the secret for the algorithm.] SecretKeySpec secretKey = new SecretKeySpec(deviceKey, hmacSha256); byte[] encryptedSig = null; try { // Codes_SRS_SIGNATUREHELPER_11_004: [The function shall encrypt the signature using the HMAC-SHA256 algorithm.] Mac hMacSha256 = Mac.getInstance(hmacSha256); hMacSha256.init(secretKey); encryptedSig = hMacSha256.doFinal(sig); } catch (NoSuchAlgorithmException e) { // should never happen, since the algorithm is hard-coded. } catch (InvalidKeyException e) { // should never happen, since the input key type is hard-coded. } return encryptedSig; }
From source file:com.myapp.common.AES4MEncrypt.java
/** * /*from w w w . j a v a2s .c o m*/ * * @param sSrc ? * @param sKey KEY * @return * @throws Exception * @author cdduqiang * @date 201443 */ public static String decrypt(String sSrc, String sKey) throws Exception { if (sKey == null) { log.error("Decrypt Key ??"); throw new Exception("Decrypt Key ??"); } byte[] raw = hex2byte(sKey); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] encrypted1 = hex2byte(sSrc); byte[] original = cipher.doFinal(encrypted1); return new String(original, ENCODING);// anslBytes2String(original); }
From source file:com.haulmont.timesheets.EncryptDecrypt.java
public EncryptDecrypt(String key) { try {/*from w w w . j a v a2 s .c o m*/ String data = new StringBuilder(SALT + key).reverse().toString(); SecretKeySpec secretKey = new SecretKeySpec(DigestUtils.md5(data), "AES"); AlgorithmParameterSpec paramSpec = new IvParameterSpec(INIT_VECTOR); eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); eCipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); dCipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); } catch (Exception e) { throw new RuntimeException("Exception while init cipher:", e); } }