DH.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch.jce;
00031 
00032 import java.math.BigInteger;
00033 import java.security.*;
00034 import javax.crypto.*;
00035 import javax.crypto.spec.*;
00036 
00037 public class DH implements com.jcraft.jsch.DH{
00038   BigInteger p;
00039   BigInteger g;
00040   BigInteger e;  // my public key
00041   byte[] e_array;
00042   BigInteger f;  // your public key
00043   BigInteger K;  // shared secret key
00044   byte[] K_array;
00045 
00046   private KeyPairGenerator myKpairGen;
00047   private KeyAgreement myKeyAgree;
00048   public void init() throws Exception{
00049     myKpairGen=KeyPairGenerator.getInstance("DH");
00050 //    myKpairGen=KeyPairGenerator.getInstance("DiffieHellman");
00051     myKeyAgree=KeyAgreement.getInstance("DH");
00052 //    myKeyAgree=KeyAgreement.getInstance("DiffieHellman");
00053   }
00054   public byte[] getE() throws Exception{
00055     if(e==null){
00056       DHParameterSpec dhSkipParamSpec=new DHParameterSpec(p, g);
00057       myKpairGen.initialize(dhSkipParamSpec);
00058       KeyPair myKpair=myKpairGen.generateKeyPair();
00059       myKeyAgree.init(myKpair.getPrivate());
00060 //    BigInteger x=((javax.crypto.interfaces.DHPrivateKey)(myKpair.getPrivate())).getX();
00061       byte[] myPubKeyEnc=myKpair.getPublic().getEncoded();
00062       e=((javax.crypto.interfaces.DHPublicKey)(myKpair.getPublic())).getY();
00063       e_array=e.toByteArray();
00064     }
00065     return e_array;
00066   }
00067   public byte[] getK() throws Exception{
00068     if(K==null){
00069       KeyFactory myKeyFac=KeyFactory.getInstance("DH");
00070       DHPublicKeySpec keySpec=new DHPublicKeySpec(f, p, g);
00071       PublicKey yourPubKey=myKeyFac.generatePublic(keySpec);
00072       myKeyAgree.doPhase(yourPubKey, true);
00073       byte[] mySharedSecret=myKeyAgree.generateSecret();
00074       K=new BigInteger(mySharedSecret);
00075       K_array=K.toByteArray();
00076 
00077 //System.err.println("K.signum(): "+K.signum()+
00078 //         " "+Integer.toHexString(mySharedSecret[0]&0xff)+
00079 //         " "+Integer.toHexString(K_array[0]&0xff));
00080 
00081       K_array=mySharedSecret;
00082     }
00083     return K_array;
00084   }
00085   public void setP(byte[] p){ setP(new BigInteger(p)); }
00086   public void setG(byte[] g){ setG(new BigInteger(g)); }
00087   public void setF(byte[] f){ setF(new BigInteger(f)); }
00088   void setP(BigInteger p){this.p=p;}
00089   void setG(BigInteger g){this.g=g;}
00090   void setF(BigInteger f){this.f=f;}
00091 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1