Package javax.crypto

Examples of javax.crypto.SecretKeyFactory


  }

  public String decryptPassword(final String password,
                                final String key) throws UnsupportedEncodingException, GeneralSecurityException
  {
    final SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    final KeySpec spec = new PBEKeySpec(key.toCharArray(), SALT, 1024, 128);
    final SecretKey tmp = factory.generateSecret(spec);
    final SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
    return decryptPassword(password, secret, "AES/CBC/PKCS5PADDING");
  }
View Full Code Here


     */
    public byte[] generateMwk(byte[] desBytes) {
        if (debug) {
            Debug.log("DES Key : " + StringUtil.toHexString(desBytes) + " / " + desBytes.length, module);
        }
        SecretKeyFactory skf1 = null;
        SecretKey mwk = null;
        try {
            skf1 = SecretKeyFactory.getInstance("DESede");
        } catch (NoSuchAlgorithmException e) {
            Debug.logError(e, module);
        }
        DESedeKeySpec desedeSpec2 = null;
        try {
            desedeSpec2 = new DESedeKeySpec(desBytes);
        } catch (InvalidKeyException e) {
            Debug.logError(e, module);
        }
        if (skf1 != null && desedeSpec2 != null) {
            try {
                mwk = skf1.generateSecret(desedeSpec2);
            } catch (InvalidKeySpecException e) {
                Debug.logError(e, module);
            }
        }
        if (mwk != null) {
View Full Code Here

        return kek;
    }

    protected SecretKey getDesEdeKey(byte[] rawKey) {
        SecretKeyFactory skf = null;
        try {
            skf = SecretKeyFactory.getInstance("DESede");
        } catch (NoSuchAlgorithmException e) {
            // should never happen since DESede is a standard algorithm
            Debug.logError(e, module);
            return null;
        }

        // load the raw key
        if (rawKey.length > 0) {
            DESedeKeySpec desedeSpec1 = null;
            try {
                desedeSpec1 = new DESedeKeySpec(rawKey);
            } catch (InvalidKeyException e) {
                Debug.logError(e, "Not a valid DESede key", module);
                return null;
            }

            // create the SecretKey Object
            SecretKey key = null;
            try {
                key = skf.generateSecret(desedeSpec1);
            } catch (InvalidKeySpecException e) {
                Debug.logError(e, module);
            }
            return key;
        } else {
View Full Code Here

      throws GeneralSecurityException
   {
      if (hashAlgorithm != null)
      {
         PBEKeySpec passwordKeySpec = new PBEKeySpec(password, salt, iterations, 256);
         SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(hashAlgorithm);
         SecretKey passwordKey = secretKeyFactory.generateSecret(passwordKeySpec);
         passwordKeySpec.clearPassword();
         return BinTools.bin2hex(passwordKey.getEncoded());
      }
      else
      {
View Full Code Here

      }

      final String algorithm = "PBEWithSHA256And192BitAES-CBC-BC";
        final Cipher c = Cipher.getInstance(algorithm, "BC");
        final PBEKeySpec          keySpec = new PBEKeySpec(p, getSalt(), iCount);
        final SecretKeyFactory    fact = SecretKeyFactory.getInstance(algorithm, "BC");
       
        c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec));
       
        final byte[] dec = c.doFinal(Hex.decode(in.getBytes("UTF-8")));
        return new String(dec);
    }
View Full Code Here

        for (int i = 0; i < key.length; i++) {
            key[i] = flipByte(key[i]);
        }

        KeySpec desKeySpec = new DESKeySpec(key);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        byte[] response = cipher.doFinal(challenge);
        return response;
View Full Code Here

    }

    private static SecretKey getSecretKey(String hmacSha1) throws Exception {

        DESedeKeySpec keyspec = new DESedeKeySpec(hmacSha1.getBytes("UTF-8"));
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = keyfactory.generateSecret(keyspec);

        return key;
    }
View Full Code Here

            // Create the PBEKeySpec with the given password
            PBEKeySpec keySpec = new PBEKeySpec(passwordChar);

            // Get a SecretFactory for PBEWithSHAAndTwofish
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(this.algorithm);
            skey = keyFactory.generateSecret(keySpec);

            // Now create a parameter spec for our salt and iterations
            PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray, ITERATION);
            cipher.init(Cipher.DECRYPT_MODE, skey, paramSpec);
View Full Code Here

            // Create the PBEKeySpec with the given password
            PBEKeySpec keySpec = new PBEKeySpec(passwordChar);

            // Get a SecretFactory for PBEWithSHAAndTwofish
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(this.algorithm);
            skey = keyFactory.generateSecret(keySpec);

            // Now create a parameter spec for our salt and iterations
            PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray, ITERATION);
            cipher.init(Cipher.DECRYPT_MODE, skey, paramSpec);
View Full Code Here

        return bytes;
    }
   
    private static String generatePBKDF2(String pwd, String salt, String algorithm, int iterations, int keyLength) throws NoSuchAlgorithmException {
        // for example PBKDF2WithHmacSHA1
        SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
        byte[] saltBytes = convertHexToBytes(salt);
        KeySpec keyspec = new PBEKeySpec(pwd.toCharArray(), saltBytes, iterations, keyLength);
        try {
            Key key = factory.generateSecret(keyspec);
            byte[] bytes = key.getEncoded();
            return convertBytesToHex(bytes);
        } catch (InvalidKeySpecException e) {
            throw new NoSuchAlgorithmException(algorithm, e);
       
View Full Code Here

TOP

Related Classes of javax.crypto.SecretKeyFactory

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.