Package javax.crypto.spec

Examples of javax.crypto.spec.SecretKeySpec


            }
        }
    }

    private static byte[] hmac(final String message, final String secretKey, final String hashAlgorithm) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
        final SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), hashAlgorithm);
        final Mac mac = Mac.getInstance(hashAlgorithm);
        mac.init(secretKeySpec);
        return mac.doFinal(message.getBytes("UTF-8"));
    }
View Full Code Here


     * @return VerificationResult a class that represent result of the signature verification
     * @throws InstagramException
     */
   
    public static VerificationResult verifySubscriptionPostRequestSignature(String clientSecret, byte[] rawJsonData, String xHubSignature) throws InstagramException{
      SecretKeySpec keySpec;
    keySpec = new SecretKeySpec(clientSecret.getBytes(Charset.forName("UTF-8")), HMAC_SHA1);
      Mac mac;
     
      try {
      mac = Mac.getInstance(HMAC_SHA1);
      mac.init(keySpec);
View Full Code Here

    public static final String ENFORCE_SIGNED_HEADER = "X-Insta-Forwarded-For";
   
    private static final String HMAC_SHA256 = "HmacSHA256";

    public static String signature(String clientSecret, String message) throws InstagramException {
        SecretKeySpec keySpec = new SecretKeySpec(clientSecret.getBytes(Charset.forName("UTF-8")), HMAC_SHA256);

        try {
            Mac mac = Mac.getInstance(HMAC_SHA256);
            mac.init(keySpec);
            byte[] result = mac.doFinal(message.getBytes(Charset.forName("UTF-8")));
View Full Code Here

    catch (NoSuchPaddingException e){
      System.out.println("No padding not available... haha!");
    }
   
    /* Create secret key from bytes. */
    this.key = new SecretKeySpec(key, "AES");
   
    /* Set IV. */
    this.iv = new byte[]{
      (byte)0x72, (byte)0xe0, (byte)0x67, (byte)0xfb,
      (byte)0xdd, (byte)0xcb, (byte)0xcf, (byte)0x77,
View Full Code Here

    private SecretKey generateAESKey(String privateKey, String salt) {
        try {
            byte[] raw = Hex.decodeHex(salt.toCharArray());
            KeySpec spec = new PBEKeySpec(privateKey.toCharArray(), raw, iterationCount, keySize);
            SecretKeyFactory factory = SecretKeyFactory.getInstance(PBKDF_2_WITH_HMAC_SHA_1);
            return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), AES_ECB_ALGORITHM);
        } catch (DecoderException e) {
            throw new IllegalStateException(e);
        } catch ( NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        } catch (InvalidKeySpecException e) {
View Full Code Here

     * @return The signed message (in hexadecimal)
     */
    public String sign(String message, byte[] key) {
        try {
            // Get an hmac_sha1 key from the raw key bytes
            SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA_1);

            // Get an hmac_sha1 Mac instance and initialize with the signing key
            Mac mac = Mac.getInstance(HMAC_SHA_1);
            mac.init(signingKey);

View Full Code Here

     * @return An hexadecimal encrypted string
     */
    public String encryptAES(String value, String privateKey) {
        try {
            byte[] raw = privateKey.getBytes(UTF_8);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, AES_ECB_ALGORITHM);
            Cipher cipher = Cipher.getInstance(AES_ECB_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            return Hex.encodeHexString(cipher.doFinal(value.getBytes(UTF_8)));
        } catch (Exception e) {
            throw new IllegalStateException(e);
View Full Code Here

     * @return The decrypted String
     */
    public String decryptAES(String value, String privateKey) {
        try {
            byte[] raw = privateKey.getBytes(UTF_8);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, AES_ECB_ALGORITHM);
            Cipher cipher = Cipher.getInstance(AES_ECB_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            return new String(cipher.doFinal(Hex.decodeHex(value.toCharArray())), UTF_8);
        } catch (Exception e) {
            throw new IllegalStateException(e);
View Full Code Here

        HmacUtils.getHmacMd5(null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testSecretKeySpecAllowsEmtyKeys() {
        new SecretKeySpec(new byte[] {}, "HmacMD5");
    }
View Full Code Here

        return hLen;
    }

    public void init(byte[] P) {
        try {
            mac.init(new SecretKeySpec(P, macAlgorithm));
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
    }
View Full Code Here

TOP

Related Classes of javax.crypto.spec.SecretKeySpec

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.