【题意要求】利用Java中分组加密的类包,完成对文件的DES算法加密。
(1) 利用java中的KeyGenerator类创建对称秘钥,利用工厂类KeyGenerator的静态方法getInstance()获得KeyGenerator()类对象;
(2) 方法getInstance()的参数为字符串类型,指定加密算法的名称如:Blowfish、DES、DESede、HmacMD5或HmacSHA1等,本题使用DES这个参数;
(3) 利用工厂类Cipher的对象可以创建密码器。同样的,getInstance()的参数为字符串类型,指定加密算法的名称,本题指定为“DES”。
对文件的操作和对字符串的操作基本是相同的,只是对文件的操作是将密钥在加密时已经存储到一个文件中,本示例程序是将其保存到key.txt中,然后分别在加密和解密时用到这个文件,进行对已经保存在相应文件中的内容进行读取并进行加解密操作。
程序代码在下面DESForFile.java中体现,其中public void savePriveKey(String keyFile)完成对密钥的保存到keyFile中;public void encryptionFile(String file,String keyFile,String encryptedFile)完成对保存密钥函数的调用,并读取密钥文件的内容,然后用其对文件file中的内容进行加密,然后将加密内容保存到encryptedFile中;public void decryptionFile(String encryptedFile,String keyFile,String decryptedFile)完成对文件keyFile的读取,并利用其对encryptedFile内容的解密操作,然后将解密的内容保存到decryptedFile中。private SecretKey getPrivetKey(String keyFile)是完成对keyFile密钥文件的内容的读取,本方法为私有方法,分别在加解密方法体中被调用。
package Practice2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DESForFile {
public void savePriveKey(String keyFile){
FileOutputStream fos=null;
try {
KeyGenerator keyGen=KeyGenerator.getInstance("DES");
keyGen.init(56);//DES needs 56 bytes.
// 生成秘密密钥
SecretKey key=keyGen.generateKey();
byte[] rawKeyData=key.getEncoded();
fos=new FileOutputStream(keyFile);
fos.write(rawKeyData);
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void encryptionFile(String file,String keyFile,String encryptedFile){
savePriveKey(keyFile);
SecretKey key=getPrivetKey(keyFile);
FileInputStream fis=null;
FileOutputStream fos=null;
try {
Cipher cipher=Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
fis=new FileInputStream(new File(file));
byte[] data=new byte[fis.available()];
fis.read(data);
byte[] encryptedData=cipher.doFinal(data);
fos=new FileOutputStream(new File(encryptedFile));
fos.write(encryptedData);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void decryptionFile(String encryptedFile,String keyFile,String decryptedFile){
SecretKey key=getPrivetKey(keyFile);
FileInputStream fis=null;
FileOutputStream fos=null;
try {
Cipher cipher=Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,key);
fis=new FileInputStream(new File(encryptedFile));
byte[] encryptedData=new byte[fis.available()];
fis.read(encryptedData);
byte[] decryptedData=cipher.doFinal(encryptedData);
fos=new FileOutputStream(new File(decryptedFile));
fos.write(decryptedData);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private SecretKey getPrivetKey(String keyFile) {
FileInputStream fis=null;
SecretKey key=null;
try {
fis=new FileInputStream(new File(keyFile));
byte[] rawKeyData=new byte[fis.available()];
fis.read(rawKeyData);
DESKeySpec dks=new DESKeySpec(rawKeyData);
key=SecretKeyFactory.getInstance("DES").generateSecret(dks);
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return key;
}
public static void main(String[] args){
DESForFile desUtil=new DESForFile();
desUtil.encryptionFile("source.txt", "key.txt","encrypted.txt");
desUtil.decryptionFile("encrypted.txt", "key.txt","decrypted.txt");
}
}