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


Java PBEKeySpec.getKeyLength方法代码示例

本文整理汇总了Java中javax.crypto.spec.PBEKeySpec.getKeyLength方法的典型用法代码示例。如果您正苦于以下问题:Java PBEKeySpec.getKeyLength方法的具体用法?Java PBEKeySpec.getKeyLength怎么用?Java PBEKeySpec.getKeyLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.crypto.spec.PBEKeySpec的用法示例。


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

示例1: engineGenerateSecret

import javax.crypto.spec.PBEKeySpec; //导入方法依赖的package包/类
protected SecretKey engineGenerateSecret(
    KeySpec keySpec)
    throws InvalidKeySpecException
{
    if (keySpec instanceof PBEKeySpec)
    {
        PBEKeySpec pbeSpec = (PBEKeySpec)keySpec;

        if (pbeSpec.getSalt() == null)
        {
            throw new InvalidKeySpecException("missing required salt");
        }

        if (pbeSpec.getIterationCount() <= 0)
        {
            throw new InvalidKeySpecException("positive iteration count required: "
                + pbeSpec.getIterationCount());
        }

        if (pbeSpec.getKeyLength() <= 0)
        {
            throw new InvalidKeySpecException("positive key length required: "
                + pbeSpec.getKeyLength());
        }

        if (pbeSpec.getPassword().length == 0)
        {
            throw new IllegalArgumentException("password empty");
        }

        int digest = SHA1;
        int keySize = pbeSpec.getKeyLength();
        int ivSize = -1;    // JDK 1,2 and earlier does not understand simplified version.
        CipherParameters param = PBE.Util.makePBEMacParameters(pbeSpec, scheme, digest, keySize);

        return new BCPBEKey(this.algName, this.algOid, scheme, digest, keySize, ivSize, pbeSpec, param);
    }

    throw new InvalidKeySpecException("Invalid KeySpec");
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:41,代码来源:SHA1.java

示例2: PBKDF2KeyImpl

import javax.crypto.spec.PBEKeySpec; //导入方法依赖的package包/类
/**
 * Creates a PBE key from a given PBE key specification.
 *
 * @param key the given PBE key specification
 */
PBKDF2KeyImpl(PBEKeySpec keySpec, String prfAlgo)
    throws InvalidKeySpecException {
    char[] passwd = keySpec.getPassword();
    if (passwd == null) {
        // Should allow an empty password.
        this.passwd = new char[0];
    } else {
        this.passwd = passwd.clone();
    }
    // Convert the password from char[] to byte[]
    byte[] passwdBytes = getPasswordBytes(this.passwd);

    this.salt = keySpec.getSalt();
    if (salt == null) {
        throw new InvalidKeySpecException("Salt not found");
    }
    this.iterCount = keySpec.getIterationCount();
    if (iterCount == 0) {
        throw new InvalidKeySpecException("Iteration count not found");
    } else if (iterCount < 0) {
        throw new InvalidKeySpecException("Iteration count is negative");
    }
    int keyLength = keySpec.getKeyLength();
    if (keyLength == 0) {
        throw new InvalidKeySpecException("Key length not found");
    } else if (keyLength < 0) {
        throw new InvalidKeySpecException("Key length is negative");
    }
    try {
        this.prf = Mac.getInstance(prfAlgo, SunJCE.getInstance());
    } catch (NoSuchAlgorithmException nsae) {
        // not gonna happen; re-throw just in case
        InvalidKeySpecException ike = new InvalidKeySpecException();
        ike.initCause(nsae);
        throw ike;
    }
    this.key = deriveKey(prf, passwdBytes, salt, iterCount, keyLength);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:44,代码来源:PBKDF2KeyImpl.java

示例3: PBKDF2KeyImpl

import javax.crypto.spec.PBEKeySpec; //导入方法依赖的package包/类
/**
 * Creates a PBE key from a given PBE key specification.
 *
 * @param keySpec the given PBE key specification
 * @param prfAlgo the given PBE key algorithm
 */
PBKDF2KeyImpl(PBEKeySpec keySpec, String prfAlgo)
    throws InvalidKeySpecException {
    char[] passwd = keySpec.getPassword();
    if (passwd == null) {
        // Should allow an empty password.
        this.passwd = new char[0];
    } else {
        this.passwd = passwd.clone();
    }
    // Convert the password from char[] to byte[]
    byte[] passwdBytes = getPasswordBytes(this.passwd);

    this.salt = keySpec.getSalt();
    if (salt == null) {
        throw new InvalidKeySpecException("Salt not found");
    }
    this.iterCount = keySpec.getIterationCount();
    if (iterCount == 0) {
        throw new InvalidKeySpecException("Iteration count not found");
    } else if (iterCount < 0) {
        throw new InvalidKeySpecException("Iteration count is negative");
    }
    int keyLength = keySpec.getKeyLength();
    if (keyLength == 0) {
        throw new InvalidKeySpecException("Key length not found");
    } else if (keyLength < 0) {
        throw new InvalidKeySpecException("Key length is negative");
    }
    try {
        this.prf = Mac.getInstance(prfAlgo);
        // SunPKCS11 requires a non-empty PBE password
        if (passwdBytes.length == 0 &&
            this.prf.getProvider().getName().startsWith("SunPKCS11")) {
            this.prf = Mac.getInstance(prfAlgo, SunJCE.getInstance());
        }
    } catch (NoSuchAlgorithmException nsae) {
        // not gonna happen; re-throw just in case
        InvalidKeySpecException ike = new InvalidKeySpecException();
        ike.initCause(nsae);
        throw ike;
    }
    this.key = deriveKey(prf, passwdBytes, salt, iterCount, keyLength);

    // Use the cleaner to zero the key when no longer referenced
    final byte[] k = this.key;
    final char[] p = this.passwd;
    CleanerFactory.cleaner().register(this,
            () -> {
                java.util.Arrays.fill(k, (byte)0x00);
                java.util.Arrays.fill(p, '0');
            });
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:59,代码来源:PBKDF2KeyImpl.java


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