Search in sources :

Example 91 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project fxosstub by Jaxo.

the class Jwt method hashKey.

/*----------------------------------------------------------------base64Url-+
   */
/**
   */
/*
   +-------------------------------------------------------------------------*/
private static String hashKey(String in) throws Exception {
    Mac mac = Mac.getInstance(ALGO);
    mac.init(new SecretKeySpec(paySecret.getBytes(ENC), ALGO));
    return base64Url(mac.doFinal(in.getBytes(ENC)));
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(javax.crypto.Mac)

Example 92 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project graylog2-server by Graylog2.

the class AESTools method decrypt.

@Nullable
public static String decrypt(String cipherText, String encryptionKey, String salt) {
    try {
        @SuppressFBWarnings("CIPHER_INTEGRITY") Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(salt.getBytes("UTF-8")));
        return new String(cipher.doFinal(Hex.decode(cipherText)), "UTF-8");
    } catch (Exception e) {
        LOG.error("Could not decrypt value.", e);
    }
    return null;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) Nullable(javax.annotation.Nullable)

Example 93 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project bigbluebutton by bigbluebutton.

the class TurnServer method calculateRFC2104HMAC.

/**
   * Computes RFC 2104-compliant HMAC signature.
   * * @param data
   * The data to be signed.
   * @param key
   * The signing key.
   * @return
   * The Base64-encoded RFC 2104-compliant HMAC signature.
   * @throws
   * java.security.SignatureException when signature generation fails
   */
private String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException {
    String result;
    try {
        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
        // get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        // compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());
        // base64-encode the hmac
        result = new String(Base64.encodeBase64(rawHmac));
    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
    return result;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SignatureException(java.security.SignatureException) Mac(javax.crypto.Mac) SignatureException(java.security.SignatureException)

Example 94 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project UltimateAndroid by cymcsg.

the class TripleDES method decrypt.

public static String decrypt(byte[] message) throws Exception {
    byte[] values = Base64decodingByte(message, 0);
    final MessageDigest md = MessageDigest.getInstance("SHA-1");
    final byte[] digestOfPassword = md.digest(token.getBytes("utf-8"));
    final byte[] keyBytes = copyOf(digestOfPassword, 24);
    for (int j = 0, k = 16; j < 8; ) {
        keyBytes[k++] = keyBytes[j++];
    }
    final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
    String s1 = "12345678";
    byte[] bytes = s1.getBytes();
    final IvParameterSpec iv = new IvParameterSpec(bytes);
    final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key, iv);
    final byte[] plainText = decipher.doFinal(values);
    return new String(plainText, "UTF-8");
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 95 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project zaproxy by zaproxy.

the class ZapNTLMEngineImpl method RC4.

/** Calculates RC4 */
static byte[] RC4(final byte[] value, final byte[] key) throws AuthenticationException {
    try {
        final Cipher rc4 = Cipher.getInstance("RC4");
        rc4.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "RC4"));
        return rc4.doFinal(value);
    } catch (Exception e) {
        throw new AuthenticationException(e.getMessage(), e);
    }
}
Also used : AuthenticationException(org.apache.commons.httpclient.auth.AuthenticationException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) AuthenticationException(org.apache.commons.httpclient.auth.AuthenticationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

SecretKeySpec (javax.crypto.spec.SecretKeySpec)498 Cipher (javax.crypto.Cipher)194 SecretKey (javax.crypto.SecretKey)142 Mac (javax.crypto.Mac)110 IvParameterSpec (javax.crypto.spec.IvParameterSpec)106 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)94 InvalidKeyException (java.security.InvalidKeyException)67 IOException (java.io.IOException)44 Key (java.security.Key)36 SecureRandom (java.security.SecureRandom)30 Test (org.junit.Test)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)29 GeneralSecurityException (java.security.GeneralSecurityException)27 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)27 MessageDigest (java.security.MessageDigest)25 BadPaddingException (javax.crypto.BadPaddingException)25 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)25 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)18 PrivateKey (java.security.PrivateKey)18 PublicKey (java.security.PublicKey)16