本文整理汇总了Java中javax.crypto.interfaces.DHKey类的典型用法代码示例。如果您正苦于以下问题:Java DHKey类的具体用法?Java DHKey怎么用?Java DHKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DHKey类属于javax.crypto.interfaces包,在下文中一共展示了DHKey类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getKeyLength
import javax.crypto.interfaces.DHKey; //导入依赖的package包/类
/**
* 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;
}
示例2: initKey
import javax.crypto.interfaces.DHKey; //导入依赖的package包/类
/**
* 初始化密钥协商算法的乙方密钥对
*
* @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;
}
示例3: engineGetKeySize
import javax.crypto.interfaces.DHKey; //导入依赖的package包/类
public int engineGetKeySize(Key key)
{
if (key instanceof DHKey)
{
return ((DHKey)key).getParams().getP().bitLength();
}
else
{
throw new IllegalArgumentException("not a DH key");
}
}
示例4: equals
import javax.crypto.interfaces.DHKey; //导入依赖的package包/类
/**
* Returns <code>true</code> if the designated object is an instance of
* {@link DHKey} and has the same Diffie-Hellman parameter values as this one.
*
* @param obj the other non-null DH key to compare to.
* @return <code>true</code> if the designated object is of the same type
* and value as this one.
*/
public boolean equals(Object obj)
{
if (obj == null)
return false;
if (! (obj instanceof DHKey))
return false;
DHKey that = (DHKey) obj;
return p.equals(that.getParams().getP())
&& g.equals(that.getParams().getG());
}
示例5: equals
import javax.crypto.interfaces.DHKey; //导入依赖的package包/类
/**
* Returns <code>true</code> if the designated object is an instance of
* {@link DHKey} and has the same Diffie-Hellman parameter values as this one.
*
* @param obj the other non-null DH key to compare to.
* @return <code>true</code> if the designated object is of the same type
* and value as this one.
*/
public boolean equals(Object obj)
{
if (obj == null)
return false;
if (! (obj instanceof DHKey))
return false;
DHKey that = (DHKey) obj;
return p.equals(that.getParams().getP())
&& g.equals(that.getParams().getG());
}
示例6: test_getParams
import javax.crypto.interfaces.DHKey; //导入依赖的package包/类
@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
method = "getX",
args = {}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
clazz = DHKey.class,
method = "getParams",
args = {}
)
})
@BrokenTest("Too slow - disabling for now")
public void test_getParams() throws Exception {
KeyPairGenerator kg = KeyPairGenerator.getInstance("DH");
kg.initialize(1024);
KeyPair kp1 = kg.genKeyPair();
KeyPair kp2 = kg.genKeyPair();
DHPrivateKey pk1 = (DHPrivateKey) kp1.getPrivate();
DHPrivateKey pk2 = (DHPrivateKey) kp2.getPrivate();
assertTrue(pk1.getX().getClass().getCanonicalName().equals("java.math.BigInteger"));
assertTrue(pk1.getParams().getClass().getCanonicalName().equals("javax.crypto.spec.DHParameterSpec"));
assertFalse(pk1.equals(pk2));
assertTrue(pk1.getX().equals(pk1.getX()));
}
示例7: test_getParams
import javax.crypto.interfaces.DHKey; //导入依赖的package包/类
@TestTargets({
@TestTargetNew(
level = TestLevel.COMPLETE,
method = "getY",
args = {}
),
@TestTargetNew(
level = TestLevel.COMPLETE,
clazz = DHKey.class,
method = "getParams",
args = {}
)
})
@BrokenTest("Too slow - disabling for now")
public void test_getParams() throws Exception {
KeyPairGenerator kg = KeyPairGenerator.getInstance("DH");
kg.initialize(1024);
KeyPair kp1 = kg.genKeyPair();
KeyPair kp2 = kg.genKeyPair();
DHPublicKey pk1 = (DHPublicKey) kp1.getPublic();
DHPublicKey pk2 = (DHPublicKey) kp2.getPublic();
assertTrue(pk1.getY().getClass().getCanonicalName().equals("java.math.BigInteger"));
assertTrue(pk2.getParams().getClass().getCanonicalName().equals("javax.crypto.spec.DHParameterSpec"));
assertFalse(pk1.equals(pk2));
assertTrue(pk1.getY().equals(pk1.getY()));
}
示例8: engineGetOutputSize
import javax.crypto.interfaces.DHKey; //导入依赖的package包/类
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");
}
}