javax.crypto.interfaces.DHKey Java Examples

The following examples show how to use javax.crypto.interfaces.DHKey. 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: KeyPairUtil.java    From portecle with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the key size of a public key.
 *
 * @param pubKey The public key
 * @return The key size, {@link #UNKNOWN_KEY_SIZE} if not known
 */
public static int getKeyLength(PublicKey pubKey)
{
	if (pubKey instanceof RSAKey)
	{
		return ((RSAKey) pubKey).getModulus().bitLength();
	}
	else if (pubKey instanceof DSAKey)
	{
		return ((DSAKey) pubKey).getParams().getP().bitLength();
	}
	else if (pubKey instanceof DHKey)
	{
		return ((DHKey) pubKey).getParams().getP().bitLength();
	}
	else if (pubKey instanceof ECKey)
	{
		// TODO: how to get key size from these?
		return UNKNOWN_KEY_SIZE;
	}

	LOG.warning("Don't know how to get key size from key " + pubKey);
	return UNKNOWN_KEY_SIZE;
}
 
Example #2
Source File: KeyPairUtil.java    From MaxKey with Apache License 2.0 6 votes vote down vote up
/**
 * Get the key size of a public key.
 * 
 * @param pubKey The public key
 * @return The key size, {@link #UNKNOWN_KEY_SIZE} if not known
 */
public static int getKeyLength(PublicKey pubKey)
{
	if (pubKey instanceof RSAKey)
	{
		return ((RSAKey) pubKey).getModulus().bitLength();
	}
	else if (pubKey instanceof DSAKey)
	{
		return ((DSAKey) pubKey).getParams().getP().bitLength();
	}
	else if (pubKey instanceof DHKey)
	{
		return ((DHKey) pubKey).getParams().getP().bitLength();
	}
	else if (pubKey instanceof ECKey)
	{
		// TODO: how to get key size from these?
		return UNKNOWN_KEY_SIZE;
	}

	_logger.warn("Don't know how to get key size from key " + pubKey);
	return UNKNOWN_KEY_SIZE;
}
 
Example #3
Source File: KeyAgreementCryptography.java    From crypto with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化密钥协商算法的乙方密钥对
 *
 * @param publicKey 甲方公钥的二进制形式
 * @return 乙方密钥对
 */
public Map<String, Key> initKey(byte[] publicKey) {
    PublicKey pubKey = this.toPublicKey(publicKey);
    KeyPairGenerator keyPairGenerator = getKeyPairGenerator();
    AlgorithmParameterSpec algorithmParameterSpec = null;
    if (pubKey instanceof DHKey) {
        algorithmParameterSpec = ((DHKey) pubKey).getParams();
    } else if (pubKey instanceof ECKey) {
        algorithmParameterSpec = ((ECKey) pubKey).getParams();
    } else {
        throw new CryptographyException(ExceptionInfo.NO_SUCH_ALGORITHM_EXCEPTION_INFO + getConfiguration().getKeyAlgorithm());
    }
    try {
        keyPairGenerator.initialize(algorithmParameterSpec);
    } catch (InvalidAlgorithmParameterException e) {
        throw new CryptographyException(ExceptionInfo.NO_SUCH_ALGORITHM_EXCEPTION_INFO + getConfiguration().getKeyAlgorithm(), e);
    }
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    Map<String, Key> keyMap = new HashMap<String, Key>();
    keyMap.put(PRIVATE_KEY, keyPair.getPrivate());
    keyMap.put(PUBLIC_KEY, keyPair.getPublic());
    return keyMap;
}
 
Example #4
Source File: IESCipher.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public int engineGetKeySize(Key key)
{
    if (key instanceof DHKey)
    {
        return ((DHKey)key).getParams().getP().bitLength();
    }
    else
    {
        throw new IllegalArgumentException("not a DH key");
    }
}
 
Example #5
Source File: IESCipher.java    From ripple-lib-java with ISC License 5 votes vote down vote up
public int engineGetKeySize(Key key)
{
    if (key instanceof DHKey)
    {
        return ((DHKey)key).getParams().getP().bitLength();
    }
    else
    {
        throw new IllegalArgumentException("not a DH key");
    }
}
 
Example #6
Source File: IESCipher.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
public int engineGetOutputSize(int inputLen)
{
    int len1, len2, len3;

    len1 = engine.getMac().getMacSize();

    if (key != null)
    {
        len2 = ((DHKey)key).getParams().getP().bitLength() / 8 + 1;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (engine.getCipher() == null)
    {
        len3 = inputLen;
    }
    else if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen);
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen - len1 - len2);
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        return buffer.size() + len1 + len2 + len3;
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        return buffer.size() - len1 - len2 + len3;
    }
    else
    {
        throw new IllegalStateException("IESCipher not initialised");
    }

}
 
Example #7
Source File: IESCipher.java    From ripple-lib-java with ISC License 4 votes vote down vote up
public int engineGetOutputSize(int inputLen)
{
    int len1, len2, len3;

    len1 = engine.getMac().getMacSize();

    if (key != null)
    {
        len2 = ((DHKey)key).getParams().getP().bitLength() / 8 + 1;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (engine.getCipher() == null)
    {
        len3 = inputLen;
    }
    else if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen);
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen - len1 - len2);
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        return buffer.size() + len1 + len2 + len3;
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        return buffer.size() - len1 - len2 + len3;
    }
    else
    {
        throw new IllegalStateException("IESCipher not initialised");
    }

}