用DES对文件加密的程序

                                                            用DES对文件加密的程序

                                                                                                                              作者:周顺利

        这个程序使用了JAVA类库的功能实现对磁盘文件的加密,加密算法采用DES加密,不过经过简单的修改我们可以采用JDK的 其他加密算法.在加密文件以前必须用JDK工具Keytool生成一个.ks库文件.这里为jpatkeystore.ks密码为changeit.具体参考keytool的帮助!

import java.io.*;
import javax.crypto.*;
import java.security.Key;
import java.security.KeyStore;

public class FileEncryption
{
 static final String KEYSTORE="jpatkeystore.ks";
 static final String KEYALIAS="examplesRSApair";
 static final String STOREPASSWD="changeit";
 static final String ALIASPASSWD="changeit";
private static Key getKey()
{
Key k=null;
try
{
KeyStore ks=KeyStore.getInstance("JCEKS");
ks.load(new FileInputStream(KEYSTORE),STOREPASSWD.toCharArray());
if(ks.isKeyEntry(KEYALIAS))
{
k=ks.getKey(KEYALIAS,ALIASPASSWD.toCharArray());
}
else
{
KeyGenerator kg=KeyGenerator.getInstance("DES");
k=kg.generateKey();
ks.setKeyEntry(KEYALIAS,k,ALIASPASSWD.toCharArray(),null);
ks.store(new FileOutputStream(KEYSTORE),STOREPASSWD.toCharArray());
}
}
catch(Exception x)
{
System.out.println(x);
x.printStackTrace();
}
return k;
}

private static Cipher getCipher(int opMode)
{
Cipher c=null;
try
{
Key k=getKey();
c=Cipher.getInstance(k.getAlgorithm());
c.init(opMode,k);
}
catch(Exception x)
{
System.out.println(x);
x.printStackTrace();
}
return c;
}
private static void encryptFile(String clearFile,String encrFile)
{
try
{
FileInputStream fis=new FileInputStream(clearFile);
FileOutputStream fos=new FileOutputStream(encrFile);
Cipher c=getCipher(Cipher.ENCRYPT_MODE);
CipherOutputStream cphout=new CipherOutputStream(fos,c);
byte[] byteBuff=new byte[8196];
int bytesRead=fis.read(byteBuff);
while(bytesRead!=-1)
{
cphout.write(byteBuff,0,bytesRead);
bytesRead=fis.read(byteBuff);
}
cphout.close();
fis.close();
fos.close();
}
catch(IOException iox)
{
System.out.println(iox);
}
}

private static void decryptFile(String encrFile,String clearFile)
{
try
{
FileInputStream fis=new FileInputStream(encrFile);
Cipher c=getCipher(Cipher.DECRYPT_MODE);
CipherInputStream cphin=new CipherInputStream(fis,c);
FileOutputStream fos=new FileOutputStream(clearFile);
byte[] byteBuff=new byte[8196];
int byteRead=cphin.read(byteBuff);
while(byteRead!=-1)
{
fos.write(byteBuff,0,byteRead);
byteRead=cphin.read(byteBuff);
}
fis.close();
fos.close();
cphin.close();
}
catch(IOException iox)
{
System.out.println(iox);
}
}
private static void displayUsageMessage()
{
System.out.println("Usage:"+FileEncryption.class.getName()+"{/e|/d}"+"soruce_file target_file");
}
public static void main(String[] args)
{
if(args.length<2)
{
}
else
{
if(args[0].equalsIgnoreCase("/e"))
{
encryptFile(args[1],args[2]);
System.out.println(args[1]+"successfully encrytped as"+args[2]);
}
else if(args[0].equalsIgnoreCase("/d"))
{
decryptFile(args[1],args[2]);
System.out.println(args[1]+"successfully decrypted as"+args[2]);
}
else
{
displayUsageMessage();
}
}
}
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值