DES加密算法的实现

  我从网上找了很久,但始终没能找到使自己满意的加密算法。后来找到了一种比较简单的加密算法。但是网上的源码却是错误的。综合了一些网上的资料和自己的想法,改写出了一个满意的DES算法的java实现。源码如下:

package encryption;

import java.security.*;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.*;

/**
 *   使用DES加密与解密,可对byte[],String类型进行加密与解密
 *   密文可使用String,byte[]存储.
 */
public class encryption {
	public static void main(String[] args){
		encryption en=new encryption();
		String key="123";
		en.setKey(key);
		String str="爱我中华!";
		System.out.println(str);
		
		en.setEncString(str);
		String strmi=en.getStrMi();
		System.out.println(strmi);//输出加密后的密文
		
		en.setDesString(strmi);
		String strming=en.getStrMing();
		System.out.println(strming);//输出解密后的明文
		
	}
	
	private Key key;
	private byte[] bMi;
	private byte[] bMing;
	private String sMi;
	private String sMing;
	
	public encryption(){
		this.bMi=null;
		this.bMing=null;
		this.sMi="";
		this.sMing="";
	}
	
	//  根据参数生成KEY
	  public void setKey(String strKey){
	   try{
	        KeyGenerator generator = KeyGenerator.getInstance("DES");
	        generator.init(new SecureRandom(strKey.getBytes()));
	        this.key = generator.generateKey();
	        generator=null;
	        }
	    catch(Exception e){
	     e.printStackTrace();
	     }
	   }
	
	//  加密String明文输入,String密文输出  
	  public void setEncString(String strMing){
	   BASE64Encoder base64en = new BASE64Encoder();  
	    try {
	     this.bMing = strMing.getBytes("UTF8");  
	      this.bMi = this.getEncCode(this.bMing);  
	      this.sMi = base64en.encode(this.bMi);
	     }  
	    catch(Exception e)
	    {
	     e.printStackTrace();
	     }
	   finally
	     {
	      this.bMing = null;  
	      this.bMi = null;
	      }
	  }
	  
	  //加密以byte[]明文输入,byte[]密文输出    
	  private byte[] getEncCode(byte[] byteS){
	   byte[] byteFina = null;  
	    Cipher cipher;
	    try
	     {
	      cipher = Cipher.getInstance("DES");  
	      cipher.init(Cipher.ENCRYPT_MODE,key);
	      byteFina = cipher.doFinal(byteS);
	      }  
	    catch(Exception e)
	     {
	      e.printStackTrace();
	      }  
	    finally
	    {
	     cipher = null;
	     }
	       
	   return byteFina;
	  }
	  
	// 解密:以String密文输入,String明文输出   
	  public void setDesString(String strMi){  
	   BASE64Decoder base32De = new BASE64Decoder(); 
	    try
	    {
	    	//this.bMi=strMi.getBytes("UTF8");
	      this.bMi = base32De.decodeBuffer(strMi);
	      this.bMing = this.getDesCode(bMi);
	      this.sMing = new String(bMing,"UTF8");
	      }
	    catch(Exception e)
	     {
	      e.printStackTrace();
	      }  
	    finally
	     {
	      //base32De = null;  
	      //bMing = null;  
	      //bMi = null;
	      }
	  }
	  
	// 解密以byte[]密文输入,以byte[]明文输出    
	  private byte[] getDesCode(byte[] byteD){
	    Cipher cipher;  
	     byte[] byteFina=null;  
	     try{
	      cipher = Cipher.getInstance("DES");  
	       cipher.init(Cipher.DECRYPT_MODE,key);  
	       byteFina = cipher.doFinal(byteD);
	       }
	    catch(Exception e)
	      {
	       e.printStackTrace();
	       }
	    finally
	      {
	       cipher=null;
	       }  
	     return byteFina;
	   } 
	  
	  //返回加密后的密文strMi  
	  public String getStrMi()
	  {
	   return this.sMi;
	  }
	  //返回解密后的明文
	  public String getStrMing()
	  {
	   return this.sMing;
	  }
}


如果只是直接复制粘贴可能会出现错误,需要对编译器进行一些简单的设置。如:

在MyEclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示: 
Access restriction : The type BASE64Decoder is not accessible due to restriction on required library C:\Program 
files\java\jre6\lib\rt.jar 
Access restriction : The constructor BASE64Decoder() is not accessible due to restriction on required library C:\Program files\java\jre6\lib\rt.jar 

解决方案1(推荐): 
只需要在project build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。 
解决方案2: 
Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -> 
Deprecated and trstricted API -> Forbidden reference (access rules): -> change to warning 

你可能感兴趣的:(算法概论)