Skip to content
广告❤️成为赞助商

Field Encryption and Decryption

The @FieldEncrypt annotation provides field-level encryption and decryption, supporting multiple algorithms such as MD5, BASE64, AES, RSA, etc.

@FieldEncrypt

  • Data encryption and decryption annotation
  • algorithm(): Encryption type, default is Base64
  • findDecrypt(): Whether to decrypt when querying. If set to false, no decryption occurs on query
  • key(): Symmetric encryption key, takes priority over global configuration
  • privateKey(): Private key for asymmetric encryption, takes priority over global configuration
  • publicKey(): Public key for asymmetric encryption, takes priority over global configuration
  • encryptor(): Custom encryptor, must implement com.mongoplus.encryptor.Encryptor

AlgorithmEnum Encryption Types

AlgorithmDescription
MD5_3232-bit MD5 algorithm
MD5_1616-bit MD5 algorithm
BASE64Base64 encoding algorithm
AESAES symmetric algorithm
RSAAsymmetric encryption algorithm
SM2Chinese SM2 asymmetric encryption based on ECC
SM3Chinese SM3 message digest algorithm (like MD5)
SM4Chinese SM4 symmetric algorithm, WLAN standard block cipher
PBEWithMD5AndDESHybrid algorithm
PBEWithMD5AndTripleDESHybrid algorithm
PBEWithSHA1AndDESedeHybrid algorithm
PBEWithSHA1AndRC2_40Hybrid algorithm

Warning

Do not transmit QueryChainWrapper over RPC calls.

  1. MD5 is irreversible; both stored and queried values are ciphertext.
  2. SM2, SM3, SM4 require the BouncyCastle library.

Required dependency for SM2, SM3, SM4

xml
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk18on</artifactId>
    <version>1.78.1</version>
</dependency>

GenerateKeyUtil Key Generation Utility

java
public static void main(String[] args) {
    try {
        // Generate AES key
        String aesKey = GenerateKeyUtil.generateAESKey("123456");
        // Generate RSA key pair
        MutablePair<String, String> rsaKey = GenerateKeyUtil.generateRSAKey();
        // Generate SM2 key pair
        MutablePair<String, String> sm2Key = GenerateKeyUtil.generateSM2Key();
        // Generate SM4 key
        String sm4Key = GenerateKeyUtil.generateSM4Key();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

Examples

Global Configuration

yml
mongo-plus:
    encryptor:
        key: XXXXXXXXXXXX
        public-key: XXXXXXXXXXXXXXXXXXXXXXX
        private-key: XXXXXXXXXXXXXXXXXXXXXXX

Entity Class Encryption

java
public class User {
    @FieldEncrypt(key = "", publicKey = "", privateKey = "")
    private String userName;
}

Custom Encryptor

java
public class CustomEncryptor implements Encryptor {
    @Override
    public String encrypt(String data, String key, String publicKey) throws Exception {
        return data;
    }

    @Override
    public String decrypt(String data, String key, String privateKey) throws Exception {
        return data;
    }
}

// Entity class usage
public class User {
    @FieldEncrypt(key = "", publicKey = "", privateKey = "", encryptor = CustomEncryptor.class)
    private String userName;
}