知识大全 Java文件加密-spring属性文件加密

Posted 文件

篇首语:知识养成了思想,思想同时又在融化知识。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Java文件加密-spring属性文件加密相关的知识,希望对你有一定的参考价值。

Java文件加密-spring属性文件加密  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  package happy security properties;

  import java io ByteArrayInputStream;

  import java io ByteArrayOutputStream;

  import java io File;

  import java io FileInputStream;

  import java io FileOutputStream;

  import java io InputStream;

  import java io ObjectInputStream;

  import java io ObjectOutputStream;

  import java security Key;

  import java security NoSuchAlgorithmException;

  import java security SecureRandom;

  import java security Security;

  import javax crypto Cipher;

  import javax crypto KeyGenerator;

  public class DESEncryptUtil

  public static Key createKey() throws NoSuchAlgorithmException //创建密钥

  Security insertProviderAt(new sun crypto provider SunJCE() );

  KeyGenerator generator = KeyGenerator getInstance( DES );

  generator init(new SecureRandom());

  Key key = generator generateKey();

  return key;

  

  public static Key getKey(InputStream is)

  try

  ObjectInputStream ois = new ObjectInputStream(is);

  return (Key) ois readObject();

   catch (Exception e)

  e printStackTrace();

  throw new RuntimeException(e);

  

  

  private static byte[] doEncrypt(Key key byte[] data) //对数据进行加密?

  try

  Cipher cipher = Cipher getInstance( DES/ECB/PKCS Padding );

  cipher init(Cipher ENCRYPT_MODE key);

  byte[] raw = cipher doFinal(data);

  return raw;

   catch (Exception e)

  e printStackTrace();

  throw new RuntimeException(e);

  

  

  public static InputStream doDecrypt(Key key InputStream in) //对数据进行解密?

  try

  Cipher cipher = Cipher getInstance( DES/ECB/PKCS Padding );

  cipher init(Cipher DECRYPT_MODE key);

  ByteArrayOutputStream bout = new ByteArrayOutputStream();

  byte[] tmpbuf = new byte[ ];

  int count = ;

  while ((count = in read(tmpbuf)) != )

  bout write(tmpbuf count);

  tmpbuf = new byte[ ];

  

  in close();

  byte[] Data = bout toByteArray();

  byte[] raw = cipher doFinal(Data);

  ByteArrayInputStream bin = new ByteArrayInputStream(raw);

  return bin;

   catch (Exception e)

  e printStackTrace();

  throw new RuntimeException(e);

  

  

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

  System out println( =================== );

  if (args length == && args[ ] equals( key ))

  Key key = DESEncryptUtil createKey();

  ObjectOutputStream oos = new ObjectOutputStream(

  new FileOutputStream(args[ ]));

  oos writeObject(key);

  oos close();

  System out println( 成功生成密钥文件 );

   else if (args length == && 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 = in read(tmpbuf)) != )

  bout write(tmpbuf count);

  tmpbuf = new byte[ ];

  

  in close();

  byte[] Data = bout toByteArray();

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

  byte[] raw = DESEncryptUtil doEncrypt(key Data);

  file = new File(file getParent() + \\\\en_ + file getName());

  FileOutputStream out = new FileOutputStream(file);

  out write(raw);

  out close();

  System out println( 成功加密 加密文件位: +file getAbsolutePath());

   else if (args length == && args[ ] equals( decrypt ))//对文件进行解密

  File file = new File(args[ ]);

  FileInputStream fis = new FileInputStream(file);

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

  InputStream raw = DESEncryptUtil doDecrypt(key fis);

  ByteArrayOutputStream bout = new ByteArrayOutputStream();

  byte[] tmpbuf = new byte[ ];

  int count = ;

  while ((count = raw read(tmpbuf)) != )

  bout write(tmpbuf count);

  tmpbuf = new byte[ ];

  

  raw close();

  byte[] Data = bout toByteArray();

  file = new File(file getParent() + \\\\rs_ + file getName());

  FileOutputStream fos = new FileOutputStream(file);

  fos write(Data);

  System out println( 成功解密 解密文件位: +file getAbsolutePath());

  else if(args length== && args[ ] equals( h ))

  System out println( \\t文件加密解密\\n );

  System out println( 创建密钥文件 java jar key jar key E:/key dat );

  System err println( 其中key jar为需要运行的Jar文件 key参数表示要创建加密文件 E:/key dat表示加密文件的存放位置 );

  System out println( \\n );

  System out println( 加密文件:java jar key jar encrypt E:/test properties E:/key dat );

  System err println( 其中key jar为需要运行的Jar文件 encrypt 参数表示加密 E:/test properties表示需要加密的文件 E:/key dat表示加密密钥文件 );

  System out println( \\n );

  System out println( 解密文件 java jar key jar decrypt E:/en_test properties E:/key dat );

  System err println( 其中key jar为需要运行的Jar文件 decrypt参数表示解密 E:/en_test properties表示需要解密的文件 E:/key dat表示解密的密钥文件 );

  else

  System out println( 你需要运行参数 h );

  

  

cha138/Article/program/Java/ky/201311/28658

相关参考