【题意要求】利用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 voidsavePriveKey(String keyFile)完成对密钥的保存到keyFile中;publicvoid encryptionFile(String file,String keyFile,StringencryptedFile)完成对保存密钥函数的调用,并读取密钥文件的内容,然后用其对文件file中的内容进行加密,然后将加密内容保存到encryptedFile中;publicvoid decryptionFile(String encryptedFile,StringkeyFile,StringdecryptedFile)完成对文件keyFile的读取,并利用其对encryptedFile内容的解密操作,然后将解密的内容保存到decryptedFile中。privateSecretKey getPrivetKey(StringkeyFile)是完成对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){
FileOutputStreamfos=null;
try {
KeyGeneratorkeyGen=KeyGenerator.getInstance("DES");
keyGen.init(56);//DESneeds 56 bytes.
//生成秘密密钥
SecretKeykey=keyGen.generateKey();
byte[]rawKeyData=key.getEncoded();
fos=newFileOutputStream(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,StringkeyFile,String encryptedFile){
savePriveKey(keyFile);
SecretKeykey=getPrivetKey(keyFile);
FileInputStream fis=null;
FileOutputStreamfos=null;
try {
Ciphercipher=Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
fis=newFileInputStream(new File(file));
byte[]data=new byte[fis.available()];
fis.read(data);
byte[]encryptedData=cipher.doFinal(data);
fos=newFileOutputStream(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(StringencryptedFile,String keyFile,String decryptedFile){
SecretKeykey=getPrivetKey(keyFile);
FileInputStream fis=null;
FileOutputStreamfos=null;
try {
Ciphercipher=Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,key);
fis=newFileInputStream(new File(encryptedFile));
byte[]encryptedData=new byte[fis.available()];
fis.read(encryptedData);
byte[]decryptedData=cipher.doFinal(encryptedData);
fos=newFileOutputStream(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 {