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


Java RSAKeyFactory.checkKeyLengths方法代码示例

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


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

示例1: generatePublic

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
private PublicKey generatePublic(BigInteger n, BigInteger e)
        throws PKCS11Exception, InvalidKeyException {
    RSAKeyFactory.checkKeyLengths(n.bitLength(), e, -1, 64 * 1024);
    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
        new CK_ATTRIBUTE(CKA_MODULUS, n),
        new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, e),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PUBLIC_KEY, CKK_RSA, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.publicKey
            (session, keyID, "RSA", n.bitLength(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:22,代码来源:P11RSAKeyFactory.java

示例2: engineInitSign

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
protected void engineInitSign(PrivateKey key) throws InvalidKeyException
{
    // This signature accepts only RSAPrivateKey
    if ((key instanceof sun.security.mscapi.RSAPrivateKey) == false) {
        throw new InvalidKeyException("Key type not supported");
    }
    privateKey = (sun.security.mscapi.RSAPrivateKey) key;

    // Check against the local and global values to make sure
    // the sizes are ok.  Round up to nearest byte.
    RSAKeyFactory.checkKeyLengths(((privateKey.length() + 7) & ~7),
        null, RSAKeyPairGenerator.KEY_SIZE_MIN,
        RSAKeyPairGenerator.KEY_SIZE_MAX);

    this.publicKey = null;
    resetDigest();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:RSASignature.java

示例3: generatePrivate

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
private PrivateKey generatePrivate(BigInteger n, BigInteger d)
        throws PKCS11Exception, InvalidKeyException {
    RSAKeyFactory.checkKeyLengths(n.bitLength(), null, -1, 64 * 1024);
    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
        new CK_ATTRIBUTE(CKA_MODULUS, n),
        new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT, d),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.privateKey
            (session,  keyID, "RSA", n.bitLength(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:P11RSAKeyFactory.java

示例4: engineInitSign

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
protected void engineInitSign(PrivateKey key) throws InvalidKeyException
{
    // This signature accepts only RSAPrivateKey
    if ((key instanceof sun.security.mscapi.RSAPrivateKey) == false) {
        throw new InvalidKeyException("Key type not supported");
    }
    privateKey = (sun.security.mscapi.RSAPrivateKey) key;

    // Check against the local and global values to make sure
    // the sizes are ok.  Round up to nearest byte.
    RSAKeyFactory.checkKeyLengths(((privateKey.bitLength() + 7) & ~7),
        null, RSAKeyPairGenerator.KEY_SIZE_MIN,
        RSAKeyPairGenerator.KEY_SIZE_MAX);

    this.publicKey = null;
    resetDigest();
}
 
开发者ID:aducode,项目名称:openjdk-source-code-learn,代码行数:18,代码来源:RSASignature.java

示例5: initialize

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
public void initialize(int keySize, SecureRandom random) {

        try {
            RSAKeyFactory.checkKeyLengths(keySize, null,
                KEY_SIZE_MIN, KEY_SIZE_MAX);
        } catch (InvalidKeyException e) {
            throw new InvalidParameterException(e.getMessage());
        }

        this.keySize = keySize;
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:RSAKeyPairGenerator.java

示例6: generatePrivate

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
private PrivateKey generatePrivate(BigInteger n, BigInteger e,
        BigInteger d, BigInteger p, BigInteger q, BigInteger pe,
        BigInteger qe, BigInteger coeff) throws PKCS11Exception,
        InvalidKeyException {
    RSAKeyFactory.checkKeyLengths(n.bitLength(), e, -1, 64 * 1024);
    CK_ATTRIBUTE[] attributes = new CK_ATTRIBUTE[] {
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA),
        new CK_ATTRIBUTE(CKA_MODULUS, n),
        new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, e),
        new CK_ATTRIBUTE(CKA_PRIVATE_EXPONENT, d),
        new CK_ATTRIBUTE(CKA_PRIME_1, p),
        new CK_ATTRIBUTE(CKA_PRIME_2, q),
        new CK_ATTRIBUTE(CKA_EXPONENT_1, pe),
        new CK_ATTRIBUTE(CKA_EXPONENT_2, qe),
        new CK_ATTRIBUTE(CKA_COEFFICIENT, coeff),
    };
    attributes = token.getAttributes
            (O_IMPORT, CKO_PRIVATE_KEY, CKK_RSA, attributes);
    Session session = null;
    try {
        session = token.getObjSession();
        long keyID = token.p11.C_CreateObject(session.id(), attributes);
        return P11Key.privateKey
            (session, keyID, "RSA", n.bitLength(), attributes);
    } finally {
        token.releaseSession(session);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:30,代码来源:P11RSAKeyFactory.java

示例7: initialize

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {

    int tmpSize;
    if (params == null) {
        tmpSize = KEY_SIZE_DEFAULT;
    } else if (params instanceof RSAKeyGenParameterSpec) {

        if (((RSAKeyGenParameterSpec) params).getPublicExponent() != null) {
            throw new InvalidAlgorithmParameterException
                ("Exponent parameter is not supported");
        }
        tmpSize = ((RSAKeyGenParameterSpec) params).getKeysize();

    } else {
        throw new InvalidAlgorithmParameterException
            ("Params must be an instance of RSAKeyGenParameterSpec");
    }

    try {
        RSAKeyFactory.checkKeyLengths(tmpSize, null,
            KEY_SIZE_MIN, KEY_SIZE_MAX);
    } catch (InvalidKeyException e) {
        throw new InvalidAlgorithmParameterException(
            "Invalid Key sizes", e);
    }

    this.keySize = tmpSize;
}
 
开发者ID:greghaskins,项目名称:openjdk-jdk7u-jdk,代码行数:30,代码来源:RSAKeyPairGenerator.java

示例8: checkKeySize

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
private void checkKeySize(int keySize, RSAKeyGenParameterSpec params)
    throws InvalidAlgorithmParameterException {
    // check native range first
    if ((minKeySize != -1) && (keySize < minKeySize)) {
        throw new InvalidAlgorithmParameterException(algorithm +
            " key must be at least " + minKeySize + " bits");
    }
    if ((maxKeySize != -1) && (keySize > maxKeySize)) {
        throw new InvalidAlgorithmParameterException(algorithm +
            " key must be at most " + maxKeySize + " bits");
    }

    // check our own algorithm-specific limits also
    if (algorithm.equals("EC")) {
        if (keySize < 112) {
            throw new InvalidAlgorithmParameterException
                ("Key size must be at least 112 bit");
        }
        if (keySize > 2048) {
            // sanity check, nobody really wants keys this large
            throw new InvalidAlgorithmParameterException
                ("Key size must be at most 2048 bit");
        }
    } else {
        // RSA, DH, DSA
        if (keySize < 512) {
            throw new InvalidAlgorithmParameterException
                ("Key size must be at least 512 bit");
        }
        if (algorithm.equals("RSA")) {
            BigInteger tmpExponent = rsaPublicExponent;
            if (params != null) {
                tmpExponent = params.getPublicExponent();
            }
            try {
                // Reuse the checking in SunRsaSign provider.
                // If maxKeySize is -1, then replace it with
                // Integer.MAX_VALUE to indicate no limit.
                RSAKeyFactory.checkKeyLengths(keySize, tmpExponent,
                    minKeySize,
                    (maxKeySize==-1? Integer.MAX_VALUE:maxKeySize));
            } catch (InvalidKeyException e) {
                throw new InvalidAlgorithmParameterException(e.getMessage());
            }
        } else {
            if (algorithm.equals("DH") && (params != null)) {
                // sanity check, nobody really wants keys this large
                if (keySize > 64 * 1024) {
                    throw new InvalidAlgorithmParameterException
                        ("Key size must be at most 65536 bit");
                }
            } else {
                // this restriction is in the spec for DSA
                // since we currently use DSA parameters for DH as well,
                // it also applies to DH if no parameters are specified
                if ((keySize != 2048) &&
                    ((keySize > 1024) || ((keySize & 0x3f) != 0))) {
                    throw new InvalidAlgorithmParameterException(algorithm +
                        " key must be multiples of 64 if less than 1024 bits" +
                        ", or 2048 bits");
                }
            }
        }
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:66,代码来源:P11KeyPairGenerator.java

示例9: init

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
private void init(int opmode, Key key) throws InvalidKeyException {

        boolean encrypt;

        switch (opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            paddingLength = PAD_PKCS1_LENGTH;
            encrypt = true;
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            paddingLength = 0; // reset
            encrypt = false;
            break;
        default:
            throw new InvalidKeyException("Unknown mode: " + opmode);
        }

        if (!(key instanceof sun.security.mscapi.Key)) {
            if (key instanceof java.security.interfaces.RSAPublicKey) {
                java.security.interfaces.RSAPublicKey rsaKey =
                    (java.security.interfaces.RSAPublicKey) key;

                // Convert key to MSCAPI format

                BigInteger modulus = rsaKey.getModulus();
                BigInteger exponent =  rsaKey.getPublicExponent();

                // Check against the local and global values to make sure
                // the sizes are ok.  Round up to the nearest byte.
                RSAKeyFactory.checkKeyLengths(((modulus.bitLength() + 7) & ~7),
                    exponent, -1, RSAKeyPairGenerator.KEY_SIZE_MAX);

                byte[] modulusBytes = modulus.toByteArray();
                byte[] exponentBytes = exponent.toByteArray();

                // Adjust key length due to sign bit
                int keyBitLength = (modulusBytes[0] == 0)
                    ? (modulusBytes.length - 1) * 8
                    : modulusBytes.length * 8;

                byte[] keyBlob = RSASignature.generatePublicKeyBlob(
                    keyBitLength, modulusBytes, exponentBytes);

                try {
                    key = RSASignature.importPublicKey(keyBlob, keyBitLength);

                } catch (KeyStoreException e) {
                    throw new InvalidKeyException(e);
                }

            } else {
                throw new InvalidKeyException("Unsupported key type: " + key);
            }
        }

        if (key instanceof PublicKey) {
            mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
            publicKey = (sun.security.mscapi.Key)key;
            privateKey = null;
            outputSize = publicKey.length() / 8;
        } else if (key instanceof PrivateKey) {
            mode = encrypt ? MODE_SIGN : MODE_DECRYPT;
            privateKey = (sun.security.mscapi.Key)key;
            publicKey = null;
            outputSize = privateKey.length() / 8;
        } else {
            throw new InvalidKeyException("Unknown key type: " + key);
        }

        bufOfs = 0;
        buffer = new byte[outputSize];
    }
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:75,代码来源:RSACipher.java

示例10: engineInitVerify

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
protected void engineInitVerify(PublicKey key)
    throws InvalidKeyException
{
    // This signature accepts only RSAPublicKey
    if ((key instanceof java.security.interfaces.RSAPublicKey) == false) {
        throw new InvalidKeyException("Key type not supported");
    }

    java.security.interfaces.RSAPublicKey rsaKey =
        (java.security.interfaces.RSAPublicKey) key;

    if ((key instanceof sun.security.mscapi.RSAPublicKey) == false) {

        // convert key to MSCAPI format

        BigInteger modulus = rsaKey.getModulus();
        BigInteger exponent =  rsaKey.getPublicExponent();

        // Check against the local and global values to make sure
        // the sizes are ok.  Round up to the nearest byte.
        RSAKeyFactory.checkKeyLengths(((modulus.bitLength() + 7) & ~7),
            exponent, -1, RSAKeyPairGenerator.KEY_SIZE_MAX);

        byte[] modulusBytes = modulus.toByteArray();
        byte[] exponentBytes = exponent.toByteArray();

        // Adjust key length due to sign bit
        int keyBitLength = (modulusBytes[0] == 0)
            ? (modulusBytes.length - 1) * 8
            : modulusBytes.length * 8;

        byte[] keyBlob = generatePublicKeyBlob(
            keyBitLength, modulusBytes, exponentBytes);

        try {
            publicKey = importPublicKey(keyBlob, keyBitLength);

        } catch (KeyStoreException e) {
            throw new InvalidKeyException(e);
        }

    } else {
        publicKey = (sun.security.mscapi.RSAPublicKey) key;
    }

    this.privateKey = null;
    resetDigest();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:49,代码来源:RSASignature.java

示例11: checkKeySize

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
private void checkKeySize(int keySize, RSAKeyGenParameterSpec params)
    throws InvalidAlgorithmParameterException {
    // check native range first
    if ((minKeySize != -1) && (keySize < minKeySize)) {
        throw new InvalidAlgorithmParameterException(algorithm +
            " key must be at least " + minKeySize + " bits. " +
            "The specific key size " + keySize + " is not supported");
    }
    if ((maxKeySize != -1) && (keySize > maxKeySize)) {
        throw new InvalidAlgorithmParameterException(algorithm +
            " key must be at most " + maxKeySize + " bits. " +
            "The specific key size " + keySize + " is not supported");
    }

    // check our own algorithm-specific limits also
    if (algorithm.equals("EC")) {
        if (keySize < 112) {
                throw new InvalidAlgorithmParameterException(
                "EC key size must be at least 112 bit. " +
                "The specific key size " + keySize + " is not supported");
        }
        if (keySize > 2048) {
            // sanity check, nobody really wants keys this large
            throw new InvalidAlgorithmParameterException(
                "EC key size must be at most 2048 bit. " +
                "The specific key size " + keySize + " is not supported");
        }
    } else {
        // RSA, DH, DSA
        if (keySize < 512) {
            throw new InvalidAlgorithmParameterException(algorithm +
                " key size must be at least 512 bit. " +
                "The specific key size " + keySize + " is not supported");
        }
        if (algorithm.equals("RSA")) {
            BigInteger tmpExponent = rsaPublicExponent;
            if (params != null) {
                tmpExponent = params.getPublicExponent();
            }
            try {
                // Reuse the checking in SunRsaSign provider.
                // If maxKeySize is -1, then replace it with
                // Integer.MAX_VALUE to indicate no limit.
                RSAKeyFactory.checkKeyLengths(keySize, tmpExponent,
                    minKeySize,
                    (maxKeySize==-1? Integer.MAX_VALUE:maxKeySize));
            } catch (InvalidKeyException e) {
                throw new InvalidAlgorithmParameterException(e.getMessage());
            }
        } else {
            if (algorithm.equals("DH") && (params != null)) {
                // sanity check, nobody really wants keys this large
                if (keySize > 64 * 1024) {
                    throw new InvalidAlgorithmParameterException(
                        "DH key size must be at most 65536 bit. " +
                        "The specific key size " +
                        keySize + " is not supported");
                }
            } else {
                // this restriction is in the spec for DSA
                // since we currently use DSA parameters for DH as well,
                // it also applies to DH if no parameters are specified
                if ((keySize != 2048) &&
                    ((keySize > 1024) || ((keySize & 0x3f) != 0))) {
                    throw new InvalidAlgorithmParameterException(algorithm +
                        " key must be multiples of 64 if less than 1024 bits" +
                        ", or 2048 bits. " +
                        "The specific key size " +
                        keySize + " is not supported");
                }
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:75,代码来源:P11KeyPairGenerator.java

示例12: checkKeySize

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
private void checkKeySize(int keySize, AlgorithmParameterSpec params)
        throws InvalidAlgorithmParameterException {
    if (algorithm.equals("EC")) {
        if (keySize < 112) {
            throw new InvalidAlgorithmParameterException
                ("Key size must be at least 112 bit");
        }
        if (keySize > 2048) {
            // sanity check, nobody really wants keys this large
            throw new InvalidAlgorithmParameterException
                ("Key size must be at most 2048 bit");
        }
        return;
    } else if (algorithm.equals("RSA")) {
        BigInteger tmpExponent = rsaPublicExponent;
        if (params != null) {
            // Already tested for instanceof RSAKeyGenParameterSpec above
            tmpExponent =
                ((RSAKeyGenParameterSpec)params).getPublicExponent();
        }
        try {
            // This provider supports 64K or less.
            RSAKeyFactory.checkKeyLengths(keySize, tmpExponent,
                512, 64 * 1024);
        } catch (InvalidKeyException e) {
            throw new InvalidAlgorithmParameterException(e.getMessage());
        }
        return;
    }

    if (keySize < 512) {
        throw new InvalidAlgorithmParameterException
            ("Key size must be at least 512 bit");
    }
    if (algorithm.equals("DH") && (params != null)) {
        // sanity check, nobody really wants keys this large
        if (keySize > 64 * 1024) {
            throw new InvalidAlgorithmParameterException
                ("Key size must be at most 65536 bit");
        }
    } else {
        // this restriction is in the spec for DSA
        // since we currently use DSA parameters for DH as well,
        // it also applies to DH if no parameters are specified
        if ((keySize > 1024) || ((keySize & 0x3f) != 0)) {
            throw new InvalidAlgorithmParameterException
                ("Key size must be a multiple of 64 and at most 1024 bit");
        }
    }
}
 
开发者ID:greghaskins,项目名称:openjdk-jdk7u-jdk,代码行数:51,代码来源:P11KeyPairGenerator.java

示例13: init

import sun.security.rsa.RSAKeyFactory; //导入方法依赖的package包/类
private void init(int opmode, Key key) throws InvalidKeyException {

        boolean encrypt;

        switch (opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            paddingLength = PAD_PKCS1_LENGTH;
            encrypt = true;
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            paddingLength = 0; // reset
            encrypt = false;
            break;
        default:
            throw new InvalidKeyException("Unknown mode: " + opmode);
        }

        if (!(key instanceof sun.security.mscapi.Key)) {
            if (key instanceof java.security.interfaces.RSAPublicKey) {
                java.security.interfaces.RSAPublicKey rsaKey =
                    (java.security.interfaces.RSAPublicKey) key;

                // Convert key to MSCAPI format

                BigInteger modulus = rsaKey.getModulus();
                BigInteger exponent =  rsaKey.getPublicExponent();

                // Check against the local and global values to make sure
                // the sizes are ok.  Round up to the nearest byte.
                RSAKeyFactory.checkKeyLengths(((modulus.bitLength() + 7) & ~7),
                    exponent, -1, RSAKeyPairGenerator.KEY_SIZE_MAX);

                byte[] modulusBytes = modulus.toByteArray();
                byte[] exponentBytes = exponent.toByteArray();

                // Adjust key length due to sign bit
                int keyBitLength = (modulusBytes[0] == 0)
                    ? (modulusBytes.length - 1) * 8
                    : modulusBytes.length * 8;

                byte[] keyBlob = RSASignature.generatePublicKeyBlob(
                    keyBitLength, modulusBytes, exponentBytes);

                try {
                    key = RSASignature.importPublicKey(keyBlob, keyBitLength);

                } catch (KeyStoreException e) {
                    throw new InvalidKeyException(e);
                }

            } else {
                throw new InvalidKeyException("Unsupported key type: " + key);
            }
        }

        if (key instanceof PublicKey) {
            mode = encrypt ? MODE_ENCRYPT : MODE_VERIFY;
            publicKey = (sun.security.mscapi.Key)key;
            privateKey = null;
            outputSize = publicKey.bitLength() / 8;
        } else if (key instanceof PrivateKey) {
            mode = encrypt ? MODE_SIGN : MODE_DECRYPT;
            privateKey = (sun.security.mscapi.Key)key;
            publicKey = null;
            outputSize = privateKey.bitLength() / 8;
        } else {
            throw new InvalidKeyException("Unknown key type: " + key);
        }

        bufOfs = 0;
        buffer = new byte[outputSize];
    }
 
开发者ID:openjdk,项目名称:jdk7-jdk,代码行数:75,代码来源:RSACipher.java


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