Package com.jcraft.jsch.bc

Source Code of com.jcraft.jsch.bc.DH

/* -*-mode:java; c-basic-offset:2; -*- */
/*
Copyright (c) 2004 ymnk, JCraft,Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the distribution.

  3. The names of the authors may not be used to endorse or promote products
     derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.jcraft.jsch.bc;

import java.math.BigInteger;
import java.security.SecureRandom;

import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.agreement.DHBasicAgreement;
import org.bouncycastle.crypto.generators.DHKeyPairGenerator;
import org.bouncycastle.crypto.params.DHKeyGenerationParameters;
import org.bouncycastle.crypto.params.DHParameters;
import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
import org.bouncycastle.crypto.params.DHPublicKeyParameters;

public class DH implements com.jcraft.jsch.DH{
  private BigInteger p;
  private BigInteger g;
  private BigInteger e;  // my public key
  private byte[] e_array;
  private BigInteger f;  // your public key
  private BigInteger K;  // shared secret key
  private byte[] K_array;

  private DHBasicAgreement myKeyAgree;
  private DHParameters param=null;

  public void init() throws Exception{
    myKeyAgree=new DHBasicAgreement();
  }
  public byte[] getE() throws Exception{
    if(e==null){
      DHKeyGenerationParameters params=
  new DHKeyGenerationParameters(new SecureRandom(),
              getParameter()
              );
      DHKeyPairGenerator kpgen=new DHKeyPairGenerator();
      kpgen.init(params);
      AsymmetricCipherKeyPair myKpair=kpgen.generateKeyPair();
      DHPublicKeyParameters pu=(DHPublicKeyParameters)myKpair.getPublic();
      DHPrivateKeyParameters pv=(DHPrivateKeyParameters)myKpair.getPrivate();

      myKeyAgree.init(pv);
      e=pu.getY();
      e_array=e.toByteArray();
    }
    return e_array;
  }
  public byte[] getK() throws Exception{
    if(K==null){
      DHPublicKeyParameters yourPubKey=
  new DHPublicKeyParameters(f,
          getParameter()
          );
      K=myKeyAgree.calculateAgreement(yourPubKey);
      K_array=K.toByteArray();
    }
    return K_array;
  }
  public void setP(byte[] p){ setP(new BigInteger(p)); }
  public void setG(byte[] g){ setG(new BigInteger(g)); }
  public void setF(byte[] f){ setF(new BigInteger(f)); }
  private void setP(BigInteger p){this.p=p;}
  private void setG(BigInteger g){this.g=g;}
  private void setF(BigInteger f){this.f=f;}
  private DHParameters getParameter(){
    if(param==null){
      param=new DHParameters(p, g);
    }
    return param;
  }
}
TOP

Related Classes of com.jcraft.jsch.bc.DH

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.