加密(X,Y坐标sm2+hex加解密)

导包:

        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.68</version>
        </dependency>

SM2国密算法:

public class SM2Digest {

    /**
     * 执行标准
     */
    private static String SM2_CURVE_NAME = "sm2p256v1";

    public static MessageDigest GETSM2ENCRYPTION() throws Exception {
        BouncyCastleProvider provider = new BouncyCastleProvider();
        // 获取椭圆曲线相关生成参数规格
        ECGenParameterSpec genParameterSpec = new ECGenParameterSpec("sm2p256v1");
        // 获取一个椭圆曲线类型的**对生成器
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", provider);
        // 使用SM2的算法区域初始化**生成器
        keyPairGenerator.initialize(genParameterSpec, new SecureRandom());
        // 生成**对
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        // 生成的私钥
        BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate();
        // 生成的公钥
        BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic();
        // 拿到32字节的私钥HEX
        System.out.println("privateKEY:" + privateKey);
        System.out.println("Private Key: " + privateKey.getD().toString(16));
        // true  代表压缩**,以02、03开头,长度为33字节
        // false 代表未压缩,以04开头,长度为65字节
        System.out.println("publicKey:" + publicKey);
        System.out.println("Public Key: " + Hex.encode(publicKey.getQ().getEncoded(true)));
        System.out.println("--------------------------生成现有公私钥-------------------------------------");
        return null;

    }


//    public static void main(String[] args) throws Exception {
//        //公钥坐标
//        String x = "ed771635c8344a3f0f715e02fcd44a0f4a771611c07bbfd941e9d8de2b58a35d";
//        String y = "a57a2eb3ef4aca94dab8e9d4237f63f87e5043ba78847cb175e5ed3a8f26c6d9";
//
//        String info = "测试公私钥匙加密解密";
//        byte[] data = info.getBytes();
//
//        SM2Digest sm2 = new SM2Digest();
//        ECDomainParameters domain = sm2.getDomain();
//        SM2Engine engine = sm2.getSM2Engine();
//        //公钥加密
//        CipherParameters pubKeyParameters = sm2.getPubKey(x, y, domain);
//        engine.init(true, pubKeyParameters);
//        byte[] encData = engine.processBlock(data, 0, data.length);
//        System.out.println("加密后内容:" + Hex.toHexString(encData));
        //私钥解密
        CipherParameters PrivateKey = sm2.getPrvKey(prvKey, domain);
        engine.init(false, PrivateKey);
        byte[] decData = engine.processBlock(encData, 0, encData.length);
        System.out.println("解密后内容:" + new String(decData));
//    }

    /**
     * 加密密码
     *
     * @param pwd
     * @param X
     * @param Y
     * @return
     */
    public static byte[] encryptPwd(String pwd, String X, String Y) {
        byte[] r = null;
        try {
            byte[] data = pwd.getBytes();

            SM2Digest sm2 = new SM2Digest();
            ECDomainParameters domain = sm2.getDomain();
            SM2Engine engine = sm2.getSM2Engine();
            //公钥加密
            CipherParameters pubKeyParameters = sm2.getPubKey(X, Y, domain);
            engine.init(true, pubKeyParameters);
            r = engine.processBlock(data, 0, data.length);
        } catch (Exception e) {

        }
        return r;
    }


    /**
     * 构造私钥对象
     *
     * @param prvKey
     * @return
     */
    public static CipherParameters getPrvKey(String prvKey, ECDomainParameters domain) {
        BigInteger prvKeyData = BigIntegers.fromUnsignedByteArray(Hex.decode(prvKey));
        ECPrivateKeyParameters PrivateKey = new ECPrivateKeyParameters(prvKeyData, domain);
        return PrivateKey;
    }

    /**
     * 构造公钥对象
     *
     * @param x
     * @param y
     * @param domain
     * @return
     */
    public static CipherParameters getPubKey(String x, String y, ECDomainParameters domain) {
        ECCurve curve = domain.getCurve();
        ECPoint point = curve.createPoint(BigIntegers.fromUnsignedByteArray(Hex.decode(x)), BigIntegers.fromUnsignedByteArray(Hex.decode(y)));
        ECPublicKeyParameters PublicKey = new ECPublicKeyParameters(point, domain);
        CipherParameters pubKeyParameters = new ParametersWithRandom(PublicKey);
        return pubKeyParameters;
    }

    public SM2Engine getSM2Engine() {
        Digest digest = new SM3Digest();
        SM2Engine engine = new SM2Engine(digest, SM2Engine.Mode.C1C3C2);
        return engine;
    }

    public ECDomainParameters getDomain() {
        X9ECParameters x9ECParameters = GMNamedCurves.getByName(SM2_CURVE_NAME);
        ECDomainParameters domain = new ECDomainParameters(
                x9ECParameters.getCurve(),
                x9ECParameters.getG(),
                x9ECParameters.getN(),
                x9ECParameters.getH()
        );
        return domain;
    }

你可能感兴趣的:(java,数据库,开发语言)