whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/encryption/SM2.java
@@ -391,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();
                }
            }
        }
    }
@@ -411,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];
@@ -422,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;
    }
    /**
@@ -438,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();
                }
            }
        }
    }
@@ -457,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;
    }
    /**
@@ -796,16 +837,31 @@
    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();
            ObjectInputStream ois = new ObjectInputStream(inputStream);
            BigInteger res = (BigInteger) (ois.readObject());
            ois.close();
            return res;
            ois = new ObjectInputStream(inputStream);
            res = (BigInteger) (ois.readObject());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }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;
    }
    /**