Java Key Create getKeySize(PrivateKey key)

Here you can find the source of getKeySize(PrivateKey key)

Description

Returns the key size of the given key object in bits.

License

Open Source License

Parameter

Parameter Description
key the key object, cannot be null

Return

the key size of the given key object in bits, or -1 if the key size is not accessible

Declaration

public static final int getKeySize(PrivateKey key) 

Method Source Code


//package com.java2s;
/*//  www .  j av  a 2 s  .  c o m
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * This code is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 only, as published by
 * the Free Software Foundation. Oracle designates this particular file as
 * subject to the "Classpath" exception as provided by Oracle in the LICENSE
 * file that accompanied this code.
 * 
 * This code is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License version 2 for more
 * details (a copy is included in the LICENSE file that accompanied this code).
 * 
 * You should have received a copy of the GNU General Public License version 2
 * along with this work; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA or
 * visit www.oracle.com if you need additional information or have any
 * questions.
 */

import javax.crypto.interfaces.DHKey;

import java.security.PrivateKey;
import java.security.interfaces.DSAKey;
import java.security.interfaces.ECKey;
import java.security.interfaces.RSAKey;

public class Main {
    /**
     * Returns the key size of the given key object in bits.
     *
     * @param key
     *            the key object, cannot be null
     * @return the key size of the given key object in bits, or -1 if the key
     *         size is not accessible
     */
    public static final int getKeySize(PrivateKey key) {
        int size = -1;

        // try to parse the length from key specification
        if (key instanceof RSAKey) {
            RSAKey pubk = (RSAKey) key;
            size = pubk.getModulus().bitLength();
        } else if (key instanceof ECKey) {
            ECKey pubk = (ECKey) key;
            size = pubk.getParams().getOrder().bitLength();
        } else if (key instanceof DSAKey) {
            DSAKey pubk = (DSAKey) key;
            size = pubk.getParams().getP().bitLength();
        } else if (key instanceof DHKey) {
            DHKey pubk = (DHKey) key;
            size = pubk.getParams().getP().bitLength();
        } // Otherwise, it may be a unextractable key of PKCS#11, or
          // a key we are not able to handle.

        return size;
    }
}

Related

  1. getKeyPair()
  2. getKeyPair(KeyStore keyStore, String alias, String password)
  3. getKeyPairGenerator(final String algorithm)
  4. getKeys()
  5. getKeySize(final Key key)
  6. getKeyStore()
  7. getKeyStore()
  8. getKeyStore(byte[] bytes, char[] password)
  9. getKeyStore(Certificate[] certificateChain, PrivateKey privateKey, char[] password)