当前位置: 首页>>代码示例>>Java>>正文


Java PBEKey类代码示例

本文整理汇总了Java中javax.crypto.interfaces.PBEKey的典型用法代码示例。如果您正苦于以下问题:Java PBEKey类的具体用法?Java PBEKey怎么用?Java PBEKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


PBEKey类属于javax.crypto.interfaces包,在下文中一共展示了PBEKey类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: runTest

import javax.crypto.interfaces.PBEKey; //导入依赖的package包/类
private static void runTest(String alg, byte[] plaintext,
                            char[] password, Provider p)
    throws Exception {
    Cipher cipher = Cipher.getInstance(alg, p);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
    AlgorithmParameters pbeParams = null;
    SecretKey key = keyFac.generateSecret(pbeKeySpec);
    cipher.init(Cipher.ENCRYPT_MODE, key, pbeParams);
    byte[] enc1 = cipher.doFinal(plaintext);
    byte[] enc2 = cipher.doFinal(plaintext);
    if (Arrays.equals(enc1, enc2) == false) {
        throw new Exception("Re-encryption test failed");
    }
    pbeParams = cipher.getParameters();
    cipher.init(Cipher.DECRYPT_MODE, key, pbeParams);
    byte[] dec = cipher.doFinal(enc1);
    if (Arrays.equals(plaintext, dec) == false) {
        throw new Exception("decryption test for " + alg + " failed");
    }

    PBEParameterSpec spec = (PBEParameterSpec)
        pbeParams.getParameterSpec(PBEParameterSpec.class);
    PBEKey key2 = new
        MyPBEKey(password, spec.getSalt(), spec.getIterationCount());
    cipher.init(Cipher.DECRYPT_MODE, key2, pbeParams);
    byte[] dec2 = cipher.doFinal(enc1);
    if (Arrays.equals(dec2, dec) == false) {
        throw new Exception("Re-decryption test#1 failed");
    }

    cipher.init(Cipher.DECRYPT_MODE, key2, (AlgorithmParameters) null);
    byte[] dec3 = cipher.doFinal(enc1);
    if (Arrays.equals(dec3, dec) == false) {
        throw new Exception("Re-decryption test#2 failed");
    }

    System.out.println("passed: " + alg);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:40,代码来源:PKCS12Cipher.java

示例2: createPBEKey

import javax.crypto.interfaces.PBEKey; //导入依赖的package包/类
private static SecretKey createPBEKey(byte[] salt, String password) throws NoSuchAlgorithmException, InvalidKeySpecException {
	if (password == null || password.length() == 0) {
		throw new IllegalArgumentException(L10nUtil.getMessage(MessageCodes.PBE_PASSWORD_ZERO_LENGTH_ERROR, DefaultMessages.PBE_PASSWORD_ZERO_LENGTH_ERROR));
	}
	SecretKeyFactory factory = SecretKeyFactory.getInstance(PBE_KEY_ALGORITHM);
	PBEKey pbeKey = (PBEKey) factory.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_ITERATIONS, SYMMETRIC_KEY_LENGTH));
	// int rounds = PBE_KEY_ITERATIONS;
	// PBEKey pbeKey = (PBEKey) factory.generateSecret(new PBEKeySpec(password.toCharArray(), salt, rounds, SYMMETRIC_KEY_LENGTH));
	// if ((rounds = Settings.getInt(SettingCodes.PBE_KEY_ITERATIONS, Bundle.SETTINGS, DefaultSettings.PBE_KEY_ITERATIONS)) > 1) {
	// factory.generateSecret(new PBEKeySpec(password.toCharArray(), salt, rounds - 1, SYMMETRIC_KEY_LENGTH));
	// }
	return new SecretKeySpec(pbeKey.getEncoded(), SYMMETRIC_ALGORITHM);
}
 
开发者ID:phoenixctms,项目名称:ctsms,代码行数:14,代码来源:CryptoUtil.java

示例3: engineInit

import javax.crypto.interfaces.PBEKey; //导入依赖的package包/类
protected void engineInit(int opmode, Key key, SecureRandom random)
    throws InvalidKeyException
{
  if (! (key instanceof PBEKey))
    throw new InvalidKeyException("not a PBE key");
  super.engineInit(opmode, genkey((PBEKey) key), random);
}
 
开发者ID:vilie,项目名称:javify,代码行数:8,代码来源:PBES2.java

示例4: genkey

import javax.crypto.interfaces.PBEKey; //导入依赖的package包/类
private SecretKeySpec genkey(PBEKey key) throws InvalidKeyException
{
  IRandom kdf = PRNGFactory.getInstance("PBKDF2-" + macName);
  if (kdf == null)
    throw new IllegalArgumentException("no such KDF: PBKDF2-" + macName);
  HashMap attrib = new HashMap();
  attrib.put(IPBE.ITERATION_COUNT, Integer.valueOf(key.getIterationCount()));
  attrib.put(IPBE.PASSWORD, key.getPassword());
  attrib.put(IPBE.SALT, key.getSalt());
  try
    {
      kdf.init(attrib);
    }
  catch (IllegalArgumentException iae)
    {
      throw new InvalidKeyException(iae.toString());
    }
  byte[] dk = new byte[mode.defaultKeySize()];
  try
    {
      kdf.nextBytes(dk, 0, dk.length);
    }
  catch (LimitReachedException shouldNotHappen)
    {
      throw new Error(String.valueOf(shouldNotHappen));
    }
  return new SecretKeySpec(dk, cipher.name());
}
 
开发者ID:vilie,项目名称:javify,代码行数:29,代码来源:PBES2.java


注:本文中的javax.crypto.interfaces.PBEKey类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。