-
大小: 10KB文件類型: .rar金幣: 1下載: 0 次發(fā)布日期: 2021-06-11
- 語言: Java
- 標(biāo)簽:
資源簡(jiǎn)介
文件加密解密算法(Java源碼)
java,file,算法,加密解密,java源碼
package com.crypto.encrypt;
import java.security.SecureRandom;
import java.io.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.Cipher;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.lang.reflect.Constructor;
import java.security.spec.KeySpec;
import java.lang.reflect.InvocationTargetException;
public class EncryptData {
private String keyfile=null;
public EncryptData() {
}
public EncryptData(String keyfile) {
this.keyfile=keyfile;
}
/**
* 加密文件
* @param filename String 源路徑
* @param filenamekey String 加密后的路徑
*/
public void createEncryptData(String filename,String filenamekey) throws
IllegalStateException, IllegalBlockSizeException, BadPaddingException,
NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException,
InvalidKeyException, IOException, InstantiationException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException,
ClassNotFoundException, IllegalStateException, IllegalBlockSizeException,
BadPaddingException, NoSuchPaddingException, InvalidKeySpecException,
NoSuchAlgorithmException, InvalidKeyException, IOException {
//驗(yàn)證keyfile
if(keyfile==null || keyfile.equals(""))
{
throw new NullPointerException("無效的key文件路徑");
}
encryptData(filename,filenamekey);
}
/**
* 加密類文件
* @param filename String 原始的類文件
* @param encryptfile String 加密后的類文件
* @throws IOException
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @thro
代碼片段和文件信息
package?com.crypto.encrypt;
import?java.security.SecureRandom;
import?javax.crypto.KeyGenerator;
import?java.security.NoSuchAlgorithmException;
import?javax.crypto.SecretKey;
import?java.io.*;
public?class?CreateKey?{
??String?filename=““;
??public?CreateKey()?{
??}
??/**
???*?獲得密匙字節(jié)內(nèi)容
???*?@throws?IOException
???*?@return?byte[]
???*/
??public?byte[]?getKeyByte()?throws?IOException?{
????byte[]?bytes=Util.readFile(filename);
????return?bytes;
??}
??public?void?CreateKeyFile(String?filename)?throws?IOException
??????NoSuchAlgorithmException?{
????this.filename=filename;
????if(filename==null?||?filename.equals(““))
????{
??????throw?new?NullPointerException(“無效的文件路徑“);
????}
????createKey();
??}
??/**
???*?生成密匙
???*?@throws?NoSuchAlgorithmException
???*?@throws?IOException
???*/
??private?void?createKey()?throws?NoSuchAlgorithmException?IOException?{
????SecureRandom?secureRandom?=?new?SecureRandom();
????//?為我們選擇的DES算法生成一個(gè)KeyGenerator對(duì)象
????KeyGenerator?kg?=?KeyGenerator.getInstance(Util.getValue(“algorithm“));
????kg.init(secureRandom);
????//?生成密鑰
????SecretKey?key?=?kg.generateKey();
????//?將密鑰數(shù)據(jù)保存為文件供以后使用
????Util.writeFile(key.getEncoded()filename);
??}
??/**
???*?獲得密匙文件路徑
???*?@return?String
???*/
??public?String?getKeyFilePath()
??{
????return?filename;
??}
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件???????1417??2005-08-14?13:00??文件加密解密\com\crypto\encrypt\CreateKey.java
?????文件???????5574??2005-08-15?21:40??文件加密解密\com\crypto\encrypt\EncryptData.java
?????文件???????6194??2005-08-15?21:39??文件加密解密\com\crypto\encrypt\UnEncryptData.java
?????文件???????2641??2005-08-15?23:36??文件加密解密\com\crypto\encrypt\Util.java
?????文件??????13070??2005-09-15?13:27??文件加密解密\des.jar
?????文件?????????76??2005-08-15?23:27??文件加密解密\resource\algorithm.properties
?????文件??????????8??2005-08-15?23:06??文件加密解密\resource\key
?????目錄??????????0??2005-09-15?13:20??文件加密解密\com\crypto\encrypt
?????目錄??????????0??2005-08-16?08:35??文件加密解密\com\crypto
?????目錄??????????0??2005-08-16?08:35??文件加密解密\com
?????目錄??????????0??2005-08-16?08:35??文件加密解密\resource
?????目錄??????????0??2009-12-29?15:28??文件加密解密
-----------?---------??----------?-----??----
????????????????28980????????????????????12
評(píng)論
共有 條評(píng)論