javax.crypto.spec.PBEParameterSpec Java Examples

The following examples show how to use javax.crypto.spec.PBEParameterSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PasswordUtil.java    From jeewx with Apache License 2.0 6 votes vote down vote up
/**
 * 解密密文字符串
 * 
 * @param ciphertext
 *            待解密的密文字符串
 * @param password
 *            生成密钥时所使用的密码(如需解密,该参数需要与加密时使用的一致)
 * @param salt
 *            盐值(如需解密,该参数需要与加密时使用的一致)
 * @return 解密后的明文字符串
 * @throws Exception
 */
public static String decrypt(String ciphertext, String password, byte[] salt) {

	Key key = getPBEKey(password);
	byte[] passDec = null;
	PBEParameterSpec parameterSpec = new PBEParameterSpec(getStaticSalt(), ITERATIONCOUNT);
	try {
		Cipher cipher = Cipher.getInstance(ALGORITHM);

		cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);

		passDec = cipher.doFinal(hexStringToBytes(ciphertext));
	}

	catch (Exception e) {
		// TODO: handle exception
	}
	return new String(passDec);
}
 
Example #2
Source File: PBEUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static String decode64(String secret, String cipherAlgorithm,
   SecretKey cipherKey, PBEParameterSpec cipherSpec)
   throws Exception
{
   byte [] encoding;
   try {
      encoding = Base64Utils.fromb64(secret);
   }
   catch (IllegalArgumentException e) {
      // fallback when original string is was created with faulty version of Base64 
      encoding = Base64Utils.fromb64("0" + secret);
      PicketBoxLogger.LOGGER.wrongBase64StringUsed("0" + secret);
   }
   byte[] decode = decode(encoding, cipherAlgorithm, cipherKey, cipherSpec);
   return new String(decode, "UTF-8");
}
 
Example #3
Source File: PKCS12KeyStore.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private AlgorithmParameters getPBEAlgorithmParameters(String algorithm)
    throws IOException
{
    AlgorithmParameters algParams = null;

    // create PBE parameters from salt and iteration count
    PBEParameterSpec paramSpec =
            new PBEParameterSpec(getSalt(), PBE_ITERATION_COUNT);
    try {
       algParams = AlgorithmParameters.getInstance(algorithm);
       algParams.init(paramSpec);
    } catch (Exception e) {
       throw new IOException("getPBEAlgorithmParameters failed: " +
                             e.getMessage(), e);
    }
    return algParams;
}
 
Example #4
Source File: EncryptedPropertiesApache.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void initialise(String password) {
    PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
    SecretKeyFactory kf;
    try {
        kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey k =
                kf.generateSecret(new javax.crypto.spec.PBEKeySpec(password.toCharArray()));
        encrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        decrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        encrypter.init(Cipher.ENCRYPT_MODE, k, ps);
        decrypter.init(Cipher.DECRYPT_MODE, k, ps);
    } catch (NoSuchAlgorithmException
            | NoSuchPaddingException
            | InvalidKeySpecException
            | InvalidKeyException
            | InvalidAlgorithmParameterException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
}
 
Example #5
Source File: PasswordUtil.java    From jeecg-boot-with-activiti with MIT License 6 votes vote down vote up
/**
 * 解密密文字符串
 * 
 * @param ciphertext
 *            待解密的密文字符串
 * @param password
 *            生成密钥时所使用的密码(如需解密,该参数需要与加密时使用的一致)
 * @param salt
 *            盐值(如需解密,该参数需要与加密时使用的一致)
 * @return 解密后的明文字符串
 * @throws Exception
 */
public static String decrypt(String ciphertext, String password, String salt) {

	Key key = getPBEKey(password);
	byte[] passDec = null;
	PBEParameterSpec parameterSpec = new PBEParameterSpec(salt.getBytes(), ITERATIONCOUNT);
	try {
		Cipher cipher = Cipher.getInstance(ALGORITHM);

		cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);

		passDec = cipher.doFinal(hexStringToBytes(ciphertext));
	}

	catch (Exception e) {
		// TODO: handle exception
	}
	return new String(passDec);
}
 
Example #6
Source File: PKCS12KeyStore.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private AlgorithmParameters getPBEAlgorithmParameters(String algorithm)
    throws IOException
{
    AlgorithmParameters algParams = null;

    // create PBE parameters from salt and iteration count
    PBEParameterSpec paramSpec =
            new PBEParameterSpec(getSalt(), PBE_ITERATION_COUNT);
    try {
       algParams = AlgorithmParameters.getInstance(algorithm);
       algParams.init(paramSpec);
    } catch (Exception e) {
       throw new IOException("getPBEAlgorithmParameters failed: " +
                             e.getMessage(), e);
    }
    return algParams;
}
 
Example #7
Source File: PKCS12KeyStore.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private AlgorithmParameters getAlgorithmParameters(String algorithm)
    throws IOException
{
    AlgorithmParameters algParams = null;

    // create PBE parameters from salt and iteration count
    PBEParameterSpec paramSpec =
            new PBEParameterSpec(getSalt(), iterationCount);
    try {
       algParams = AlgorithmParameters.getInstance(algorithm);
       algParams.init(paramSpec);
    } catch (Exception e) {
       throw new IOException("getAlgorithmParameters failed: " +
                             e.getMessage(), e);
    }
    return algParams;
}
 
Example #8
Source File: EntryProtectionTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #9
Source File: PasswordUtil.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
 * 加密明文字符串
 * 
 * @param plaintext
 *            待加密的明文字符串
 * @param password
 *            生成密钥时所使用的密码
 * @param salt
 *            盐值
 * @return 加密后的密文字符串
 * @throws Exception
 */
public static String encrypt(String plaintext, String password, String salt) {

	Key key = getPBEKey(password);
	byte[] encipheredData = null;
	PBEParameterSpec parameterSpec = new PBEParameterSpec(salt.getBytes(), ITERATIONCOUNT);
	try {
		Cipher cipher = Cipher.getInstance(ALGORITHM);

		cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
		//update-begin-author:sccott date:20180815 for:中文作为用户名时,加密的密码windows和linux会得到不同的结果 gitee/issues/IZUD7
		encipheredData = cipher.doFinal(plaintext.getBytes("utf-8"));
		//update-end-author:sccott date:20180815 for:中文作为用户名时,加密的密码windows和linux会得到不同的结果 gitee/issues/IZUD7
	} catch (Exception e) {
	}
	return bytesToHexString(encipheredData);
}
 
Example #10
Source File: PicketBoxSecurityVault.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private String decode(String maskedString, String salt, int iterationCount) throws Exception
{
   String pbeAlgo = "PBEwithMD5andDES";
   if (maskedString.startsWith(PASS_MASK_PREFIX))
   {
      // Create the PBE secret key 
      SecretKeyFactory factory = SecretKeyFactory.getInstance(pbeAlgo);

      char[] password = "somearbitrarycrazystringthatdoesnotmatter".toCharArray();
      PBEParameterSpec cipherSpec = new PBEParameterSpec(salt.getBytes(), iterationCount);
      PBEKeySpec keySpec = new PBEKeySpec(password);
      SecretKey cipherKey = factory.generateSecret(keySpec);

      maskedString = maskedString.substring(PASS_MASK_PREFIX.length());
      String decodedValue = PBEUtils.decode64(maskedString, pbeAlgo, cipherKey, cipherSpec);
      maskedString = decodedValue;
   }
   return maskedString;
}
 
Example #11
Source File: PBEUtils.java    From tomcat-vault with Apache License 2.0 6 votes vote down vote up
public static String decode64(String secret, String cipherAlgorithm,
   SecretKey cipherKey, PBEParameterSpec cipherSpec)
   throws Exception
{
   byte [] encoding;
   try {
      encoding = Base64Utils.fromb64(secret);
   }
   catch (IllegalArgumentException e) {
      // fallback when original string is was created with faulty version of Base64 
      encoding = Base64Utils.fromb64("0" + secret);
      log.warn(sm.getString("PBEUtils.wrongBase64StringUsed", "0" + secret));
   }
   byte[] decode = decode(encoding, cipherAlgorithm, cipherKey, cipherSpec);
   return new String(decode, "UTF-8");
}
 
Example #12
Source File: EntryProtectionTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #13
Source File: ToolPBE.java    From protools with Apache License 2.0 6 votes vote down vote up
/**
 * 解密
 *
 * @param data
 *         数据
 * @param password
 *         密码
 * @param salt
 *         盐
 *
 * @return byte[] 解密数据
 *
 * @throws Exception
 */
public static byte[] decrypt(byte[] data, String password, byte[] salt) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    // 转换密钥
    Key key = toKey(password);

    // 实例化PBE参数材料
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100);

    // 实例化
    Cipher cipher = Cipher.getInstance(ALGORITHM);

    // 初始化
    cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

    // 执行操作
    return cipher.doFinal(data);
}
 
Example #14
Source File: PBEUtils.java    From tomcat-vault with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception
{
   if( args.length != 4 )
   {
      System.err.println(msm.getString("pbeUtilsMessage"));
   }

   byte[] salt = args[0].substring(0, 8).getBytes();
   int count = Integer.parseInt(args[1]);
   char[] password = args[2].toCharArray();
   byte[] passwordToEncode = args[3].getBytes("UTF-8");
   PBEParameterSpec cipherSpec = new PBEParameterSpec(salt, count);
   PBEKeySpec keySpec = new PBEKeySpec(password);
   SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEwithMD5andDES");
   SecretKey cipherKey = factory.generateSecret(keySpec);
   String encodedPassword = encode64(passwordToEncode, "PBEwithMD5andDES",
      cipherKey, cipherSpec);
   System.err.println("Encoded password: "+encodedPassword);
}
 
Example #15
Source File: PasswordUtil.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 加密明文字符串
 * 
 * @param plaintext
 *            待加密的明文字符串
 * @param password
 *            生成密钥时所使用的密码
 * @param salt
 *            盐值
 * @return 加密后的密文字符串
 * @throws Exception
 */
public static String encrypt(String plaintext, String password, String salt) {

	Key key = getPBEKey(password);
	byte[] encipheredData = null;
	PBEParameterSpec parameterSpec = new PBEParameterSpec(salt.getBytes(), ITERATIONCOUNT);
	try {
		Cipher cipher = Cipher.getInstance(ALGORITHM);

		cipher.init(Cipher.ENCRYPT_MODE, key, parameterSpec);
		//update-begin-author:sccott date:20180815 for:中文作为用户名时,加密的密码windows和linux会得到不同的结果 gitee/issues/IZUD7
		encipheredData = cipher.doFinal(plaintext.getBytes("utf-8"));
		//update-end-author:sccott date:20180815 for:中文作为用户名时,加密的密码windows和linux会得到不同的结果 gitee/issues/IZUD7
	} catch (Exception e) {
	}
	return bytesToHexString(encipheredData);
}
 
Example #16
Source File: PasswordUtil.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 解密密文字符串
 * 
 * @param ciphertext
 *            待解密的密文字符串
 * @param password
 *            生成密钥时所使用的密码(如需解密,该参数需要与加密时使用的一致)
 * @param salt
 *            盐值(如需解密,该参数需要与加密时使用的一致)
 * @return 解密后的明文字符串
 * @throws Exception
 */
public static String decrypt(String ciphertext, String password, String salt) {

	Key key = getPBEKey(password);
	byte[] passDec = null;
	PBEParameterSpec parameterSpec = new PBEParameterSpec(salt.getBytes(), ITERATIONCOUNT);
	try {
		Cipher cipher = Cipher.getInstance(ALGORITHM);

		cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);

		passDec = cipher.doFinal(hexStringToBytes(ciphertext));
	}

	catch (Exception e) {
		// TODO: handle exception
	}
	return new String(passDec);
}
 
Example #17
Source File: PKCS12KeyStore.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private AlgorithmParameters getPBEAlgorithmParameters(String algorithm)
    throws IOException
{
    AlgorithmParameters algParams = null;

    // create PBE parameters from salt and iteration count
    PBEParameterSpec paramSpec =
            new PBEParameterSpec(getSalt(), PBE_ITERATION_COUNT);
    try {
       algParams = AlgorithmParameters.getInstance(algorithm);
       algParams.init(paramSpec);
    } catch (Exception e) {
       throw new IOException("getPBEAlgorithmParameters failed: " +
                             e.getMessage(), e);
    }
    return algParams;
}
 
Example #18
Source File: AbstractInAppService.java    From atomic-plugins-inapps with Mozilla Public License 2.0 6 votes vote down vote up
protected void loadCipheredStock(){
    mStock = new HashMap<String, Integer>();

    try {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        String value = preferences.getString("inappservice_stock", "");
        if (value.length() == 0) {
            return;
        }
        final byte[] bytes = Base64.decode(value,Base64.DEFAULT);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(getUniquePseudoID().toCharArray()));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID).getBytes("utf-8"), 20));
        String json = new String(pbeCipher.doFinal(bytes),"utf-8");
        JSONObject object = new JSONObject(json);
        Iterator<?> keys = object.keys();
        while( keys.hasNext() ){
            String pid = (String)keys.next();
            this.mStock.put(pid, object.optInt(pid));
        }

    } catch( Exception e) {
        e.printStackTrace();
    }
}
 
Example #19
Source File: PKCS12KeyStore.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private AlgorithmParameters getAlgorithmParameters(String algorithm)
    throws IOException
{
    AlgorithmParameters algParams = null;

    // create PBE parameters from salt and iteration count
    PBEParameterSpec paramSpec =
            new PBEParameterSpec(getSalt(), iterationCount);
    try {
       algParams = AlgorithmParameters.getInstance(algorithm);
       algParams.init(paramSpec);
    } catch (Exception e) {
       throw new IOException("getAlgorithmParameters failed: " +
                             e.getMessage(), e);
    }
    return algParams;
}
 
Example #20
Source File: PKCS12KeyStore.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private AlgorithmParameters getPBEAlgorithmParameters(String algorithm)
    throws IOException
{
    AlgorithmParameters algParams = null;

    // create PBE parameters from salt and iteration count
    PBEParameterSpec paramSpec =
            new PBEParameterSpec(getSalt(), PBE_ITERATION_COUNT);
    try {
       algParams = AlgorithmParameters.getInstance(algorithm);
       algParams.init(paramSpec);
    } catch (Exception e) {
       throw new IOException("getPBEAlgorithmParameters failed: " +
                             e.getMessage(), e);
    }
    return algParams;
}
 
Example #21
Source File: PBEUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception
{
   if( args.length != 4 )
   {
      System.err.println(PicketBoxMessages.MESSAGES.pbeUtilsMessage());
   }

   byte[] salt = args[0].substring(0, 8).getBytes();
   int count = Integer.parseInt(args[1]);
   char[] password = args[2].toCharArray();
   byte[] passwordToEncode = args[3].getBytes("UTF-8");
   PBEParameterSpec cipherSpec = new PBEParameterSpec(salt, count);
   PBEKeySpec keySpec = new PBEKeySpec(password);
   SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEwithMD5andDES");
   SecretKey cipherKey = factory.generateSecret(keySpec);
   String encodedPassword = encode64(passwordToEncode, "PBEwithMD5andDES",
      cipherKey, cipherSpec);
   System.err.println("Encoded password: "+encodedPassword);
}
 
Example #22
Source File: EntryProtectionTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void setUp() {
    out.println("Using KEYSTORE_PATH:"+KEYSTORE_PATH);
    Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
    Random rand = RandomFactory.getRandom();
    rand.nextBytes(SALT);
    out.print("Salt: ");
    for (byte b : SALT) {
        out.format("%02X ", b);
    }
    out.println("");
    PASSWORD_PROTECTION
            .add(new KeyStore.PasswordProtection(PASSWORD,
                            "PBEWithMD5AndDES", new PBEParameterSpec(SALT,
                                    ITERATION_COUNT)));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndDESede", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC2_128", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_40", null));
    PASSWORD_PROTECTION.add(new KeyStore.PasswordProtection(PASSWORD,
            "PBEWithSHA1AndRC4_128", null));
}
 
Example #23
Source File: PBECipherWrapper.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public PBECipherWrapper(
        Provider p, String algo, String passwd, PrintStream out)
        throws Exception {
    super(algo,
            SecretKeyFactory.getInstance(
                    new StringTokenizer(algo, "/").nextToken(), p).generateSecret(
                    new PBEKeySpec(passwd.toCharArray())),
            Cipher.getInstance(algo, p), out);

    int SALT_SIZE = 8;
    aps = new PBEParameterSpec(generateSalt(SALT_SIZE), ITERATION_COUNT);
}
 
Example #24
Source File: PBES2Parameters.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException
{
   if (!(paramSpec instanceof PBEParameterSpec)) {
       throw new InvalidParameterSpecException
           ("Inappropriate parameter specification");
   }
   this.salt = ((PBEParameterSpec)paramSpec).getSalt().clone();
   this.iCount = ((PBEParameterSpec)paramSpec).getIterationCount();
   this.cipherParam = ((PBEParameterSpec)paramSpec).getParameterSpec();
}
 
Example #25
Source File: SecureString.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private void encrypt(byte[] cleartext) throws GeneralSecurityException {
   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
   SecretKey key = keyFactory.generateSecret(new PBEKeySpec(getMetaPassword()));
   Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
   pbeCipher.init(1, key, new PBEParameterSpec(this.salt, 20));
   this.cipherBytes = pbeCipher.doFinal(cleartext);
}
 
Example #26
Source File: PBES2Parameters.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException
{
   if (!(paramSpec instanceof PBEParameterSpec)) {
       throw new InvalidParameterSpecException
           ("Inappropriate parameter specification");
   }
   this.salt = ((PBEParameterSpec)paramSpec).getSalt().clone();
   this.iCount = ((PBEParameterSpec)paramSpec).getIterationCount();
   this.cipherParam = ((PBEParameterSpec)paramSpec).getParameterSpec();
}
 
Example #27
Source File: SecKFTranslateTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
SecretKey intSecurityKey(AlgorithmParameterSpec[] spec)
        throws InvalidKeySpecException {
    byte[] salt = new byte[8];
    int iterCnt = 6;
    new Random().nextBytes(salt);
    spec[0] = new PBEParameterSpec(salt, iterCnt);
    PBEKeySpec pbeKS = new PBEKeySpec(
            new String("So far so good").toCharArray());
    SecretKey key1 = new MyOwnSecKey(pbeKS);
    return key1;
}
 
Example #28
Source File: PBEIdentityLoginModule.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private String encode(String secret)
   throws Exception
{
   // Create the PBE secret key
   cipherSpec = new PBEParameterSpec(salt, iterationCount);
   PBEKeySpec keySpec = new PBEKeySpec(pbepass);
   SecretKeyFactory factory = SecretKeyFactory.getInstance(pbealgo);
   SecretKey cipherKey = factory.generateSecret(keySpec);

   // Decode the secret
   Cipher cipher = Cipher.getInstance(pbealgo);
   cipher.init(Cipher.ENCRYPT_MODE, cipherKey, cipherSpec);
   byte[] encoding = cipher.doFinal(secret.getBytes());
   return Base64Utils.tob64(encoding);
}
 
Example #29
Source File: PKCS12KeyStore.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private byte[] calculateMac(char[] passwd, byte[] data)
    throws IOException
{
    byte[] mData = null;
    String algName = macAlgorithm.substring(7);

    try {
        // Generate a random salt.
        byte[] salt = getSalt();

        // generate MAC (MAC key is generated within JCE)
        Mac m = Mac.getInstance(macAlgorithm);
        PBEParameterSpec params =
                    new PBEParameterSpec(salt, macIterationCount);
        SecretKey key = getPBEKey(passwd);
        m.init(key, params);
        m.update(data);
        byte[] macResult = m.doFinal();

        // encode as MacData
        MacData macData = new MacData(algName, macResult, salt,
                macIterationCount);
        DerOutputStream bytes = new DerOutputStream();
        bytes.write(macData.getEncoded());
        mData = bytes.toByteArray();
    } catch (Exception e) {
        throw new IOException("calculateMac failed: " + e, e);
    }
    return mData;
}
 
Example #30
Source File: PBECipherWrapper.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public PBECipherWrapper(
        Provider p, String algo, String passwd, PrintStream out)
        throws Exception {
    super(algo,
            SecretKeyFactory.getInstance(
                    new StringTokenizer(algo, "/").nextToken(), p).generateSecret(
                    new PBEKeySpec(passwd.toCharArray())),
            Cipher.getInstance(algo, p), out);

    int SALT_SIZE = 8;
    aps = new PBEParameterSpec(generateSalt(SALT_SIZE), ITERATION_COUNT);
}