protected int engineDoFinal(
      byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) {
    if (inputLen != 0) {
      cipher.processBytes(input, inputOffset, inputLen, output, outputOffset);
    }

    cipher.reset();

    return inputLen;
  }
  protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) {
    if (inputLen != 0) {
      byte[] out = engineUpdate(input, inputOffset, inputLen);

      cipher.reset();

      return out;
    }

    cipher.reset();

    return new byte[0];
  }
  protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) {
    byte[] out = new byte[inputLen];

    cipher.processBytes(input, inputOffset, inputLen, out, 0);

    return out;
  }
Beispiel #4
0
  @Test
  public void testRc4Drop() {
    String security = "RbotCX+Rseavyum82S6p8t1a6MvD5XTupx6bqUR+zck=";
    byte[] securityBytes = BaseEncoding.base64().decode(security);

    String data = "OkSiMbE40ve1t6t6jZXyDugJz3Q=";
    byte[] dataBytes = data.getBytes(Charsets.UTF_8);

    byte[] encryptedDataBytes = new byte[dataBytes.length];
    StreamCipher rc4Drop = Rc4Utils.createRC4DropCipher(securityBytes, 1024);
    rc4Drop.processBytes(dataBytes, 0, dataBytes.length, encryptedDataBytes, 0);

    Assert.assertEquals(
        "AIw2+eGPJBQt5j/P+754Z8Q6M/e4RGoGTYhEGQ==",
        BaseEncoding.base64().encode(encryptedDataBytes));
  }
  protected int engineUpdate(
      byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
      throws ShortBufferException {
    try {
      cipher.processBytes(input, inputOffset, inputLen, output, outputOffset);

      return inputLen;
    } catch (DataLengthException e) {
      throw new ShortBufferException(e.getMessage());
    }
  }
  /**
   * Writes the specified byte to this output stream.
   *
   * @param b the <code>byte</code>.
   * @exception java.io.IOException if an I/O error occurs.
   */
  public void write(int b) throws IOException {
    oneByte[0] = (byte) b;

    if (bufferedBlockCipher != null) {
      int len = bufferedBlockCipher.processBytes(oneByte, 0, 1, buf, 0);

      if (len != 0) {
        out.write(buf, 0, len);
      }
    } else {
      out.write(streamCipher.returnByte((byte) b));
    }
  }
  /**
   * Writes <code>len</code> bytes from the specified byte array starting at offset <code>off</code>
   * to this output stream.
   *
   * @param b the data.
   * @param off the start offset in the data.
   * @param len the number of bytes to write.
   * @exception java.io.IOException if an I/O error occurs.
   */
  public void write(byte[] b, int off, int len) throws IOException {
    if (bufferedBlockCipher != null) {
      byte[] buf = new byte[bufferedBlockCipher.getOutputSize(len)];

      int outLen = bufferedBlockCipher.processBytes(b, off, len, buf, 0);

      if (outLen != 0) {
        out.write(buf, 0, outLen);
      }
    } else {
      byte[] buf = new byte[len];

      streamCipher.processBytes(b, off, len, buf, 0);

      out.write(buf, 0, len);
    }
  }
  protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random)
      throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;

    this.pbeSpec = null;
    this.pbeAlgorithm = null;

    this.engineParams = null;

    //
    // basic key check
    //
    if (!(key instanceof SecretKey)) {
      throw new InvalidKeyException(
          "Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
    }

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

      if (k.getOID() != null) {
        pbeAlgorithm = k.getOID().getId();
      } else {
        pbeAlgorithm = k.getAlgorithm();
      }

      if (k.getParam() != null) {
        param = k.getParam();
        pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
      } else if (params instanceof PBEParameterSpec) {
        param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName());
        pbeSpec = (PBEParameterSpec) params;
      } else {
        throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
      }

      if (k.getIvSize() != 0) {
        ivParam = (ParametersWithIV) param;
      }
    } else if (params == null) {
      param = new KeyParameter(key.getEncoded());
    } else if (params instanceof IvParameterSpec) {
      param =
          new ParametersWithIV(
              new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
      ivParam = (ParametersWithIV) param;
    } else {
      throw new IllegalArgumentException("unknown parameter type.");
    }

    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
      SecureRandom ivRandom = random;

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

      if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
        byte[] iv = new byte[ivLength];

        ivRandom.nextBytes(iv);
        param = new ParametersWithIV(param, iv);
        ivParam = (ParametersWithIV) param;
      } else {
        throw new InvalidAlgorithmParameterException("no IV set when one expected");
      }
    }

    switch (opmode) {
      case Cipher.ENCRYPT_MODE:
      case Cipher.WRAP_MODE:
        cipher.init(true, param);
        break;
      case Cipher.DECRYPT_MODE:
      case Cipher.UNWRAP_MODE:
        cipher.init(false, param);
        break;
      default:
        System.out.println("eeek!");
    }
  }