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

org.shoulder.crypto.negotiation.cipher.DefaultTransportCipher Maven / Gradle / Ivy

Go to download

Shoulder 提供的 协商加密模块,用于非信任网络环境下的安全通信。基于 DH + ECC 实现先进的加密算法协商算法,比传统的 DH + DES 协商算法性能显著更高,更安全。

There is a newer version: 1.0.0-M2.2
Show newest version
package org.shoulder.crypto.negotiation.cipher;

import jakarta.annotation.Nonnull;
import org.shoulder.crypto.exception.CryptoErrorCodeEnum;
import org.shoulder.crypto.negotiation.dto.NegotiationResult;
import org.shoulder.crypto.negotiation.util.TransportCryptoUtil;
import org.shoulder.crypto.symmetric.exception.SymmetricCryptoException;

/**
 * 传输加解密,使用者使用
 *
 * @author lym
 */
public class DefaultTransportCipher implements TransportTextCipher {

    private final NegotiationResult negotiationResult;

    /**
     * 数据密钥明文
     */
    private final byte[] dk;


    private DefaultTransportCipher(NegotiationResult keyExchangeInfo, byte[] dk) {
        this.negotiationResult = keyExchangeInfo;
        this.dk = dk;
    }

    /**
     * 创建解密器
     *
     * @param keyExchangeInfo 密钥协商结果信息
     * @param dk              数据密钥明文
     * @return 解密器
     */
    public static DecryptCipher buildDecryptCipher(NegotiationResult keyExchangeInfo, byte[] dk) {
        return new DecryptCipher(keyExchangeInfo, dk);
    }

    /**
     * 创建加密器
     *
     * @param keyExchangeInfo 密钥协商结果信息
     * @return 加密器
     */
    public static EncryptCipher buildEncryptCipher(NegotiationResult keyExchangeInfo, byte[] dk) {
        return new EncryptCipher(keyExchangeInfo, dk);
    }


    /**
     * 加密
     *
     * @param toCipher 待加密明文
     * @return 密文
     */
    @Override
    public String encrypt(@Nonnull String toCipher) {
        try {
            return TransportCryptoUtil.encrypt(negotiationResult, dk, toCipher);
        } catch (SymmetricCryptoException e) {
            throw CryptoErrorCodeEnum.ENCRYPT_FAIL.toException(e);
        }
    }

    /**
     * 解密
     *
     * @param cipherText 对方加密过的密文
     * @return 明文
     */
    @Override
    public String decrypt(@Nonnull String cipherText) {
        try {
            return TransportCryptoUtil.decrypt(negotiationResult, dk, cipherText);
        } catch (SymmetricCryptoException e) {
            throw CryptoErrorCodeEnum.DECRYPT_FAIL.toException(e);
        }
    }

    /**
     * 加密或解密,根据实现类具体职责决定
     */
    @Override
    public String doCipher(String input) {
        throw new UnsupportedOperationException("not support!");
    }

    // ---------------------------------------------------------------------------------------------

    /**
     * 加密器
     */
    public static class EncryptCipher extends DefaultTransportCipher {

        private EncryptCipher(NegotiationResult keyExchangeInfo, byte[] dk) {
            super(keyExchangeInfo, dk);
        }

        @Override
        public String decrypt(@Nonnull String toCipher) {
            throw new UnsupportedOperationException("Can't decrypt with a encryptCipher!");
        }

        /**
         * 加密
         */
        @Override
        public String doCipher(String input) {
            return super.encrypt(input);
        }
    }


    /**
     * 解密器
     */
    public static class DecryptCipher extends DefaultTransportCipher {

        private DecryptCipher(NegotiationResult keyExchangeInfo, byte[] dk) {
            super(keyExchangeInfo, dk);
        }

        @Override
        public String encrypt(@Nonnull String toCipher) {
            throw new UnsupportedOperationException("Can't encrypt with a decryptCipher!");
        }

        /**
         * 加密
         */
        @Override
        public String doCipher(String input) {
            return super.decrypt(input);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy