Diffie-Hellman key exchange : Key Generator « Security « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Security » Key Generator 




Diffie-Hellman key exchange
        

//Copyright 2007-2008 David Yu [email protected]
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.


//package com.dyuproject.util;

import java.math.BigInteger;

/**
 
 * Diffie-Hellman key exchange is a cryptographic protocol that allows two parties 
 * that have no prior knowledge of each other to jointly establish a shared secret 
 * key over an insecure communications channel.
 
 @author David Yu
 * @created Sep 7, 2008
 */

public final class DiffieHellman
{
    
    public static final DiffieHellman BASE_2 = new DiffieHellman(BigInteger.valueOf(2));
    public static final DiffieHellman BASE_5 = new DiffieHellman(BigInteger.valueOf(5));
    
    private static final long __loadTime = System.currentTimeMillis();
    
    private final BigInteger _base;
    
    public DiffieHellman(BigInteger base)
    {
        _base = base;
    }
    
    /**
     * Generates a random private Key (element 0) and a random public key (element 1) 
     * from the given {@code modulus}.
     
     @param modulus
     @return BigInteger array.  Element 0 is privateKey.  Element 1 is publicKey.
     */
    public BigInteger[] generateRandomKeys(BigInteger modulus)
    {
        BigInteger privateKey = BigInteger.valueOf(System.currentTimeMillis() + __loadTime);
        return new BigInteger[]{privateKey, generatePublicKey(privateKey, modulus)};
    }
    
    /**
     * Generates a public key from the given {@code privateKey} and {@code modulus}.
     */
    public BigInteger generatePublicKey(BigInteger privateKey, BigInteger modulus)
    {
        return _base.modPow(privateKey, modulus);
    }
    
    /**
     * Gets/computes the shared secret key from the given {@code privateKey}, 
     * {@code modulus} and {@code responseKey} - which is a public key.
     */
    public static BigInteger getSharedSecretKey(BigInteger privateKey, BigInteger modulus, 
            BigInteger responseKey)
    {
        return responseKey.modPow(privateKey, modulus);
    }

}
/*

//Copyright 2007-2008 David Yu [email protected]
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.


package com.dyuproject.util;

import java.math.BigInteger;

import junit.framework.TestCase;

// * @author David Yu
// * @created Sep 8, 2008

public class DiffieHellmanTest extends TestCase
{
    
    public void testSharedSecret()
    {
        //from http://en.wikipedia.org/wiki/Diffie-Hellman
        BigInteger modulus = BigInteger.valueOf(23);
        
        BigInteger a_private = BigInteger.valueOf(6);
        BigInteger a_public = DiffieHellman.BASE_5.generatePublicKey(a_private, modulus);        
        
        BigInteger b_private = BigInteger.valueOf(15);
        BigInteger b_public = DiffieHellman.BASE_5.generatePublicKey(b_private, modulus);
        
        BigInteger a_shared_secret = DiffieHellman.getSharedSecretKey(a_private, modulus, b_public);
        
        BigInteger b_shared_secret = DiffieHellman.getSharedSecretKey(b_private, modulus, a_public);
        
        assertEquals(a_shared_secret, b_shared_secret);
        assertEquals(2, a_shared_secret.intValue());
    }

}

*/

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.Using the KeyGenerator class and showing how to create a SecretKeySpec from an encoded key
2.Key Generator Mac
3.Generate DSA key pair
4.KeyPair Generator For Private Key
5.KeyPair Generator For Public Key
6.Wrap And Unwrap Key
7.Generating a Public/Private Key Pair
8.Generate a 576-bit DH key pair
9.Generate a 1024-bit RSA key pair
10.Getting the Bytes of a Generated Key Pair
11.Get the bytes of the public and private keys
12.The bytes can be converted back to public and private key objects
13.Generate a key for the HMAC-SHA1 keyed-hashing algorithm
14.Asymmetric Key Maker
15.Generating a Symmetric Key
16.Xml dap Certs And Keys
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.