/** * 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; }
/** * 初始化密钥协商算法的乙方密钥对 * * @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"); } }
/** * 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()); }
@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())); }
@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())); }
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"); } }