DES加密算法的java实现(基于java类库)

嗯嗯........这个是我新开的博客上的第一篇的文章,这里小白希望自己的技术能够一天比一天好(p≧w≦q),加油!

好吧,现在来一个基于java类库的DES加密算法的实现吧~网上不少的代码要不运行有问题,要不就是简简单单内置一个固定的加密字符串就简单完事了。好吧,我承认我现在是为懒人服务的,不过这里面还是有一点点需要解决的问题的啊!

注意的问题,在存储文件的时候,不要把它转为String在存到文件里面去,因为在转为String的时候,byte类型会发生变化,所以我是直接存储byte的,而且前面要生成一个DES key,方便不同文档之间的加解密。


先附上源代码

//因为楼主经常跨平台,MAC、Windows两边跑,中文容易乱码,所以英文注释哦



这个有一个问题,就是在加密完成,解密后会丢掉换行符。。。

import java.util.Scanner;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;

import java.util.*;
import java.awt.*;
import java.io.*;
import java.math.*;

public class DES_N {
	String password="95880228";
	String algorithm="DES";
	SecretKey DESkey;
	public byte[] decryptFun(byte[] cipertext){
		try{
			
			//use the ciper text in a byte type from the father function
			if(!(new File("DESkey.dat")).exists()){
				System.out.println("can not find the DES key!");
				return null;
			}
			else{
				ObjectInputStream in = new ObjectInputStream(new FileInputStream("DESkey.dat"));
				DESkey = (SecretKey)in.readObject();
				in.close();
			}
			Cipher cipher = Cipher.getInstance(algorithm);
			cipher.init(Cipher.DECRYPT_MODE, DESkey);
			//System.out.println("decrypt string:"+cipertext.toString());
			return  cipher.doFinal(cipertext);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	public byte[] encrypt(String input)
	{
		try{
			BufferedReader in_clear = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
			StringBuffer content = new StringBuffer();
			String s="";
			while((s=in_clear.readLine())!=null){
				content.append(s);
			}
			in_clear.close();
			
			String cleartext=content.toString();
			if(!(new File("DESkey.dat")).exists()){
				System.out.println("creating DES key");
				KeyGenerator keygen=KeyGenerator.getInstance(algorithm);
				DESkey = keygen.generateKey();
				
				ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("DESkey.dat"));
				outputStream.writeObject(DESkey);
				outputStream.close();
			}
			else{
				System.out.println("read DES key");
				ObjectInputStream in = new ObjectInputStream(new FileInputStream("DESkey.dat"));
				DESkey = (SecretKey)in.readObject();
				in.close();
			}
			Cipher c1=Cipher.getInstance(algorithm);
			c1.init(Cipher.ENCRYPT_MODE, DESkey);
			
			return c1.doFinal(cleartext.getBytes());
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	
	public byte[] encryptFun(String inputfile) {
		try{
			BufferedReader in_clear = new BufferedReader(new InputStreamReader(new FileInputStream(inputfile)));
			StringBuffer content = new StringBuffer();
			String s="";
			while((s=in_clear.readLine())!=null){
				content.append(s);
			}
			in_clear.close();
			String cleartext=content.toString();
			//get the content from a document
			
			SecureRandom random = new SecureRandom();
			DESKeySpec deskey = new DESKeySpec(password.getBytes());
			
			//creat a key factory 
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secrekey=keyFactory.generateSecret(deskey);
			
			//use ciper object to encrypt
			Cipher cipher=Cipher.getInstance(algorithm);
			cipher.init(Cipher.ENCRYPT_MODE, secrekey,random);
			
			return cipher.doFinal(cleartext.getBytes());
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	public byte[] readInbyte(String inputname)
	{
		try{
			File file = new File(inputname);
			FileInputStream in = new FileInputStream(file);
			long filesize = file.length();
			byte[] readin = new byte[(int)filesize];
			
			int offset=0;
			int numRead=0;
			
			while(offset=0){
				offset+=numRead;
			}
			if(offset!=readin.length){
				throw new IOException("can not read completely of file :"+file.getName());
			}
			in.close();
			return readin;
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println("Please select the running mode:");
		System.out.println("1.encrypt");
		System.out.println("2.decrypt");
		DES_N des=new DES_N();
		Scanner scanner = new Scanner(System.in);
		int mode;
		mode = scanner.nextInt();
		switch (mode) {
		case 1:{
			System.out.println("Input the encrypt file name");
			scanner.nextLine();
			String input=scanner.nextLine();
			System.out.println("Input the ciper file name");
			String cipername=scanner.nextLine();
			byte[] ciper = des.encrypt(input);
			//System.out.println(ciper.toString());
			System.out.println(ciper);
			
			//write the byte from the ciper into a document
			try{
				FileOutputStream out = new FileOutputStream(new File(cipername));
				out.write(ciper);
				out.close();
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			System.out.println("des decrypt"+new String(des.decryptFun(ciper)));
			break;
		
		}
			case 2:{
			System.out.println("Input the decrypt file");
			scanner.nextLine();
			String input = scanner.nextLine();
			System.out.println("Input the plain text name");
			String outname = scanner.nextLine();
			
			byte[] clear = des.readInbyte(input);
			clear = des.decryptFun(clear);
			System.out.println("decrypt content:  "+new String(clear));
			try{
				FileOutputStream outputStream = new FileOutputStream(new File(outname));
				outputStream.write(clear);
				outputStream.close();
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			break;
		}
		default:
			break;
		}
	}	
}

你可能感兴趣的:(DES加密算法的java实现(基于java类库))