java文件加密方案
DES 密鑰生成,加解密方法,,你可以看一下
//DES 密鑰生成工具
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class GenKey {
private static final String DES = "DES";
public static final String SKEY_NAME = "key.des";
public static void genKey1(String path) {
// 密鑰
SecretKey skey = null;
// 密鑰隨機數生成
SecureRandom sr = new SecureRandom();
//生成密鑰文件
File file = genFile(path);
try {
// 獲取密鑰生成實例
KeyGenerator gen = KeyGenerator.getInstance(DES);
// 初始化密鑰生成器
gen.init(sr);
// 生成密鑰
skey = gen.generateKey();
// System.out.println(skey);
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeObject(skey);
oos.close();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param file : 生成密鑰的路徑
* SecretKeyFactory 方式生成des密鑰
* */
public static void genKey2(String path) {
// 密鑰隨機數生成
SecureRandom sr = new SecureRandom();
// byte[] bytes = {11,12,44,99,76,45,1,8};
byte[] bytes = sr.generateSeed(20);
// 密鑰
SecretKey skey = null;
//生成密鑰文件路徑
File file = genFile(path);
try {
//創建deskeyspec對象
DESKeySpec desKeySpec = new DESKeySpec(bytes,9);
//實例化des密鑰工廠
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
//生成密鑰對象
skey = keyFactory.generateSecret(desKeySpec);
//寫出密鑰對象
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeObject(skey);
oos.close();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static File genFile(String path) {
String temp = null;
File newFile = null;
if (path.endsWith("/") || path.endsWith("\\")) {
temp = path;
} else {
temp = path + "/";
}
File pathFile = new File(temp);
if (!pathFile.exists())
pathFile.mkdirs();
newFile = new File(temp+SKEY_NAME);
return newFile;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
genKey2("E:/a/aa/");
}
}
//DES加解密方法
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*制卡文件加/解密 加密方式DES
*/
public class SecUtil {
public static final Log log = LogFactory.getLog(SecUtil.class);
/**
* 解密
*
* @param keyPath
* 密鑰路徑
* @param source
* 解密前文件
* @param dest
* 解密後文件
*/
public static void decrypt(String keyPath, String source, String dest) {
SecretKey key = null;
try {
ObjectInputStream keyFile = new ObjectInputStream(
// 讀取加密密鑰
new FileInputStream(keyPath));
key = (SecretKey) keyFile.readObject();
keyFile.close();
} catch (FileNotFoundException ey1) {
log.info("Error when read keyFile");
throw new RuntimeException(ey1);
} catch (Exception ey2) {
log.info("error when read the keyFile");
throw new RuntimeException(ey2);
}
// 用key產生Cipher
Cipher cipher = null;
try {
// 設置演算法,應該與加密時的設置一樣
cipher = Cipher.getInstance("DES");
// 設置解密模式
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (Exception ey3) {
log.info("Error when create the cipher");
throw new RuntimeException(ey3);
}
// 取得要解密的文件並解密
File file = new File(source);
String filename = file.getName();
try {
// 輸出流,請注意文件名稱的獲取
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(dest));
// 輸入流
CipherInputStream in = new CipherInputStream(
new BufferedInputStream(new FileInputStream(file)), cipher);
int thebyte = 0;
while ((thebyte = in.read()) != -1) {
out.write(thebyte);
}
in.close();
out.close();
} catch (Exception ey5) {
log.info("Error when encrypt the file");
throw new RuntimeException(ey5);
}
}
/**
* 加密
* @param keyPath 密鑰路徑
* @param source 加密前文件
* @param dest 加密後文件
*/
public static void encrypt(String keyPath, String source, String dest) {
SecretKey key = null;
try {
ObjectInputStream keyFile = new ObjectInputStream(
// 讀取加密密鑰
new FileInputStream(keyPath));
key = (SecretKey) keyFile.readObject();
keyFile.close();
} catch (FileNotFoundException ey1) {
log.info("Error when read keyFile");
throw new RuntimeException(ey1);
} catch (Exception ey2) {
log.info("error when read the keyFile");
throw new RuntimeException(ey2);
}
// 用key產生Cipher
Cipher cipher = null;
try {
// 設置演算法,應該與加密時的設置一樣
cipher = Cipher.getInstance("DES");
// 設置解密模式
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (Exception ey3) {
log.info("Error when create the cipher");
throw new RuntimeException(ey3);
}
// 取得要解密的文件並解密
File file = new File(source);
String filename = file.getName();
try {
// 輸出流,請注意文件名稱的獲取
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(dest));
// 輸入流
CipherInputStream in = new CipherInputStream(
new BufferedInputStream(new FileInputStream(file)), cipher);
int thebyte = 0;
while ((thebyte = in.read()) != -1) {
out.write(thebyte);
}
in.close();
out.close();
} catch (Exception ey5) {
log.info("Error when encrypt the file");
throw new RuntimeException(ey5);
}
}
}
Ⅱ java 對大文件怎麼加密
選擇一個加密演算法
輸入為bufferIn,輸出為bufferOut
循環讀取文件,讀滿緩沖區就調用加密演算法,然後輸出至目標文件
不可能一次性將幾兆的文件一次性進行加密的
Ⅲ java加密
可以的,但是對jar包直接加密,目前只支持J2SE,還不支持J2EE。更多的還是用混編器(java obfuscator)。下面是關於HASP的介紹。
-----------------------------------------------------
針對java加密防止反編譯的解決方案
眾所周知,java開發語言提供了很方便的開發平台,開發出來的程序很容易在不同的平台上被移植,現在越來越多的人使用它來開發軟體,與.net語言並駕齊驅。
Java有它方便的一面,同時也給開發者帶來了一個不小的煩惱,就是保護程序代碼變得困難,因為java語言編譯和代碼執行的特殊性,目前,除了HASP外,還沒有一個更好的解決辦法或保護方案,但如果不採取有力的措施,則自己辛辛苦苦開發出來的程序很容易被人復制而據為己有,一般情況下,大多數的人都是用混編器(java obfuscator)來把開發出來的程序進行打亂,以想達到防止反編譯的目的,但是,這種方法在網上很容易找到相關的軟體來重新整理,那麼這個混編器工具也只能控制一些本來就沒有辦法的人,而對於稍懂工具的人幾乎是透明的,沒有任何意義。再說硬體加密鎖,大多數廠商提供的加密鎖只能進行dll的連接或簡單的api調用,只要簡單地反編譯,就很容易把api去掉,這樣加密鎖根本起不了作用,那到底是否還有更好的解決辦法呢?
現提供2種解決辦法:
1、以色列阿拉丁公司的HASP HL加密鎖提供的外殼加密工具中,有一個叫做數據加密的功能,這個功能可以很好的防止反編譯而去掉api的調用,大家知道:硬體加密鎖的保護原理就是讓加密過的軟體和硬體緊密地連接在一起,調用不會輕易地被剔除,這樣才能持久地保護您的軟體不被盜版,同時,這種方式使用起來非常簡單,很容易被程序員掌握,要對一個軟體實現保護,大約只需幾分鍾的時間就可以了,下面簡單介紹一下它的原理:
運用HASP HL的外殼工具先把java解釋器進行加密,那麼,如果要啟動這個解釋器就需要有特定的加密鎖存在,然後,再運用外殼工具中的數據加密功能把java程序(CLASS或JAR包)當作一個數據文件來進行加密處理,生成新的java程序(CLASS或JAR包),因為這個加密過程是在鎖內完成的,並採用了128位的AES演算法,這樣,加密後的java程序,無論你採用什麼樣的反編譯工具,都是無法反編譯出來的。您的軟體也只有被加密過的java解釋器並有加密鎖的情況下才能正常運行,如果沒有加密鎖,程序不能運行,從而達到真正保護您的軟體的目的。
2、HASP HL提供專門針對java外殼加密工具,直接加密jar包,防止外編譯,目前只支持J2SE,將來會進一步支持J2EE,如果情況適合則是最簡單的方法。
Ⅳ java項目如何加密
Java基本的單向加密演算法:
1.BASE64 嚴格地說,屬於編碼格式,而非加密演算法
2.MD5(Message Digest algorithm 5,信息摘要演算法)
3.SHA(Secure Hash Algorithm,安全散列演算法)
4.HMAC(Hash Message Authentication Code,散列消息鑒別碼)
按 照RFC2045的定義,Base64被定義為:Base64內容傳送編碼被設計用來把任意序列的8位位元組描述為一種不易被人直接識別的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.)
常見於郵件、http加密,截取http信息,你就會發現登錄操作的用戶名、密碼欄位通過BASE64加密的。
主要就是BASE64Encoder、BASE64Decoder兩個類,我們只需要知道使用對應的方法即可。另,BASE加密後產生的位元組位數是8的倍數,如果不夠位數以=符號填充。
MD5
MD5 -- message-digest algorithm 5 (信息-摘要演算法)縮寫,廣泛用於加密和解密技術,常用於文件校驗。校驗?不管文件多大,經過MD5後都能生成唯一的MD5值。好比現在的ISO校驗,都 是MD5校驗。怎麼用?當然是把ISO經過MD5後產生MD5的值。一般下載linux-ISO的朋友都見過下載鏈接旁邊放著MD5的串。就是用來驗證文 件是否一致的。
HMAC
HMAC(Hash Message Authentication Code,散列消息鑒別碼,基於密鑰的Hash演算法的認證協議。消息鑒別碼實現鑒別的原理是,用公開函數和密鑰產生一個固定長度的值作為認證標識,用這個 標識鑒別消息的完整性。使用一個密鑰生成一個固定大小的小數據塊,即MAC,並將其加入到消息中,然後傳輸。接收方利用與發送方共享的密鑰進行鑒別認證 等。
Ⅳ JAVA程序加密,怎麼做才安全
程序加密?你說的是代碼加密還是數據加密。我都說一下吧。
Java代碼加密:
這點因為Java是開源的,想達到完全加密,基本是不可能的,因為在反編譯的時候,雖然反編譯回來的時候可能不是您原來的代碼,但是意思是接近的,所以是不行的。
那麼怎麼增加反編譯的難度(閱讀難度),那麼可以採用多層繼承(實現)方式來解決,這樣即使反編譯出來的代碼,可讀性太差,復用性太差了。
Java數據加密:
我們一般用校驗性加密,常用的是MD5,優點是速度快,數據佔用空間小。缺點是不可逆,所以我們一般用來校驗數據有沒有被改動等。
需要可逆,可以選用base64,Unicode,缺點是沒有密鑰,安全性不高。
而我們需要可逆而且採用安全的方式是:對稱加密和非堆成加密,我們常用的有AES、DES等單密鑰和雙密鑰的方式。而且是各種語言通用的。
全部手動敲字,望採納,下面是我用Javascript方式做的一系列在線加密/解密工具:
http://www.sojson.com/encrypt.html
Ⅵ java 一個將文件加密的程序
import java.io.*;
class Encrypt{
private String pw;
private File fin;
private File fout;
public Encrypt(File fin,String pw,File fout) throws Exception{
if("".equals(pw)){
System.out.println("密碼不能為空");
System.exit(1);
}
this.pw=pw;
if(!fin.exists()){
System.out.println("輸入文件不存在");
System.exit(1);
}
this.fin=fin;
if(fout.exists()){
System.out.println("輸出文件已存在");
System.exit(1);
}
this.fout=fout;
this.enc();
}
public void enc() throws Exception{
byte[] pass=pw.getBytes();
int length=pw.length();
int i=0;
InputStream in=new FileInputStream(fin);
OutputStream out=new FileOutputStream(fout);
int indata;
indata=in.read();
while(indata!=-1){
i++;
indata^=pass[i%length];
out.write(indata);
System.out.print((char)indata);
indata=in.read();
}
in.close();
out.close();
}
}
public class EncryptFile{
public static void main(String args[]) throws Exception{
if(args.length!=3){
System.out.println("參數錯誤:參數格式應為: input_file password output_file");
System.exit(1);
}
File fin=new File(args[0]);
File fout=new File(args[2]);
new Encrypt(fin,args[1],fout);
}
}
沒完全按照你的要求 自己再改改吧 ~~~~~~~~~
Ⅶ Java加密方式
這個一般沒有統一的標准,教材有不同的版本一樣。
我做過這個,記得很清楚
加密方式1:Conye加密方法
加密方式2:WeiffbYfds方法
就是這樣了,不懂追問哈,嘻嘻。
Ⅷ java中文件加鎖機制是怎麼實現的。
Java中文件加鎖機制如下:
在對文件操作過程中,有時候需要對文件進行加鎖操作,防止其他線程訪問該文件。對文件的加鎖方法有兩種:
第一種方法:使用RandomAccessFile類操作文件。
在java.io.RandomAccessFile類的open方法,提供了參數實現獨占的方式打開文件:
RandomAccessFile raf = new RandomAccessFile(file, "rws");
其中的「rws」參數,rw代表讀取和寫入,s代表了同步方式,也就是同步鎖。這種方式打開的文件,就是獨占方式的。
第二種方法:使用sun.nio.FileChannel對文件進行加鎖。
代碼:
RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
FileChannel fc = raf.getChannel();
FileLock fl = fc.tryLock();
if(fl.isValid())
System.out.println("You have got the file lock.");
以上是通過RandomAccessFile來獲得文件鎖的,方法如下:
代碼:
FileOutputStream fos = new FileOutputStream("file.txt");
FileChannel fc = fos.getChannel(); //獲取FileChannel對象
FileLock fl = fc.tryLock(); //or fc.lock();
if(null != fl)
System.out.println("You have got file lock.");
//TODO write content to file
//TODO write end, should release this lock
fl.release(); //釋放文件鎖
fos.close; //關閉文件寫操作
如果在讀文件操作的時候,對文件進行加鎖,操作過程如下:
FileChannel也可以從FileInputStream中直接獲得,但是這種直接獲得FileChannel的對象直接去操作FileLock會報異常NonWritableChannelException,需要自己去實現getChannel方法,代碼如下:
private static FileChannel getChannel(FileInputStream fin, FileDescriptor fd) {
FileChannel channel = null;
synchronized(fin){
channel = FileChannelImpl.open(fd, true, true, fin);
return channel;
}
}
其實,看FileInputStream時,發現getChannel方法與我們寫的代碼只有一個地方不同,即open方法的第三個參數不同,如果設置為false,就不能鎖住文件了。預設的getChannel方法,就是false,因此,不能鎖住文件。