1 2 29 30 package com.jcraft.jsch.jce; 31 32 import java.math.BigInteger ; 33 import java.security.*; 34 import javax.crypto.*; 35 import javax.crypto.spec.*; 36 37 public class DH implements com.jcraft.jsch.DH{ 38 BigInteger p; 39 BigInteger g; 40 BigInteger e; byte[] e_array; 42 BigInteger f; BigInteger K; byte[] K_array; 45 46 private KeyPairGenerator myKpairGen; 47 private KeyAgreement myKeyAgree; 48 public void init() throws Exception { 49 myKpairGen=KeyPairGenerator.getInstance("DH"); 50 myKeyAgree=KeyAgreement.getInstance("DH"); 52 } 54 public byte[] getE() throws Exception { 55 if(e==null){ 56 DHParameterSpec dhSkipParamSpec=new DHParameterSpec(p, g); 57 myKpairGen.initialize(dhSkipParamSpec); 58 KeyPair myKpair=myKpairGen.generateKeyPair(); 59 myKeyAgree.init(myKpair.getPrivate()); 60 byte[] myPubKeyEnc=myKpair.getPublic().getEncoded(); 62 e=((javax.crypto.interfaces.DHPublicKey)(myKpair.getPublic())).getY(); 63 e_array=e.toByteArray(); 64 } 65 return e_array; 66 } 67 public byte[] getK() throws Exception { 68 if(K==null){ 69 KeyFactory myKeyFac=KeyFactory.getInstance("DH"); 70 DHPublicKeySpec keySpec=new DHPublicKeySpec(f, p, g); 71 PublicKey yourPubKey=myKeyFac.generatePublic(keySpec); 72 myKeyAgree.doPhase(yourPubKey, true); 73 byte[] mySharedSecret=myKeyAgree.generateSecret(); 74 K=new BigInteger (mySharedSecret); 75 K_array=K.toByteArray(); 76 77 81 K_array=mySharedSecret; 82 } 83 return K_array; 84 } 85 public void setP(byte[] p){ setP(new BigInteger (p)); } 86 public void setG(byte[] g){ setG(new BigInteger (g)); } 87 public void setF(byte[] f){ setF(new BigInteger (f)); } 88 void setP(BigInteger p){this.p=p;} 89 void setG(BigInteger g){this.g=g;} 90 void setF(BigInteger f){this.f=f;} 91 } 92 | Popular Tags |