whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/encryption/SM2.java
@@ -3,6 +3,7 @@
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.math.BigInteger;
@@ -34,6 +35,14 @@
    private static ECCurve.Fp curve;
    private static ECPoint G;
    private boolean debug = false;
    static {
        curve = new ECCurve.Fp(p, // q
                a, // a
                b); // b
        G = curve.createPoint(gx, gy);
        ecc_bc_spec = new ECDomainParameters(curve, G, n);
    }
    public boolean isDebug() {
        return debug;
@@ -382,15 +391,23 @@
     */
    public void exportPublicKey(ECPoint publicKey, String path) {
        File file = new File(path);
        FileOutputStream fos=null;
        try {
            if (!file.exists())
                file.createNewFile();
            byte buffer[] = publicKey.getEncoded(false);
            FileOutputStream fos = new FileOutputStream(file);
            fos = new FileOutputStream(file);
            fos.write(buffer);
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
@@ -402,10 +419,12 @@
     */
    public ECPoint importPublicKey(String path) {
        File file = new File(path);
        FileInputStream fis =null;
        ECPoint ec=null;
        try {
            if (!file.exists())
                return null;
            FileInputStream fis = new FileInputStream(file);
            fis = new FileInputStream(file);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte buffer[] = new byte[16];
@@ -413,12 +432,19 @@
            while ((size = fis.read(buffer)) != -1) {
                baos.write(buffer, 0, size);
            }
            fis.close();
            return curve.decodePoint(baos.toByteArray());
            ec=curve.decodePoint(baos.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
        return ec;
    }
    /**
@@ -429,14 +455,23 @@
     */
    public void exportPrivateKey(BigInteger privateKey, String path) {
        File file = new File(path);
        ObjectOutputStream oos =null;
        try {
            if (!file.exists())
                file.createNewFile();
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(privateKey);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(oos!=null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
@@ -448,20 +483,35 @@
     */
    public BigInteger importPrivateKey(String path) {
        File file = new File(path);
        FileInputStream fis =null;
        ObjectInputStream ois =null;
        BigInteger res =null;
        try {
            if (!file.exists()) {
                return null;
            }
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            BigInteger res = (BigInteger) (ois.readObject());
            ois.close();
            fis.close();
            return res;
             fis = new FileInputStream(file);
             ois = new ObjectInputStream(fis);
             res = (BigInteger) (ois.readObject());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
        return res;
    }
    /**
@@ -780,9 +830,81 @@
        }
    }
    /**
     * 获取私钥
     * @return
     */
    public static BigInteger getPrivateKey(){
        ClassPathResource classPathResource = new ClassPathResource("/config/pri_key.ksm");
        InputStream inputStream = null;
        BigInteger res=null;
        ObjectInputStream ois=null;
        try {
            inputStream = classPathResource.getInputStream();
            ois = new ObjectInputStream(inputStream);
            res = (BigInteger) (ois.readObject());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return res;
    }
    /**
     * 获取公钥
     * @return
     */
    public static ECPoint getPublicKey(){
        try {
            ClassPathResource classPathResource = new ClassPathResource("/config/pub_key.ksm");
            InputStream inputStream = classPathResource.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte buffer[] = new byte[16];
            int size;
            while ((size = inputStream.read(buffer)) != -1) {
                baos.write(buffer, 0, size);
            }
            inputStream.close();
            return curve.decodePoint(baos.toByteArray());
        }catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void main(String[] args) throws UnsupportedEncodingException {
        SM2 sm02 = new SM2();
        //BigInteger privateKey = sm02.importPrivateKey(ClassUtils.getDefaultClassLoader().getResource("").getPath() + "config/pri_key.ksm");
        //ECPoint publicKey = sm02.importPublicKey(ClassUtils.getDefaultClassLoader().getResource("").getPath() + "config/pri_key.ksm");
        BigInteger privateKey2 = sm02.getPrivateKey();
        privateKey2 = privateKey2.add(new BigInteger("1"));
        ECPoint publicKey2 = sm02.getPublicKey();
        byte[] encrypt = sm02.encrypt("123456", publicKey2);
        String decrypt = sm02.decrypt(encrypt, privateKey2);
        System.out.println(decrypt);
        //System.out.println(privateKey);
        //System.out.println(publicKey);
        //System.out.println(privateKey2);
        //System.out.println(publicKey2);
      /*
       BigInteger px = new BigInteger(
       "0AE4C779 8AA0F119 471BEE11 825BE462 02BB79E2 A5844495 E97C04FF 4DF2548A".replace(" ", ""), 16);
@@ -792,15 +914,15 @@
       BigInteger privateKey = new BigInteger(
       "128B2FA8 BD433C6C 068C8D80 3DFF7979 2A519A55 171B1B65 0C23661D 15897263".replace(" ", ""), 16);
       */
        SM2KeyPair keyPair = sm02.generateKeyPair();
        /*SM2KeyPair keyPair = sm02.generateKeyPair();
        ECPoint publicKey=keyPair.getPublicKey();
        BigInteger privateKey=keyPair.getPrivateKey();
        sm02.exportPublicKey(publicKey, "E:/publickey.pem");
        sm02.exportPrivateKey(privateKey, "E:/privatekey.pem");
        System.out.println("-----------------公钥加密与解�?-----------------");
        /*ECPoint*/ publicKey = sm02.importPublicKey("E:/publickey.pem");
        /*BigInteger*/ privateKey = sm02.importPrivateKey("E:/privatekey.pem");
        *//*ECPoint*//* publicKey = sm02.importPublicKey("E:/publickey.pem");
        *//*BigInteger*//* privateKey = sm02.importPrivateKey("E:/privatekey.pem");
        byte[] data = sm02.encrypt("测试加密aaaaaaaaaaa123aabb", publicKey);
        System.out.print("密文:");
        SM2.printHexString(data);
@@ -826,7 +948,7 @@
        TransportEntity entity1 = aKeyExchange.keyExchange_1();
        TransportEntity entity2 = bKeyExchange.keyExchange_2(entity1);
        TransportEntity entity3 = aKeyExchange.keyExchange_3(entity2);
        bKeyExchange.keyExchange_4(entity3);
        bKeyExchange.keyExchange_4(entity3);*/
    }
    public static class Signature {