Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.jcajce.provider.asymmetric.ec;
  
  import java.security.Key;
 
 
 
 
 public class IESCipher
     extends CipherSpi
 {
     private final JcaJceHelper helper = new BCJcaJceHelper();
 
     private int ivLength;
     private IESEngine engine;
     private int state = -1;
     private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
     private AlgorithmParameters engineParam = null;
     private IESParameterSpec engineSpec = null;
     private AsymmetricKeyParameter key;
     private SecureRandom random;
     private boolean dhaesMode = false;
     private AsymmetricKeyParameter otherKeyParameter = null;
 
     public IESCipher(IESEngine engine)
     {
         this. = engine;
         this. = 0;
     }
 
     public IESCipher(IESEngine engineint ivLength)
     {
         this. = engine;
         this. = ivLength;
     }
 
     public int engineGetBlockSize()
     {
         if (.getCipher() != null)
         {
             return .getCipher().getBlockSize();
         }
         else
         {
             return 0;
         }
     }
 
 
     public int engineGetKeySize(Key key)
     {
         if (key instanceof ECKey)
         {
             return ((ECKey)key).getParameters().getCurve().getFieldSize();
         }
        else
        {
            throw new IllegalArgumentException("not an EC key");
        }
    }
    public byte[] engineGetIV()
    {
        return null;
    }
    {
        if ( == null &&  != null)
        {
            try
            {
                 = .createAlgorithmParameters("IES");
                .init();
            }
            catch (Exception e)
            {
                throw new RuntimeException(e.toString());
            }
        }
        return ;
    }
    public void engineSetMode(String mode)
        throws NoSuchAlgorithmException
    {
        String modeName = Strings.toUpperCase(mode);
        if (modeName.equals("NONE"))
        {
             = false;
        }
        else if (modeName.equals("DHAES"))
        {
             = true;
        }
        else
        {
            throw new IllegalArgumentException("can't support mode " + mode);
        }
    }
    public int engineGetOutputSize(int inputLen)
    {
        int len1len2len3;
        len1 = .getMac().getMacSize();
        if ( != null)
        {
            len2 = 1 + 2 * (((ECKey)).getParameters().getCurve().getFieldSize() + 7) / 8;
        }
        else
        {
            throw new IllegalStateException("cipher not initialised");
        }
        if (.getCipher() == null)
        {
            len3 = inputLen;
        }
        else if ( == . ||  == .)
        {
            len3 = .getCipher().getOutputSize(inputLen);
        }
        else if ( == . ||  == .)
        {
            len3 = .getCipher().getOutputSize(inputLen - len1 - len2);
        }
        else
        {
            throw new IllegalStateException("cipher not initialised");
        }
        if ( == . ||  == .)
        {
            return .size() + len1 + len2 + len3;
        }
        else if ( == . ||  == .)
        {
            return .size() - len1 - len2 + len3;
        }
        else
        {
            throw new IllegalStateException("cipher not initialised");
        }
    }
    public void engineSetPadding(String padding)
        throws NoSuchPaddingException
    {
        String paddingName = Strings.toUpperCase(padding);
        // TDOD: make this meaningful...
        if (paddingName.equals("NOPADDING"))
        {
        }
        else if (paddingName.equals("PKCS5PADDING") || paddingName.equals("PKCS7PADDING"))
        {
        }
        else
        {
            throw new NoSuchPaddingException("padding not available with IESCipher");
        }
    }
    // Initialisation methods
    public void engineInit(
        int opmode,
        Key key,
        AlgorithmParameters params,
        SecureRandom random)
    {
        AlgorithmParameterSpec paramSpec = null;
        if (params != null)
        {
            try
            {
                paramSpec = params.getParameterSpec(IESParameterSpec.class);
            }
            catch (Exception e)
            {
                throw new InvalidAlgorithmParameterException("cannot recognise parameters: " + e.toString());
            }
        }
         = params;
        engineInit(opmodekeyparamSpecrandom);
    }
    public void engineInit(
        int opmode,
        Key key,
        AlgorithmParameterSpec engineSpec,
        SecureRandom random)
    {
         = null;
        // Use default parameters (including cipher key size) if none are specified
        if (engineSpec == null)
        {
            this. = IESUtil.guessParameterSpec();
        }
        else if (engineSpec instanceof IESParameterSpec)
        {
            this. = (IESParameterSpec)engineSpec;
        }
        else
        {
            throw new InvalidAlgorithmParameterException("must be passed IES parameters");
        }
        byte[] nonce = this..getNonce();
        if (nonce != null)
        {
            if ( == 0)
            {
                throw new InvalidAlgorithmParameterException("NONCE present in IES Parameters when none required");
            }
            else if (nonce.length != )
            {
                throw new InvalidAlgorithmParameterException("NONCE in IES Parameters needs to be " +  + " bytes long");
            }
        }
        // Parse the recipient's key
        if (opmode == . || opmode == .)
        {
            if (key instanceof PublicKey)
            {
                this. = ECUtil.generatePublicKeyParameter((PublicKey)key);
            }
            else if (key instanceof IESKey)
            {
                IESKey ieKey = (IESKey)key;
                this. = ECUtil.generatePublicKeyParameter(ieKey.getPublic());
                this. = ECUtil.generatePrivateKeyParameter(ieKey.getPrivate());
            }
            else
            {
                throw new InvalidKeyException("must be passed recipient's public EC key for encryption");
            }
        }
        else if (opmode == . || opmode == .)
        {
            if (key instanceof PrivateKey)
            {
                this. = ECUtil.generatePrivateKeyParameter((PrivateKey)key);
            }
            else if (key instanceof IESKey)
            {
                IESKey ieKey = (IESKey)key;
                this. = ECUtil.generatePublicKeyParameter(ieKey.getPublic());
                this. = ECUtil.generatePrivateKeyParameter(ieKey.getPrivate());
            }
            else
            {
                throw new InvalidKeyException("must be passed recipient's private EC key for decryption");
            }
        }
        else
        {
            throw new InvalidKeyException("must be passed EC key");
        }
        this. = random;
        this. = opmode;
        .reset();
    }
    public void engineInit(
        int opmode,
        Key key,
        SecureRandom random)
        throws InvalidKeyException
    {
        try
        {
            engineInit(opmodekey, (AlgorithmParameterSpec)nullrandom);
        }
        catch (InvalidAlgorithmParameterException e)
        {
            throw new IllegalArgumentException("can't handle supplied parameter spec");
        }
    }
    // Update methods - buffer the input
    public byte[] engineUpdate(
        byte[] input,
        int inputOffset,
        int inputLen)
    {
        .write(inputinputOffsetinputLen);
        return null;
    }
    public int engineUpdate(
        byte[] input,
        int inputOffset,
        int inputLen,
        byte[] output,
        int outputOffset)
    {
        .write(inputinputOffsetinputLen);
        return 0;
    }
    // Finalisation methods
    public byte[] engineDoFinal(
        byte[] input,
        int inputOffset,
        int inputLen)
    {
        if (inputLen != 0)
        {
            .write(inputinputOffsetinputLen);
        }
        final byte[] in = .toByteArray();
        .reset();
        // Convert parameters for use in IESEngine
            .getEncodingV(),
            .getMacKeySize(),
            .getCipherKeySize());
        if (.getNonce() != null)
        {
            params = new ParametersWithIV(params.getNonce());
        }
        final ECDomainParameters ecParams = ((ECKeyParameters)).getParameters();
        final byte[] V;
        if ( != null)
        {
            try
            {
                if ( == . ||  == .)
                {
                    .init(trueparams);
                }
                else
                {
                    .init(falseparams);
                }
                return .processBlock(in, 0, in.length);
            }
            catch (Exception e)
            {
                throw new BadPaddingException(e.getMessage());
            }
        }
        if ( == . ||  == .)
        {
            // Generate the ephemeral key pair
            ECKeyPairGenerator gen = new ECKeyPairGenerator();
            gen.init(new ECKeyGenerationParameters(ecParams));
            final boolean usePointCompression = .getPointCompression();
            EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(gennew KeyEncoder()
            {
                public byte[] getEncoded(AsymmetricKeyParameter keyParameter)
                {
                    return ((ECPublicKeyParameters)keyParameter).getQ().getEncoded(usePointCompression);
                }
            });
            // Encrypt the buffer
            try
            {
                .init(paramskGen);
                return .processBlock(in, 0, in.length);
            }
            catch (Exception e)
            {
                throw new BadPaddingException(e.getMessage());
            }
        }
        else if ( == . ||  == .)
        {
            // Decrypt the buffer
            try
            {
                .init(paramsnew ECIESPublicKeyParser(ecParams));
                return .processBlock(in, 0, in.length);
            }
            catch (InvalidCipherTextException e)
            {
                throw new BadPaddingException(e.getMessage());
            }
        }
        else
        {
            throw new IllegalStateException("cipher not initialised");
        }
    }
    public int engineDoFinal(
        byte[] input,
        int inputOffset,
        int inputLength,
        byte[] output,
        int outputOffset)
    {
        byte[] buf = engineDoFinal(inputinputOffsetinputLength);
        System.arraycopy(buf, 0, outputoutputOffsetbuf.length);
        return buf.length;
    }

    
Classes that inherit from us
    static public class ECIES
        extends IESCipher
    {
        public ECIES()
        {
            super(new IESEngine(new ECDHBasicAgreement(),
                new KDF2BytesGenerator(new SHA1Digest()),
                new HMac(new SHA1Digest())));
        }
    }
    static public class ECIESwithDESede
        extends IESCipher
    {
        public ECIESwithDESede()
        {
            super(new IESEngine(new ECDHBasicAgreement(),
                new KDF2BytesGenerator(new SHA1Digest()),
                new HMac(new SHA1Digest()),
                new PaddedBufferedBlockCipher(new DESedeEngine())));
        }
    }
    static public class ECIESwithAES
        extends IESCipher
    {
        public ECIESwithAES()
        {
            super(new IESEngine(new ECDHBasicAgreement(),
                new KDF2BytesGenerator(new SHA1Digest()),
                new HMac(new SHA1Digest()),
                new PaddedBufferedBlockCipher(new AESEngine())));
        }
    }
    static public class ECIESwithDESedeCBC
        extends IESCipher
    {
        public ECIESwithDESedeCBC()
        {
            super(new IESEngine(new ECDHBasicAgreement(),
                new KDF2BytesGenerator(new SHA1Digest()),
                new HMac(new SHA1Digest()),
                new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()))), 8);
        }
    }
    static public class ECIESwithAESCBC
        extends IESCipher
    {
        public ECIESwithAESCBC()
        {
            super(new IESEngine(new ECDHBasicAgreement(),
                new KDF2BytesGenerator(new SHA1Digest()),
                new HMac(new SHA1Digest()),
                new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()))), 16);
        }
    }
New to GrepCode? Check out our FAQ X