From cba96d4cbd3ddd8af20b2d65d1f430c75f667821 Mon Sep 17 00:00:00 2001 From: lxw <810412026@qq.com> Date: 星期三, 22 十一月 2023 11:43:43 +0800 Subject: [PATCH] 2.2.9资源未释放 --- src/main/java/com/whyc/encryption/SM2.java | 96 ++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 76 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/whyc/encryption/SM2.java b/src/main/java/com/whyc/encryption/SM2.java index 29c1478..cbf573f 100644 --- a/src/main/java/com/whyc/encryption/SM2.java +++ b/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; } /** -- Gitblit v1.9.1