熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Java文件加密-spring屬性文件加密

2022-06-13   來源: Java開源技術 

  package comhappysecurityproperties;

  import javaioByteArrayInputStream;

  import javaioByteArrayOutputStream;

  import javaioFile;

  import javaioFileInputStream;

  import javaioFileOutputStream;

  import javaioInputStream;

  import javaioObjectInputStream;

  import javaioObjectOutputStream;

  import javasecurityKey;

  import javasecurityNoSuchAlgorithmException;

  import javasecuritySecureRandom;

  import javasecuritySecurity;

  import javaxcryptoCipher;

  import javaxcryptoKeyGenerator;

  public class DESEncryptUtil {

  public static Key createKey() throws NoSuchAlgorithmException {//創建密鑰

  SecurityinsertProviderAt(new comsuncryptoproviderSunJCE() );

  KeyGenerator generator = KeyGeneratorgetInstance(DES);

  generatorinit(new SecureRandom());

  Key key = generatorgenerateKey();

  return key;

  }

  public static Key getKey(InputStream is) {

  try {

  ObjectInputStream ois = new ObjectInputStream(is);

  return (Key) oisreadObject();

  } catch (Exception e) {

  eprintStackTrace();

  throw new RuntimeException(e);

  }

  }

  private static byte[] doEncrypt(Key key byte[] data) {//對數據進行加密?

  try {

  Cipher cipher = CiphergetInstance(DES/ECB/PKCSPadding);

  cipherinit(CipherENCRYPT_MODE key);

  byte[] raw = cipherdoFinal(data);

  return raw;

  } catch (Exception e) {

  eprintStackTrace();

  throw new RuntimeException(e);

  }

  }

  public static InputStream doDecrypt(Key key InputStream in) {//對數據進行解密?

  try {

  Cipher cipher = CiphergetInstance(DES/ECB/PKCSPadding);

  cipherinit(CipherDECRYPT_MODE key);

  ByteArrayOutputStream bout = new ByteArrayOutputStream();

  byte[] tmpbuf = new byte[];

  int count = ;

  while ((count = inread(tmpbuf)) != ) {

  boutwrite(tmpbuf count);

  tmpbuf = new byte[];

  }

  inclose();

  byte[] orgData = bouttoByteArray();

  byte[] raw = cipherdoFinal(orgData);

  ByteArrayInputStream bin = new ByteArrayInputStream(raw);

  return bin;

  } catch (Exception e){

  eprintStackTrace();

  throw new RuntimeException(e);

  }

  }

  public static void main(String[] args) throws Exception{

  Systemoutprintln(===================);

  if (argslength == && args[]equals(key)){

  Key key = DESEncryptUtilcreateKey();

  ObjectOutputStream oos = new ObjectOutputStream(

  new FileOutputStream(args[]));

  ooswriteObject(key);

  oosclose();

  Systemoutprintln(成功生成密鑰文件);

  } else if (argslength == && args[]equals(encrypt)){

  File file = new File(args[]);

  FileInputStream in = new FileInputStream(file);

  ByteArrayOutputStream bout = new ByteArrayOutputStream();

  byte[] tmpbuf = new byte[];

  int count = ;

  while ((count = inread(tmpbuf)) != ){

  boutwrite(tmpbuf count);

  tmpbuf = new byte[];

  }

  inclose();

  byte[] orgData = bouttoByteArray();

  Key key = getKey(new FileInputStream(args[]));

  byte[] raw = DESEncryptUtildoEncrypt(key orgData);

  file = new File(filegetParent() + \\en_ + filegetName());

  FileOutputStream out = new FileOutputStream(file);

  outwrite(raw);

  outclose();

  Systemoutprintln(成功加密加密文件位:+filegetAbsolutePath());

  } else if (argslength == && args[]equals(decrypt)){//對文件進行解密

  File file = new File(args[]);

  FileInputStream fis = new FileInputStream(file);

  Key key = getKey(new FileInputStream(args[]));

  InputStream raw = DESEncryptUtildoDecrypt(key fis);

  ByteArrayOutputStream bout = new ByteArrayOutputStream();

  byte[] tmpbuf = new byte[];

  int count = ;

  while ((count = rawread(tmpbuf)) != ){

  boutwrite(tmpbuf count);

  tmpbuf = new byte[];

  }

  rawclose();

  byte[] orgData = bouttoByteArray();

  file = new File(filegetParent() + \\rs_ + filegetName());

  FileOutputStream fos = new FileOutputStream(file);

  foswrite(orgData);

  Systemoutprintln(成功解密解密文件位:+filegetAbsolutePath());

  }else if(argslength== && args[]equals(h)) {

  Systemoutprintln(\t文件加密解密\n);

  Systemoutprintln(創建密鑰文件java jar keyjar key E:/keydat);

  Systemerrprintln(其中keyjar為需要運行的Jar文件key參數表示要創建加密文件 E:/keydat表示加密文件的存放位置);

  Systemoutprintln(\n);

  Systemoutprintln(加密文件:java jar keyjar encrypt E:/testproperties E:/keydat );

  Systemerrprintln(其中keyjar為需要運行的Jar文件 encrypt 參數表示加密 E:/testproperties表示需要加密的文件 E:/keydat表示加密密鑰文件);

  Systemoutprintln(\n);

  Systemoutprintln(解密文件java jar keyjar decrypt E:/en_testproperties E:/keydat );

  Systemerrprintln(其中keyjar為需要運行的Jar文件 decrypt參數表示解密 E:/en_testproperties表示需要解密的文件 E:/keydat表示解密的密鑰文件);

  }else {

  Systemoutprintln(你需要運行參數h);

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28658.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.