use of javax.crypto.interfaces.DHPublicKey in project Bytecoder by mirkosertic.
the class DHCrypt method checkConstraints.
// Check constraints of the specified DH public key.
void checkConstraints(AlgorithmConstraints constraints, BigInteger peerPublicValue) throws SSLHandshakeException {
try {
KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
DHPublicKeySpec spec = new DHPublicKeySpec(peerPublicValue, modulus, base);
DHPublicKey publicKey = (DHPublicKey) kf.generatePublic(spec);
// check constraints of DHPublicKey
if (!constraints.permits(EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), publicKey)) {
throw new SSLHandshakeException("DHPublicKey does not comply to algorithm constraints");
}
} catch (GeneralSecurityException gse) {
throw (SSLHandshakeException) new SSLHandshakeException("Could not generate DHPublicKey").initCause(gse);
}
}
use of javax.crypto.interfaces.DHPublicKey in project protools by SeanDragon.
the class ToolDH method initKey.
/**
* 初始化甲方密钥
*
* @return Map 甲方密钥Map
*
* @throws Exception
*/
public static Map<String, Object> initKey() throws NoSuchAlgorithmException {
// 实例化密钥对生成器
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
// 初始化密钥对生成器
keyPairGenerator.initialize(KEY_SIZE);
// 生成密钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 甲方公钥
DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();
// 甲方私钥
DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();
// 将密钥对存储在Map中
Map<String, Object> keyMap = Maps.newHashMapWithExpectedSize(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
use of javax.crypto.interfaces.DHPublicKey in project Gradle-demo by Arisono.
the class DHUtil method initKey.
/**
* 乙方根据甲方公钥初始化并返回密钥对
*/
public static Map<String, Object> initKey(byte[] key) throws Exception {
// 将甲方公钥从字节数组转换为publicKey
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key);
// 实例化密钥工厂
KeyFactory keyFactory = KeyFactory.getInstance("DH");
// 产生甲方公钥pubKey
DHPublicKey dhPublicKey = (DHPublicKey) keyFactory.generatePublic(keySpec);
// 剖析甲方公钥,得到其参数
DHParameterSpec dhParameterSpec = dhPublicKey.getParams();
// 实例化密钥对生成器
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DH");
// 用甲方公钥初始化密钥对生成器
keyPairGenerator.initialize(dhParameterSpec);
// 产生密钥对
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 得到乙方公钥
DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();
// 得到乙方私钥
DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();
// 将公钥和私钥封装到Map中,方便以后使用
Map<String, Object> keyMap = new HashMap<String, Object>();
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
Aggregations