> For the complete documentation index, see [llms.txt](https://bayun.gitbook.io/bayuncoresdk-android/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bayun.gitbook.io/bayuncoresdk-android/5.-bayuncoresdk-methods/5.2-text-locking-unlocking.md).

# 6.2 Lock/Unlock Text

### 6.2.1 Lock Text

The `lockText` method of `BayunCore` class locks text with default `encryption-policy` and `key-generation-policy` dictated by server settings.

* **Parameters**
  * **text** : Text to be locked.
  * **success** : Success block to be executed after text is successfully locked, returns locked text through key "lockedText".
  * **failure** : Failure block to be executed if locking fails, returns `BayunError`.

{% tabs %}
{% tab title="Java" %}

```java
String plaintext = "<plaintext>";

// Callbacks for locking text
Handler.Callback success = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        String lockedText = message.getData().getString("lockedText", "");
        return false;
    }
}

Handler.Callback failure = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        String error = message.getData().getString("BayunError", "");
        Log.d(TAG, "Error locking text.");
        return false;
    }
};

bayunCore.lockText(plaintext, success, failure);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val plaintext = "<plainText>"

// Callbacks for locking text
val success = Handler.Callback {
    val lockedText = it.data.getString("lockedText", "")
    false
}

val failure = Handler.Callback {
    val error = it.data.getString("BayunError", "")
    Log.d(TAG, "Error locking text.")
    false
}

bayunCore.lockText(plaintext, success, failure)
```

{% endtab %}
{% endtabs %}

### 6.2.2 Lock Text with Encryption Policy, Key Generation Policy

The `lockText` method with encryption policy, key generation policy as parameters locks text with the encryption key dictated by the policy.

* **Parameters**
  * **text** : Text to be locked.
  * **encryptionPolicy** : [BayunEncryptionPolicy](https://docs.bayunsystems.com/bayuncoresdk-android/5.-bayuncoresdk-methods#encryption-policy) determines the key for locking.
  * **keyGenerationPolicy** : [BayunKeyGenerationPolicy](https://docs.bayunsystems.com/bayuncoresdk-android/5.-bayuncoresdk-methods#key-generation-policy) determines the policy to generate the data encryption key.
  * **groupId** : GroupId is required if encryptionPolicy is `BayunEncryptionPolicyGroup`.
  * **success** : Success block to be executed after text is successfully locked, returns locked text through key "lockedText".
  * **failure** : Failure block to be executed if locking fails, returns `BayunError`.

{% tabs %}
{% tab title="Java" %}

```java
String plaintext = "<plainText>";
int encryptionPolicy = BayunCore.ENCRYPTION_POLICY_COMPANY;
int keyGenerationPolicy = BayunCore.KEY_GENERATION_POLICY_ENVELOPE;
String groupId = "<groupId>";

// Callbacks for locking text
Handler.Callback success = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        String lockedText = message.getData().getString("lockedText", "");
        return false;
    }
}

Handler.Callback failure = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        String error = message.getData().getString("BayunError", "");
        Log.d(TAG, "Error locking text.");
        return false;
    }
};

bayunCore.lockText(plaintext, encryptionPolicy, keyGenerationPolicy, groupId, success, failure);

```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val plaintext = "<plainText>"
val encryptionPolicy = BayunCore.ENCRYPTION_POLICY_COMPANY
val keyGenerationPolicy = BayunCore.KEY_GENERATION_POLICY_ENVELOPE
val groupId = "<groupId>"

// Callbacks for locking text
val success = Handler.Callback {
    val lockedText = it.data.getString("lockedText", "")
    false
}

val failure = Handler.Callback {
    val error = it.data.getString("BayunError", "")
    Log.d(TAG, "Error locking text.")
    false
}

bayunCore.lockText(plaintext, encryptionPolicy, keyGenerationPolicy, groupId, success, failure)
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
If encryption-policy is other than `BayunEncryptionPolicyGroup` then groupId should be **null**.
{% endhint %}

### 6.2.3 Unlock Text

The `unlockText` method of `BayunCore` class unlocks text.

* **Parameters**
  * **text** : Text to be unlocked.
  * **success** : Success block to be executed after text is successfully unlocked, returns unlocked text through key "unlockedText".
  * **failure** : Failure block to be executed if unlocking fails, returns BayunError.

{% tabs %}
{% tab title="Java" %}

```java
String lockedText = "<lockedText>";

// Callbacks for unlocking text
Handler.Callback success = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        String unlockedText = message.getData().getString("unlockedText", "");
        return false;
    }
}

Handler.Callback failure = new Handler.Callback() {
    @Override
    public boolean handleMessage(Message message) {
        String error = message.getData().getString("BayunError", "");
        Log.d(TAG, "Error unlocking text.");
        return false;
    }
};

bayunCore.unlockText(lockedText, success, failure);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val lockedText = "<lockedText>"

// Callbacks for unlocking text
val success = Handler.Callback {
    val unlockedText = it.data.getString("unlockedText", "")
    false
}

val failure = Handler.Callback {
    val error = it.data.getString("BayunError", "")
    Log.d(TAG, "Error unlocking text.")
    false
}

bayunCore.unlockText(lockedText, success, failure)
```

{% endtab %}
{% endtabs %}
