Java Code Examples for javax.crypto.Cipher#UNWRAP_MODE

The following examples show how to use javax.crypto.Cipher#UNWRAP_MODE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JCEIESCipher.java    From BiglyBT with GNU General Public License v2.0 6 votes vote down vote up
protected int engineGetOutputSize(
    int     inputLen)
{
    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        return buffer.size() + inputLen + 20; /* SHA1 MAC size */
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        return buffer.size() + inputLen - 20;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }
}
 
Example 2
Source File: JCEIESCipher.java    From TorrentEngine with GNU General Public License v3.0 6 votes vote down vote up
protected int engineGetOutputSize(
    int     inputLen) 
{
    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        return buffer.size() + inputLen + 20; /* SHA1 MAC size */
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        return buffer.size() + inputLen - 20;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }
}
 
Example 3
Source File: CipherSpi.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
protected int engineGetOutputSize(
    int     inputLen) 
{
    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        return buffer.size() + inputLen + 20; /* SHA1 MAC size */
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        return buffer.size() + inputLen - 20;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }
}
 
Example 4
Source File: CipherSpi.java    From ripple-lib-java with ISC License 6 votes vote down vote up
protected int engineGetOutputSize(
    int     inputLen) 
{
    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        return buffer.size() + inputLen + 20; /* SHA1 MAC size */
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        return buffer.size() + inputLen - 20;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }
}
 
Example 5
Source File: JCEIESCipher.java    From BiglyBT with GNU General Public License v2.0 4 votes vote down vote up
public void engineInit(
      int                     opmode,
      Key                     key,
      AlgorithmParameterSpec  params,
      SecureRandom            random)
  throws InvalidKeyException, InvalidAlgorithmParameterException
  {
      if (!(key instanceof IESKey))
      {
          throw new InvalidKeyException("must be passed IE key");
      }

      if (params == null && (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE))
      {
          //
          // if nothing is specified we set up for a 128 bit mac, with
          // 128 bit derivation vectors.
          //
          byte[]  d = new byte[16];
          byte[]  e = new byte[16];

          if (random == null)
          {
              random = new SecureRandom();
          }

          random.nextBytes(d);
          random.nextBytes(e);

          params = new IESParameterSpec(d, e, 128);
      }
      else if (!(params instanceof IESParameterSpec))
      {
          throw new InvalidAlgorithmParameterException("must be passed IES parameters");
      }

      IESKey       ieKey = (IESKey)key;

      CipherParameters pubKey;
      CipherParameters privKey;

      if (ieKey.getPublic() instanceof JCEECPublicKey)
      {
          pubKey = ECUtil.generatePublicKeyParameter(ieKey.getPublic());
          privKey = ECUtil.generatePrivateKeyParameter(ieKey.getPrivate());
      }
      else
      {
          pubKey = DHUtil.generatePublicKeyParameter(ieKey.getPublic());
          privKey = DHUtil.generatePrivateKeyParameter(ieKey.getPrivate());
      }

      this.engineParams = (IESParameterSpec)params;

      IESParameters       p = new IESParameters(engineParams.getDerivationV(), engineParams.getEncodingV(), engineParams.getMacKeySize());

      this.state = opmode;

      buffer.reset();

      switch (opmode)
      {
      case Cipher.ENCRYPT_MODE:
case Cipher.WRAP_MODE:
          cipher.init(true, privKey, pubKey, p);
          break;
      case Cipher.DECRYPT_MODE:
case Cipher.UNWRAP_MODE:
          cipher.init(false, privKey, pubKey, p);
          break;
      default:
          System.out.println("eeek!");
      }
  }
 
Example 6
Source File: BaseWrapCipher.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
protected void engineInit(
    int                     opmode,
    Key                     key,
    AlgorithmParameterSpec  params,
    SecureRandom            random)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
    CipherParameters        param;

    if (key instanceof BCPBEKey)
    {
        BCPBEKey k = (BCPBEKey)key;

        if (params instanceof PBEParameterSpec)
        {
            param = PBE.Util.makePBEParameters(k, params, wrapEngine.getAlgorithmName());
        }
        else if (k.getParam() != null)
        {
            param = k.getParam();
        }
        else
        {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    }
    else
    {
        param = new KeyParameter(key.getEncoded());
    }

    if (params instanceof IvParameterSpec)
    {
        IvParameterSpec iv = (IvParameterSpec) params;
        param = new ParametersWithIV(param, iv.getIV());
    }

    if (param instanceof KeyParameter && ivSize != 0)
    {
        iv = new byte[ivSize];
        random.nextBytes(iv);
        param = new ParametersWithIV(param, iv);
    }

    if (random != null)
    {
        param = new ParametersWithRandom(param, random);
    }

    switch (opmode)
    {
    case Cipher.WRAP_MODE:
        wrapEngine.init(true, param);
        break;
    case Cipher.UNWRAP_MODE:
        wrapEngine.init(false, param);
        break;
    case Cipher.ENCRYPT_MODE:
    case Cipher.DECRYPT_MODE:
        throw new IllegalArgumentException("engine only valid for wrapping");
    default:
        System.out.println("eeek!");
    }
}
 
Example 7
Source File: IESCipher.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
public int engineGetOutputSize(int inputLen)
{
    int len1, len2, len3;

    len1 = engine.getMac().getMacSize();

    if (key != null)
    {
        len2 = ((DHKey)key).getParams().getP().bitLength() / 8 + 1;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (engine.getCipher() == null)
    {
        len3 = inputLen;
    }
    else if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen);
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen - len1 - len2);
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        return buffer.size() + len1 + len2 + len3;
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        return buffer.size() - len1 - len2 + len3;
    }
    else
    {
        throw new IllegalStateException("IESCipher not initialised");
    }

}
 
Example 8
Source File: IESCipher.java    From RipplePower with Apache License 2.0 4 votes vote down vote up
public int engineGetOutputSize(int inputLen)
{
    int len1, len2, len3;

    len1 = engine.getMac().getMacSize();

    if (key != null)
    {
        len2 = 1 + 2 * (((ECKey)key).getParameters().getCurve().getFieldSize() + 7) / 8;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (engine.getCipher() == null)
    {
        len3 = inputLen;
    }
    else if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen);
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen - len1 - len2);
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        return buffer.size() + len1 + len2 + len3;
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        return buffer.size() - len1 - len2 + len3;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

}
 
Example 9
Source File: BaseWrapCipher.java    From ripple-lib-java with ISC License 4 votes vote down vote up
protected void engineInit(
    int                     opmode,
    Key                     key,
    AlgorithmParameterSpec  params,
    SecureRandom            random)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
    CipherParameters        param;

    if (key instanceof BCPBEKey)
    {
        BCPBEKey k = (BCPBEKey)key;

        if (params instanceof PBEParameterSpec)
        {
            param = PBE.Util.makePBEParameters(k, params, wrapEngine.getAlgorithmName());
        }
        else if (k.getParam() != null)
        {
            param = k.getParam();
        }
        else
        {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    }
    else
    {
        param = new KeyParameter(key.getEncoded());
    }

    if (params instanceof IvParameterSpec)
    {
        IvParameterSpec iv = (IvParameterSpec) params;
        param = new ParametersWithIV(param, iv.getIV());
    }

    if (param instanceof KeyParameter && ivSize != 0)
    {
        iv = new byte[ivSize];
        random.nextBytes(iv);
        param = new ParametersWithIV(param, iv);
    }

    if (random != null)
    {
        param = new ParametersWithRandom(param, random);
    }

    switch (opmode)
    {
    case Cipher.WRAP_MODE:
        wrapEngine.init(true, param);
        break;
    case Cipher.UNWRAP_MODE:
        wrapEngine.init(false, param);
        break;
    case Cipher.ENCRYPT_MODE:
    case Cipher.DECRYPT_MODE:
        throw new IllegalArgumentException("engine only valid for wrapping");
    default:
        System.out.println("eeek!");
    }
}
 
Example 10
Source File: IESCipher.java    From ripple-lib-java with ISC License 4 votes vote down vote up
public int engineGetOutputSize(int inputLen)
{
    int len1, len2, len3;

    len1 = engine.getMac().getMacSize();

    if (key != null)
    {
        len2 = ((DHKey)key).getParams().getP().bitLength() / 8 + 1;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (engine.getCipher() == null)
    {
        len3 = inputLen;
    }
    else if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen);
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen - len1 - len2);
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        return buffer.size() + len1 + len2 + len3;
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        return buffer.size() - len1 - len2 + len3;
    }
    else
    {
        throw new IllegalStateException("IESCipher not initialised");
    }

}
 
Example 11
Source File: IESCipher.java    From ripple-lib-java with ISC License 4 votes vote down vote up
public int engineGetOutputSize(int inputLen)
{
    int len1, len2, len3;

    len1 = engine.getMac().getMacSize();

    if (key != null)
    {
        len2 = 1 + 2 * (((ECKey)key).getParameters().getCurve().getFieldSize() + 7) / 8;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (engine.getCipher() == null)
    {
        len3 = inputLen;
    }
    else if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen);
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        len3 = engine.getCipher().getOutputSize(inputLen - len1 - len2);
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE)
    {
        return buffer.size() + len1 + len2 + len3;
    }
    else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE)
    {
        return buffer.size() - len1 - len2 + len3;
    }
    else
    {
        throw new IllegalStateException("cipher not initialised");
    }

}