[java]aes文件加密sample

package encrypt.file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.Security;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class FileAesEncryptorSample {
	private static final String ID_KEY = "^f7e5a218c9d05%";
	public static void main(String[] args) throws Exception {
		Security.addProvider(new BouncyCastleProvider());
		System.out.println("===start===========================");
		encryptFile("D:/workspace/workspace.rar", "D:/workspace/myfile.rar");
		System.out.println("===encrypt=========================");
		decryptedFile("D:/workspace/myfile.rar", "D:/workspace/myfile2.rar");
		System.out.println("===decrypt=========================");
	}
	
	@SuppressWarnings("static-access")
	//文件加密的實現方法
	public static void encryptFile(String fileName,String encryptedFileName) throws Exception{
		FileInputStream fis = new FileInputStream(fileName);
		FileOutputStream fos = new FileOutputStream(encryptedFileName);
		
		//祕鑰自動生成
		KeyGenerator keyGenerator=KeyGenerator.getInstance("AES");
		keyGenerator.init(128);
		Key key=keyGenerator.generateKey();
		
		
		byte[] keyValue=key.getEncoded();
		
		fos.write(keyValue);//記錄輸入的加密密碼的消息摘要
		
		SecretKeySpec encryKey= new SecretKeySpec(keyValue,"AES");//加密祕鑰
		
		byte[] ivValue=new byte[16];
		Random random = new Random(System.currentTimeMillis());
		random.nextBytes(ivValue);
		IvParameterSpec iv = new IvParameterSpec(ivValue);//獲取系統時間作爲IV
		
		fos.write(ID_KEY.getBytes());//文件標識符
 
			fos.write(ivValue);	//記錄IV
		Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
		cipher.init(cipher.ENCRYPT_MODE, encryKey,iv);
		
		CipherInputStream cis=new CipherInputStream(fis, cipher);
		
		byte[] buffer=new byte[1024];
		int n=0;
		while((n=cis.read(buffer))!=-1){
			fos.write(buffer,0,n);	
		}
		cis.close();
		fos.close();
	}
	
	@SuppressWarnings("static-access")
	//文件解密的實現代碼
	public static void decryptedFile(String encryptedFileName,String decryptedFileName) throws Exception{
		
		FileInputStream fis = new FileInputStream(encryptedFileName);
		FileOutputStream fos = new FileOutputStream(decryptedFileName);	
		
		byte[] fileIdentifier=new byte[15];
		
		byte[] keyValue=new byte[16];
		fis.read(keyValue);//讀記錄的文件加密密碼的消息摘要
		fis.read(fileIdentifier);				
		if(new String (fileIdentifier).equals(ID_KEY)){
			SecretKeySpec key= new SecretKeySpec(keyValue,"AES");					
			byte[] ivValue= new byte[16];
			fis.read(ivValue);//獲取IV值
			IvParameterSpec iv= new IvParameterSpec(ivValue);
			Cipher cipher = Cipher.getInstance("AES/CFB/PKCS5Padding");
			cipher.init(cipher.DECRYPT_MODE, key,iv);
			CipherInputStream cis= new CipherInputStream(fis, cipher);												
			byte[] buffer=new byte[1024];
			int n=0;
			while((n=cis.read(buffer))!=-1){
				fos.write(buffer,0,n);		
			}
			cis.close();
			fos.close();
		}else{
			System.out.println("不是我的加密文件");
		}
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章