java

位置:IT落伍者 >> java >> 浏览文章

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


发布日期:2022年08月03日
 
Java文件加密-spring属性文件加密

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);

}

}

}

               

上一篇:Struts源码研究 - Action-Input属性篇

下一篇:Eclipse常用快捷键