> For the complete documentation index, see [llms.txt](https://tech.x2bee.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tech.x2bee.com/dev-guide/developer-guide-en/dev-start/integrations/data-encryption.md).

# Data Encryption

The following describes the CryptoUtil data encryption definitions.

For encryption, the AwsCrypto package previously used in the project was removed along with the AwsCryptoUtil class file, and replaced with the CryptoUtil class file, which provides Spring's built-in **BCrypt hashing function** and **AES256 encode/decode** modules.

***

## How to Use Dependency Injection (DI)

Set the 32-character secret key used for AES256 in `crypto.secret.key` in the application.yml file.

If not set, it internally uses `X2BEE_Application_DATA_SecretKey` as the `defaultSecretKey` value. ⚠️ In production environments, you must redefine `crypto.secret.key` in application.yml (or in Jasypt/security settings) with a unique 32-character key for each deployment environment. Using the default key as-is risks exposing encrypted data.

**application.yml example:**

```yaml
crypto:
  secret:
    key: X2BEE_Application_DATA_SecretKey
```

Inject the Bean via the constructor in the class file where it will be used, then use the four functions `encodeBcrypt`, `matchesBcrypt`, `encodeAes`, and `decodeAes`.

**CryptoUtilTest1.java**

{% code title="CryptoUtilTest1.java" %}

```java
package com.x2bee.api.display;

import com.x2bee.common.base.util.CryptoUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.Assert;

@SpringBootTest
@AutoConfigureMockMvc
@Slf4j
class CryptoUtilTest1 {

    private CryptoUtil cryptoUtil;

    @Autowired
    public ApiTest(final CryptoUtil cryptoUtil) {
        Assert.notNull(cryptoUtil, "CryptoUtil can't be null");
        this.cryptoUtil = cryptoUtil;
    }

    @Test
    public void test1() throws Exception {
        log.info("------------------------------------- test1 START ----------------------------------------------------------");

        String value = cryptoUtil.encodeBcrypt("가나다");
        log.info(value);

        Boolean value2 = cryptoUtil.matchesBcrypt("가나다1", value);
        log.info("value2 : " + value2);

        Boolean value3 = cryptoUtil.matchesBcrypt("가나다", value);
        log.info("value3 : " + value3);

        String value4 = cryptoUtil.encodeAes("가나다");
        log.info(value4);

        String value5 = cryptoUtil.decodeAes(value4);
        log.info(value5);

        log.info("------------------------------------- test1 END ----------------------------------------------------------");
    }
}
```

{% endcode %}

***

## How to Use the Singleton Pattern

Obtain the singleton instance and use the functions the same way as above.

**CryptoUtilTest2.java**

{% code title="CryptoUtilTest2.java" %}

```java
package com.x2bee.api.display;

import com.x2bee.common.base.util.CryptoUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@AutoConfigureMockMvc
@Slf4j
class CryptoUtilTest2 {

    @Test
    public void test1() throws Exception {
        log.info("------------------------------------- test1 START ----------------------------------------------------------");

        CryptoUtil.getInstance().setSecretKey("X2BEE_Application_DATA_SecretKey");

        String value = CryptoUtil.getInstance().encodeBcrypt("가나다");
        log.info(value);

        Boolean value2 = CryptoUtil.getInstance().matchesBcrypt("가나다1", value);
        log.info("value2 : " + value2);

        Boolean value3 = CryptoUtil.getInstance().matchesBcrypt("가나다", value);
        log.info("value3 : " + value3);

        String value4 = CryptoUtil.getInstance().encodeAes("가나다");
        log.info(value4);

        String value5 = CryptoUtil.getInstance().decodeAes(value4);
        log.info(value5);

        log.info("------------------------------------- test1 END ----------------------------------------------------------");
    }
}
```

{% endcode %}

***

## Using the @Encrypt Custom Annotation

Using the `@Encrypt` custom annotation, AES256 encode/decode is automatically applied when MyBatis saves or queries data.

In the Common module's MyBatis AOP module, for fields with the `@Encrypt` annotation, the `encodeAes` function runs before saving to encode and store the value, and on query, after fetching from the DB, the `decodeAes` function runs to set the decoded value on the model.

**TestLog.java**

{% code title="TestLog.java" %}

```java
package com.x2bee.api.display.app.dto.sample;

import com.x2bee.common.base.encrypt.Encrypt;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.ibatis.type.Alias;

@Alias("testLog")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class TestLog {
    private Integer seq;
    private String log;

    @Encrypt
    private String testValue;
}
```

{% endcode %}

***

## Using the @Convert(converter = JpaEncryptor.class) Annotation

The `@Convert` annotation is a Convert module provided by JPA; set the Convert value to `JpaEncryptor.class`, which is in Common.

Just like with the MyBatis AOP above, saving and querying via JPA automatically applies the same AES256 encode/decode functions.

**TestLogEntity.java**

{% code title="TestLogEntity.java" %}

```java
package com.x2bee.api.display.app.entity;

import com.x2bee.api.display.app.dto.sample.TestLog;
import com.x2bee.common.base.encrypt.JpaEncryptor;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;

@Getter
@Setter
@Table(schema="public", name = "test_log")
@Entity
@NoArgsConstructor
@DynamicUpdate
public class TestLogEntity {

    @Id
    @Column(name = "seq", nullable = false)
    private Integer seq;

    @Column(name = "log", nullable = false)
    private String log;

    @Convert(converter = JpaEncryptor.class)
    @Column(name = "test_value", nullable = false)
    private String testValue;
}
```

{% endcode %}

***

## Usage Example (EncryptUtils)

Below is an example using EncryptUtils.

**Sample.java**

{% code title="Sample usage" %}

```java
/* Encrypted Value */
EncryptUtils.getEncryptValue("Sample");
// Expected result: MW5NMo6yro63GMEalqAI0A==

/* Decrypted Value */
EncryptUtils.getDecryptValue("MW5NMo6yro63GMEalqAI0A==");
// Expected result: Sample
```

{% endcode %}
