下面列出了java.security.interfaces.ECKey#javax.crypto.interfaces.DHKey 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
/**
* 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;
}
/**
* 初始化密钥协商算法的乙方密钥对
*
* @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;
}
public int engineGetKeySize(Key key)
{
if (key instanceof DHKey)
{
return ((DHKey)key).getParams().getP().bitLength();
}
else
{
throw new IllegalArgumentException("not a DH key");
}
}
public int engineGetKeySize(Key key)
{
if (key instanceof DHKey)
{
return ((DHKey)key).getParams().getP().bitLength();
}
else
{
throw new IllegalArgumentException("not a DH key");
}
}
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");
}
}
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");
}
}