• 字节数组java加密与解密


    这两天一直在查找字节数组之类的问题,今天正好有机会和大家共享一下.

    package com.wf.security;
    
    import java.security.Key;
    import java.security.Security;
    
    import javax.crypto.Cipher;
    
    /**
     * 加密密解类
     * @author wangfeng
     * @since 2013-4-27 15:50:26
     * @version 1.0
     *
     */
    public class EncryptionDecryption {
    	private static String strDefaultKey = "wfkey";
    	
    	/** 加密具工 */
    	private Cipher encryptCipher = null;
    	
    	/** 密解具工 */
    	private Cipher decryptCipher = null;
    	
    	/**
    	 * 将byte数组转换为表现16进制的字符串
    	 * @param arrB 须要转换的byte数组
    	 * @return 16进制表现的字符串
    	 * @throws Exception
    	 */
    	public static String byteArr2HexStr(byte[] arrB) throws Exception{
    		int bLen = arrB.length;
    		//每一个字符占用两个字节,所以字符串的度长需是数组度长的2倍
    		StringBuffer strBuffer = new StringBuffer(bLen*2);
    		for(int i=0; i != bLen; ++i){
    			int intTmp = arrB[i];
    			//把正数转化为正数
    			while(intTmp < 0){
    				intTmp = intTmp + 256;//因为字一个字节是8位,从低往高数,第9位为符号为,加256,相当于在第九位加1
    			}
    			//小于0F的数据须要在后面补0,(因为原来是一个字节,在现成变String是两个字节,如果小于0F的话,明说大最也盛不满第一个字节。第二个需弥补0)
    			if(intTmp < 16){
    				strBuffer.append("0");
    			}
    			strBuffer.append(Integer.toString(intTmp,16));
    		}
    		return strBuffer.toString();
    	}
    	
    	
    	/**
    	 * 将表现16进制的字符串转化为byte数组
    	 * @param hexStr
    	 * @return
    	 * @throws Exception
    	 */
    	public static byte[] hexStr2ByteArr(String hexStr) throws Exception{
    		byte[] arrB = hexStr.getBytes();
    		int bLen = arrB.length;
    		byte[] arrOut = new byte[bLen/2];
    		for(int i=0; i<bLen; i = i+2){
    			String strTmp = new String(arrB,i,2);
    			arrOut[i/2] = (byte)Integer.parseInt(strTmp,16);
    		}
    		return arrOut;
    	}
    	
    	/**
    	 * 认默构造器,应用认默密匙
    	 * @throws Exception
    	 */
    	public EncryptionDecryption() throws Exception {
    		this(strDefaultKey);
    	}
    
    	
    	
    	/**
    	 * 指定密匙构造方法
    	 * @param strKey 指定的密匙
    	 * @throws Exception
    	 */
    	   @SuppressWarnings("restriction")
    	public EncryptionDecryption(String strKey) throws Exception {
    	        Security.addProvider(new com.sun.crypto.provider.SunJCE());
    	        Key key = getKey(strKey.getBytes());
    
    	        encryptCipher = Cipher.getInstance("DES");
    	        encryptCipher.init(Cipher.ENCRYPT_MODE, key);
    
    	        decryptCipher = Cipher.getInstance("DES");
    	        decryptCipher.init(Cipher.DECRYPT_MODE, key);
    	    }
    
    	/**
    	 * 加密字节数组
    	 * @param arrB 需加密的字节数组
    	 * @return 加密后的字节数组
    	 * @throws Exception
    	 */
    	public byte[] encrypt(byte[] arrB) throws Exception{
    		return encryptCipher.doFinal(arrB);
    	}
    	
    	/**
    	 * 加密字符串
    	 * @param strIn 需加密的字符串
    	 * @return 加密后的字符串
    	 * @throws Exception
    	 */
    	public String encrypt(String strIn) throws Exception{
    		return byteArr2HexStr(encrypt(strIn.getBytes()));
    	}
    	
    	/**
    	 * 密解字节数组
    	 * @param arrB 需密解的字节数组
    	 * @return 密解后的字节数组
    	 * @throws Exception
    	 */
    	public byte[] decrypt(byte[] arrB) throws Exception{
    		return decryptCipher.doFinal(arrB);
    	}
    	
    	/**
    	 * 密解字符串
    	 * @param strIn 需密解的字符串
    	 * @return 密解后的字符串
    	 * @throws Exception
    	 */
    	public String decrypt(String strIn) throws Exception{
    		try{
    			return new String(decrypt(hexStr2ByteArr(strIn)));
    		}catch (Exception e) {
    			return "";
    		}
    	}
    	
    	
    	/**
    	 * 从指定字符串生成密匙,密匙所需的字节数组度长为8位,缺乏8位时,面后补0,超越8位时,只取后面8位
    	 * @param arrBTmp 成构字符串的字节数组
    	 * @return 生成的密匙
    	 * @throws Exception
    	 */
    	private Key getKey(byte[] arrBTmp) throws Exception{
    		byte[] arrB = new byte[8]; //认默为0
    		for(int i=0; i<arrBTmp.length && i < arrB.length; ++i){
    			arrB[i] = arrBTmp[i];
    		}
    		
    		//生成密匙
    		Key key = new javax.crypto.spec.SecretKeySpec(arrB,"DES");
    		return key;
    	}
    	
    }
        每日一道理
    岭上娇艳的鲜花,怎敌她美丽的容颜?山间清澈的小溪,怎比她纯洁的心灵?

        这里用DES算法,SUN还供提了别的算法。这里只是其中一种。

        测试代码:

    package com.wf.test;
    
    import org.junit.Test;
    
    import com.wf.security.EncryptionDecryption;
    
    public class EncryptionTest {
    	@Test
    	public void test() throws Exception{
    		EncryptionDecryption des = new EncryptionDecryption("wf");
    		String oldStr = "wangfeng";
    		String newStr = "";
    		newStr = des.encrypt(oldStr);
    		System.out.println("加密后:   "+newStr);
    		oldStr = "";//楚清老数据
    		oldStr = des.decrypt(newStr);
    		System.out.println("密解后:  "+oldStr);
    	}
    }

        输出信息:

        加密后:   d59c46653b72a6248e03aa55a8fdad6c
    密解后:  wangfeng

    文章结束给大家分享下程序员的一些笑话语录: 关于编程语言
    如果 C++是一把锤子的话,那么编程就会变成大手指头。
    如果你找了一百万只猴子来敲打一百万个键盘,那么会有一只猴子会敲出一 段 Java 程序,而其余的只会敲出 Perl 程序。
    一阵急促的敲门声,“谁啊!”,过了 5 分钟,门外传来“Java”。
    如果说 Java 很不错是因为它可以运行在所有的操作系统上,那么就可以说 肛交很不错,因为其可以使用于所有的性别上。

  • 相关阅读:
    插件式编程之使用反射分离MDI的父窗口和子窗口
    我总结的三层结构之一:Model示例
    我总结的三层结构之二:IDAL示例
    02.08 代理模式
    02.09 装饰模式
    块元素、行元素容纳规则
    CSS继承选择器与包含选择器的比较
    02.07 适配器模式
    02.10 桥模式
    匿名方法
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3049893.html
Copyright © 2020-2023  润新知