package com.project.zcustom.tools;

import org.jasypt.encryption.StringEncryptor;

/**
 * 自定义加密工具
 * 使用国密4
 */
public class MyStringEncryptor implements StringEncryptor {
    /**
     * 默认加密key
     */
    private static final String PROP_ENCRYPT_KEY = "e29c3ccb44d24b4a9701b03c2b9e9281";

    private String enccryptKey;

    public String getEnccryptKey() {
        return enccryptKey;
    }

    public void setEnccryptKey(String enccryptKey) {
        this.enccryptKey = enccryptKey;
    }

    public boolean hasEnccryptKey() {
        return !(null == enccryptKey || enccryptKey.trim().length() == 0);
    }

    public MyStringEncryptor() {
        this.enccryptKey = PROP_ENCRYPT_KEY;
    }

    public MyStringEncryptor(String enccryptKey) {
        this.enccryptKey = enccryptKey;
    }

    /**
     * 加密
     * @param message   明文
     * @return          密文
     */
    @Override
    public String encrypt(String message) {
        return EncryptionUtil.getSymmetricalEncryptionData(enccryptKey, message);
    }

    /**
     * 解密
     * @param encryptedMessage  密文
     * @return                  明文
     */
    @Override
    public String decrypt(String encryptedMessage) {
        return EncryptionUtil.getSymmetricalDecryptionData(enccryptKey, encryptedMessage);
    }
}
