Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.jcajce.provider.asymmetric.dh;
  
  import java.security.Key;
 
 
Diffie-Hellman key agreement. There's actually a better way of doing this if you are using long term public keys, see the light-weight version for details.
 
 public class KeyAgreementSpi
     extends javax.crypto.KeyAgreementSpi
 {
     private BigInteger      x;
     private BigInteger      p;
     private BigInteger      g;
     private BigInteger      result;
 
     private static final Hashtable algorithms = new Hashtable();
 
     static
     {
         Integer i64 = Integers.valueOf(64);
         Integer i192 = Integers.valueOf(192);
         Integer i128 = Integers.valueOf(128);
         Integer i256 = Integers.valueOf(256);
 
         .put("DES"i64);
         .put("DESEDE"i192);
         .put("BLOWFISH"i128);
         .put("AES"i256);
     }
 
     private byte[] bigIntToBytes(
         BigInteger    r)
     {
         //
         // RFC 2631 (2.1.2) specifies that the secret should be padded with leading zeros if necessary
         // must be the same length as p
         //
         int expectedLength = (.bitLength() + 7) / 8;
 
         byte[]    tmp = r.toByteArray();
 
         if (tmp.length == expectedLength)
         {
             return tmp;
         }
 
         if (tmp[0] == 0 && tmp.length == expectedLength + 1)
         {
             byte[]    rv = new byte[tmp.length - 1];
             
             System.arraycopy(tmp, 1, rv, 0, rv.length);
             return rv;
         }
 
         // tmp must be shorter than expectedLength
         // pad to the left with zeros.
         byte[]    rv = new byte[expectedLength];
 
         System.arraycopy(tmp, 0, rvrv.length - tmp.lengthtmp.length);
 
         return rv;
     }
     
     protected Key engineDoPhase(
         Key     key,
         boolean lastPhase
         throws InvalidKeyExceptionIllegalStateException
     {
         if ( == null)
         {
             throw new IllegalStateException("Diffie-Hellman not initialised.");
         }
 
         if (!(key instanceof DHPublicKey))
         {
             throw new InvalidKeyException("DHKeyAgreement doPhase requires DHPublicKey");
         }
         DHPublicKey pubKey = (DHPublicKey)key;
 
         if (!pubKey.getParams().getG().equals() || !pubKey.getParams().getP().equals())
        {
            throw new InvalidKeyException("DHPublicKey not for this KeyAgreement!");
        }
        if (lastPhase)
        {
             = ((DHPublicKey)key).getY().modPow();
            return null;
        }
        else
        {
             = ((DHPublicKey)key).getY().modPow();
        }
        return new BCDHPublicKey(pubKey.getParams());
    }
    protected byte[] engineGenerateSecret() 
        throws IllegalStateException
    {
        if ( == null)
        {
            throw new IllegalStateException("Diffie-Hellman not initialised.");
        }
        return bigIntToBytes();
    }
    protected int engineGenerateSecret(
        byte[]  sharedSecret,
        int     offset
    {
        if ( == null)
        {
            throw new IllegalStateException("Diffie-Hellman not initialised.");
        }
        byte[]  secret = bigIntToBytes();
        if (sharedSecret.length - offset < secret.length)
        {
            throw new ShortBufferException("DHKeyAgreement - buffer too short");
        }
        System.arraycopy(secret, 0, sharedSecretoffsetsecret.length);
        return secret.length;
    }
    protected SecretKey engineGenerateSecret(
        String algorithm
    {
        if ( == null)
        {
            throw new IllegalStateException("Diffie-Hellman not initialised.");
        }
        String algKey = Strings.toUpperCase(algorithm);
        byte[] res = bigIntToBytes();
        if (.containsKey(algKey))
        {
            Integer length = (Integer).get(algKey);
            byte[] key = new byte[length.intValue() / 8];
            System.arraycopy(res, 0, key, 0, key.length);
            if (algKey.startsWith("DES"))
            {
                DESParameters.setOddParity(key);
            }
            
            return new SecretKeySpec(keyalgorithm);
        }
        return new SecretKeySpec(resalgorithm);
    }
    protected void engineInit(
        Key                     key,
        AlgorithmParameterSpec  params,
        SecureRandom            random
    {
        if (!(key instanceof DHPrivateKey))
        {
            throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
        }
        DHPrivateKey    privKey = (DHPrivateKey)key;
        if (params != null)
        {
            if (!(params instanceof DHParameterSpec))
            {
                throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
            }
            DHParameterSpec p = (DHParameterSpec)params;
            this. = p.getP();
            this. = p.getG();
        }
        else
        {
            this. = privKey.getParams().getP();
            this. = privKey.getParams().getG();
        }
        this. = this. = privKey.getX();
    }
    protected void engineInit(
        Key             key,
        SecureRandom    random
        throws InvalidKeyException
    {
        if (!(key instanceof DHPrivateKey))
        {
            throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey");
        }
        DHPrivateKey    privKey = (DHPrivateKey)key;
        this. = privKey.getParams().getP();
        this. = privKey.getParams().getG();
        this. = this. = privKey.getX();
    }
New to GrepCode? Check out our FAQ X