天天看點

DES JAVA平台版本

本來JAVA的加密解密代碼在年前就已經完成了,可惜年前比較忙,沒來得及放上來,現在把整理過的代碼都放上來,有需要的趕緊來看把。

package base_crypt;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class base_crypt {

	/*********************************************
	 * ecb_base_encrypt("AES/ECB/PKCS5Padding","AES",s_buf,p_pass);
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 *********************************************/
	protected static byte[] ecb_base_encrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass) {
		try{
			Cipher cipher = Cipher.getInstance(p_formation);
			SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
			cipher.init(Cipher.ENCRYPT_MODE,keyspec);
			byte[] d_buf = cipher.doFinal(s_buf);
			return d_buf;
		} catch (InvalidKeyException | NoSuchAlgorithmException
				| NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*********************************************
	 * ecb_base_decrypt("AES/ECB/PKCS5Padding","AES",s_buf,p_pass);
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 *********************************************/
	protected static byte[] ecb_base_decrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass) {
		
		try{
			Cipher cipher = Cipher.getInstance(p_formation);
			SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
			cipher.init(Cipher.DECRYPT_MODE,keyspec);
			byte[] d_buf = cipher.doFinal(s_buf);
			return d_buf;
		} catch (InvalidKeyException | NoSuchAlgorithmException
				| NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*********************************************
	 * base_encrypt("AES/CBC/PKCS5Padding","AES",s_buf,p_pass,p_iv);
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 *********************************************/
	protected static byte[] base_encrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass,byte[] p_iv) {
		
		try{
			Cipher cipher = Cipher.getInstance(p_formation);
			SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
			IvParameterSpec iv = new IvParameterSpec(p_iv);
			cipher.init(Cipher.ENCRYPT_MODE,keyspec,iv);
			byte[] d_buf = cipher.doFinal(s_buf);
			return d_buf;
		} catch (InvalidKeyException | NoSuchAlgorithmException
				| NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException | InvalidAlgorithmParameterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*********************************************
	 * base_decrypt("AES/CBC/PKCS5Padding","AES",s_buf,p_pass,p_iv);
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws InvalidKeyException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 *********************************************/
	protected static byte[] base_decrypt(String p_formation,String p_algorithm,byte[] s_buf,byte[] p_pass,byte[] p_iv){
		try{
			Cipher cipher = Cipher.getInstance(p_formation);
			SecretKeySpec keyspec = new SecretKeySpec(p_pass, p_algorithm);
			IvParameterSpec iv = new IvParameterSpec(p_iv);
			cipher.init(Cipher.DECRYPT_MODE,keyspec,iv);
			byte[] d_buf = cipher.doFinal(s_buf);
			return d_buf;
		} catch (InvalidKeyException | NoSuchAlgorithmException
				| NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException | InvalidAlgorithmParameterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
           
package base_crypt;

public class base_des extends base_crypt{
	
	/* this class just provide the interface that crypt algorithm which paddinged with PKCS5Padding.*/
	
	private static String des_mode_ecb_pkcs5 = "DES/ECB/PKCS5Padding";	// mode ECB/CBC/OFB/CFB/CTR
	private static String des_mode_cbc_pkcs5 = "DES/CBC/PKCS5Padding";
	//private static String des_mode_cfb_pkcs5 = "DES/CFB/PKCS5Padding";	// java jdk is not surport
	//private static String des_mode_ofb_pkcs5 = "DES/OFB/PKCS5Padding";	// java jdk is not surport
	//private static String des_mode_ctr_pkcs5 = "DES/CTR/PKCS5Padding";	// java jdk is not surport
	
	private static String des_algorithm = "DES";
	
	private static String des_key_padding_str = "12345678";
	private static String des_iv_padding_str = "ABCDEFGH";
	
	private static byte[] default_iv = { 0x12, 0x34, 0x56, 0x78, 
		(byte) Integer.parseInt("90", 16),
		(byte) Integer.parseInt("AB", 16),
		(byte) Integer.parseInt("CD", 16),
		(byte) Integer.parseInt("EF", 16)};
	
	private static byte[] key_generator(String p_key){
		byte[] key = (p_key +des_key_padding_str).substring(0, 8).getBytes();
		return key;
	}
	private static byte[] iv_generator(String p_iv){
		byte[] iv = (p_iv + des_iv_padding_str).substring(0,8).getBytes();
		return iv;
	}
	
	public static byte[] ecb_des_encrypt(byte[] s_buf,byte[] p_pass){
		return ecb_base_encrypt(des_mode_ecb_pkcs5, des_algorithm,s_buf, p_pass);
	}
	public static byte[] ecb_des_decrypt(byte[] s_buf,byte[] p_pass){
		return ecb_base_decrypt(des_mode_ecb_pkcs5, des_algorithm, s_buf, p_pass);
	}
	public static byte[] ecb_des_encrypt(byte[] s_buf,String p_pass) {
		byte[] p_key = key_generator(p_pass);
		return ecb_base_encrypt(des_mode_ecb_pkcs5, des_algorithm, s_buf, p_key);
	}
	public static byte[] ecb_des_decrypt(byte[] s_buf,String p_pass){
		
		byte[] p_key = key_generator(p_pass);
		return ecb_base_decrypt(des_mode_ecb_pkcs5, des_algorithm, s_buf, p_key);
	}
	
	
	public static byte[] des_cbc_encrypt(byte[] s_buf,byte[] p_pass,byte[] p_iv){
		return base_encrypt(des_mode_cbc_pkcs5, des_algorithm, s_buf, p_pass, p_iv);
	}
	public static byte[] des_cbc_decrypt(byte[] s_buf,byte[] p_pass,byte[] p_iv) {
		return base_decrypt(des_mode_cbc_pkcs5, des_algorithm, s_buf, p_pass, p_iv);
	}
	public static byte[] des_cbc_encrypt(byte[] s_buf,byte[] p_pass){
		return base_encrypt(des_mode_cbc_pkcs5, des_algorithm, s_buf, p_pass, default_iv);
	}
	public static byte[] des_cbc_decrypt(byte[] s_buf,byte[] p_pass){
		return base_decrypt(des_mode_cbc_pkcs5, des_algorithm, s_buf, p_pass, default_iv);
	}
	public static byte[] des_cbc_encrypt(byte[] s_buf,String p_pass,String p_iv){
		byte[] key = key_generator(p_pass);
		byte[] iv = iv_generator(p_iv);
		return base_encrypt(des_mode_cbc_pkcs5, des_algorithm, s_buf, key, iv);
	}
	public static byte[] des_cbc_decrypt(byte[] s_buf,String p_pass,String p_iv){
		byte[] key = key_generator(p_pass);
		byte[] iv = iv_generator(p_iv);
		return base_decrypt(des_mode_cbc_pkcs5, des_algorithm, s_buf, key, iv);
	}
	public static byte[] des_cbc_encrypt(byte[] s_buf,String p_pass){
		byte[] p_key = key_generator(p_pass);
		return base_encrypt(des_mode_cbc_pkcs5, des_algorithm, s_buf, p_key,default_iv);
	}
	public static byte[] des_cbc_decrypt(byte[] s_buf,String p_pass){
		byte[] p_key = key_generator(p_pass);
		return base_decrypt(des_mode_cbc_pkcs5, des_algorithm, s_buf, p_key,default_iv);
	}

	
	
}