/**
   * Creates new encrypted credentials
   *
   * <p>Encrypts the message '<code>login</code>:<code>password</code>' using the public key <code>
   * pubKey</code> and <code>cipher</code> and store it in a new Credentials object.
   *
   * @see KeyPairUtil#encrypt(String, String, String, byte[])
   * @param login the login to encrypt
   * @param password the corresponding password to encrypt
   * @param pubKey public key used for encryption
   * @param cipher cipher parameters: combination of transformations
   * @return the Credentials object containing the encrypted data
   * @throws KeyException key generation or encryption failed
   */
  @Deprecated
  public static Credentials createCredentials(
      String login, String password, byte[] datakey, PublicKey pubKey, String cipher)
      throws KeyException {

    CredData cc = new CredData();
    cc.setLogin(CredData.parseLogin(login));
    cc.setDomain(CredData.parseDomain(login));
    cc.setPassword(password);
    cc.setKey(datakey);

    // serialize clear credentials to byte array
    byte[] clearCred = null;
    try {
      clearCred = ObjectToByteConverter.ObjectStream.convert(cc);
    } catch (IOException e1) {
      throw new KeyException(e1.getMessage());
    }

    int size = -1;
    if (pubKey instanceof java.security.interfaces.RSAPublicKey) {
      size = ((RSAPublicKey) pubKey).getModulus().bitLength();
    } else if (pubKey instanceof java.security.interfaces.DSAPublicKey) {
      size = ((DSAPublicKey) pubKey).getParams().getP().bitLength();
    } else if (pubKey instanceof javax.crypto.interfaces.DHPublicKey) {
      size = ((DHPublicKey) pubKey).getParams().getP().bitLength();
    }

    // generate symmetric key
    SecretKey aesKey = KeyUtil.generateKey(AES_ALGO, AES_KEYSIZE);

    byte[] encData = null;
    byte[] encAes = null;

    // encrypt AES key with public RSA key
    try {
      encAes = KeyPairUtil.encrypt(pubKey, size, cipher, aesKey.getEncoded());
    } catch (KeyException e) {
      throw new KeyException("Symmetric key encryption failed", e);
    }

    // encrypt clear credentials with AES key
    try {
      encData = KeyUtil.encrypt(aesKey, AES_CIPHER, clearCred);
    } catch (KeyException e) {
      throw new KeyException("Message encryption failed", e);
    }

    Credentials cred = new Credentials(pubKey.getAlgorithm(), size, cipher, encAes, encData);
    return cred;
  }