encrypt (Document - Java)

Encrypts a document.

Defined in



public void encrypt()
    throws NotesException
public	void encrypt(UserID uid)
    throws NotesException
public	void encrypt(String idfile, String password)
    throws NotesException
Parameter Description
Userid UserID (type). After setting the User id, documents in this database will be decrypted with encryption keys of this user id
idfile String. Provides the file path of id file. After setting it, all documents in this database will be decrypted with encryption keys of this id file.
password String. After setting the User id, documents in this database will be decrypted with encryption keys of this user id
Possible exception Description
Document encryption failed (4167) The operation failed to encrypt this document


General usage

The encrypted document is not saved until you call save. Only the items for which isEncrypted is true are encrypted. Items for which isEncrypted is false remain visible to any user, even if the user does not have the proper encryption key.

If the EncryptionKeys property is set with one or more named keys, those keys are used to encrypt the document. Any user who has one of the encryption keys can decrypt the document. If there are no encryption keys specified, the document is encrypted with the user's public key, in which case only the user who encrypted the document can decrypt it.

If the program is running on a server, it must have permission to use Encrypt.

Since mail encryption works differently, do not use this method if you want to mail an encrypted document. Instead, set the EncryptOnSend property to true, and use the send method.

You cannot use the encrypt method on a Document object returned by getDocumentContext.

Usage with XPages

This method can also be used for encrypting XPages using the Userid, idfile, and password parameters. To encrypt a document with a private key on web or server use the following general code steps:
  • Fetch the IDVault object
  • Fetch a UserID object out of the IDVault for a particular user with the getUserIDFile method.
  • Get a list of private key names that are available to use with the UserID with the getEncryptionKeys method.
  • Specify which key to use to encrypt the doc with the setEncryptionKeys method.
  • Use the encrypt method to encrypt note with the specified encryption key set with setEncryptionKeys


This code provides an example of how to use this method with the IDVault:

public class JavaAgent extends AgentBase {

    public void NotesMain() {

      try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();

          // (Your code goes here)
          Database db = session.getDatabase("TEST", "test1.nsf");         
         Document doc = db.createDocument();
         Item item = doc.appendItemValue("test", "value");
         doc.save(true, false);

         IDVault id = session.getIDVault();
         UserID uid = id.getUserID("test vault1/IBM", "12345", "TEST/IBM");

      } catch(Exception e) {