Code example for DHParameterSpec

Methods: getG, getP

0
DHParameterSpec parameterSpec = DiffieHellmanSession.generateRandomParameter(512, 256);
 
        assertNotNull(parameterSpec);
 
        assertEquals(512, parameterSpec.getP().bitLength());
    } 
 
    public void testGenerateRandomParameterSha256() throws Exception 
    { 
        DHParameterSpec parameterSpec = DiffieHellmanSession.generateRandomParameter(512, 256);
 
        assertNotNull(parameterSpec);
 
        assertEquals(512, parameterSpec.getP().bitLength());
    } 
 
    public void testGenerateKeyPairSha1Default() 
    { 
        DHParameterSpec parameterSpec = DiffieHellmanSession.getDefaultParameter();
 
        KeyPair keyPair = DiffieHellmanSession.generateKeyPair(parameterSpec);
 
        assertNotNull(keyPair);
    } 
 
    public void testGenerateKeyPairSha256Default() 
    { 
        DHParameterSpec parameterSpec = DiffieHellmanSession.getDefaultParameter();
 
        KeyPair keyPair = DiffieHellmanSession.generateKeyPair(parameterSpec);
 
        assertNotNull(keyPair);
    } 
 
    public void testGenerateKeyPairSha1Random() 
    { 
        DHParameterSpec parameterSpec = DiffieHellmanSession.generateRandomParameter(512, 256);
 
        KeyPair keyPair = DiffieHellmanSession.generateKeyPair(parameterSpec);
 
        assertNotNull(keyPair);
    } 
 
    public void testGenerateKeyPairSha256Random() 
    { 
        DHParameterSpec parameterSpec = DiffieHellmanSession.generateRandomParameter(512, 256);
 
        KeyPair keyPair = DiffieHellmanSession.generateKeyPair(parameterSpec);
 
        assertNotNull(keyPair);
    } 
 
    public void testPublicKeyConversion() throws AssociationException 
    { 
        DHParameterSpec dhParameterSpec = DiffieHellmanSession.getDefaultParameter();
 
        DiffieHellmanSession diffieHellmanSession = DiffieHellmanSession.create(AssociationSessionType.DH_SHA1, dhParameterSpec);
 
        String publicKeyBase64 = diffieHellmanSession.getPublicKey();
 
        assertNotNull(publicKeyBase64);
 
        DHPublicKey publicKey = diffieHellmanSession.stringToPublicKey(publicKeyBase64);
 
        assertNotNull(publicKey);
        assertEquals(publicKeyBase64, DiffieHellmanSession.publicKeyToString(publicKey));
    } 
 
    public void testEncryptDecryptMacKeySha1() throws GeneralSecurityException, AssociationException 
    { 
        DHParameterSpec dhParameterSpec = DiffieHellmanSession.getDefaultParameter();
 
        assertNotNull(dhParameterSpec);
 
        DiffieHellmanSession consumerDiffieHellmanSession = DiffieHellmanSession.create(AssociationSessionType.DH_SHA1, dhParameterSpec);
        byte[] macKey = Association.generateMacKey(Association.HMAC_SHA1_ALGORITHM, Association.HMAC_SHA1_KEYSIZE).getEncoded();
 
        testEncryptDecryptMacKey(consumerDiffieHellmanSession, macKey);
    } 
 
    public void testEncryptDecryptMacKeySha1Random() throws GeneralSecurityException, AssociationException