All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.encrypt.DefaultEncryptManager Maven / Gradle / Ivy

package shz.encrypt;

import shz.Coder;
import shz.PropHelp;
import shz.ToList;
import shz.Validator;
import shz.msg.ClientFailure;

import java.text.Collator;
import java.util.*;

public abstract class DefaultEncryptManager implements EncryptManager {
    protected final RsaEncipher rsaEncipher;
    protected final byte[] privateKey;
    protected final byte[] publicKey;

    protected DefaultEncryptManager() {
        rsaEncipher = RsaEncipher.getInstance();
        SimpleKeyPair keyPair = getKeyPair();
        if (keyPair == null
                || Validator.isBlank(keyPair.privateKey)
                || Validator.isBlank(keyPair.publicKey)) {
            rsaEncipher.initKeyPair();
            privateKey = rsaEncipher.getKeyPair().getPrivate().getEncoded();
            publicKey = rsaEncipher.getKeyPair().getPublic().getEncoded();
            saveKeyPair(new SimpleKeyPair(encodeBytes(privateKey), encodeBytes(publicKey)));
        } else {
            privateKey = decodeString(keyPair.privateKey);
            publicKey = decodeString(keyPair.publicKey);
        }
    }

    protected SimpleKeyPair getKeyPair() {
        return null;
    }

    protected void saveKeyPair(SimpleKeyPair keyPair) {
    }

    public final void resetKeyPair() {
        rsaEncipher.initKeyPair();
        System.arraycopy(rsaEncipher.getKeyPair().getPrivate().getEncoded(), 0, privateKey, 0, privateKey.length);
        System.arraycopy(rsaEncipher.getKeyPair().getPublic().getEncoded(), 0, publicKey, 0, publicKey.length);
        saveKeyPair(new SimpleKeyPair(encodeBytes(privateKey), encodeBytes(publicKey)));
    }

    public final String getPublicKey() {
        return encodeBytes(publicKey);
    }

    public final String encryptKey(String key, String publicKey) {
        return encodeBytes(rsaEncipher.encryptByPublicKey(stringToBytes(key), decodeString(publicKey)));
    }

    protected final String decryptKey(String key) {
        return bytesToString(rsaEncipher.decryptByPrivateKey(decodeString(key), privateKey));
    }

    /**
     * 使用私钥对数据签名再次签名
     */
    public final String sign(String signature) {
        return encodeBytes(rsaEncipher.encryptByPrivateKey(stringToBytes(signature), privateKey));
    }

    /**
     * 默认的数据签名方法
     */
    @Override
    public String sign(EncryptParam encryptParam, Object data) {
        Map map = PropHelp.fieldValueMap(data);
        Set keys = map.keySet();
        List list = ToList.explicitCollect(keys.stream().filter(k -> !"null".equals(k) && !"sign".equals(k) && !"pl_sign".equals(k)), keys.size());
        list.sort(Collator.getInstance(Locale.CHINA));
        return Coder.md5(stringToBytes(String.join("", ToList.explicitCollect(list.stream().map(map::get), list.size())) + encryptParam.signKey));
    }

    @Override
    public final void checkSign(EncryptParam encryptParam, String signature, Object data) {
        if (Validator.nonBlank(encryptParam.publicKey))
            signature = bytesToString(rsaEncipher.decryptByPublicKey(decodeString(signature), decodeString(encryptParam.publicKey)));
        ClientFailure.INVALID_SIGNATURE.requireNon(!Objects.equals(sign(encryptParam, data), signature));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy