| | |
| | | */ |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | 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]; |
| | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | 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; |
| | | } |
| | | |
| | | /** |