DWS 根据密钥的 加密解密算法

public class jiami implements Encrypt{
	private static String strDefaultKey;
	private Cipher decryptCipher = null;
	private Cipher encryptCipher = null;
	
	
	
	public jiami() throws Exception {
        this(strDefaultKey);
    }
	
	private Key getKey(byte[] arrBTmp) throws Exception {
        byte[] arrB = new byte[8];

        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;
    }
	
	 public jiami(String strKey) throws Exception {
	        if (strKey == null)
	            return;
	        java.security.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);
	    }
	 
	 public String jiami(String Key,String code){
		 	return encode(Key,code);
	 }
	 
	 public String encrypt(String strIn) throws Exception {
	        return byteArr2HexStr(encrypt(strIn.getBytes()));
	    }
	 
	 public byte[] encrypt(byte[] arrB) throws Exception {
	        return encryptCipher.doFinal(arrB);
	    }
	 
	 public static String byteArr2HexStr(byte[] arrB) throws Exception {
	        int iLen = arrB.length;
	        StringBuffer sb = new StringBuffer(iLen * 2);
	        for (int i = 0; i < iLen; i++) {
	            int intTmp = arrB[i];
	            while (intTmp < 0) {
	                intTmp = intTmp + 256;
	            }
	            if (intTmp < 16) {
	                sb.append("0");
	            }
	            sb.append(Integer.toString(intTmp, 16));
	        }
	        return sb.toString();
	    }
	
	
	public static String encode(String key, String str) {
        jiami des = null;
        try {
            des = new jiami(key);
            return des.encrypt(str);
        } catch (Exception ex) {
        }
        return "";
    }

	public String jiemi(String key, String str) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

package com.wk.des;

import java.security.Key;

import javax.crypto.Cipher;



public class jiemi implements Encrypt{
	private Cipher decryptCipher = null;
	private Cipher encryptCipher = null;
	 private static String strDefaultKey = null;
	
	
	public byte[] decrypt(byte[] arrB) throws Exception {
        return decryptCipher.doFinal(arrB);
    }
	public byte[] encrypt(byte[] arrB) throws Exception {
        return encryptCipher.doFinal(arrB);
    }

    public String encrypt(String strIn) throws Exception {
        return byteArr2HexStr(encrypt(strIn.getBytes()));
    }
    
    public static String byteArr2HexStr(byte[] arrB) throws Exception {
        int iLen = arrB.length;
        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; i++) {
            int intTmp = arrB[i];
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }
            if (intTmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }
	
	public jiemi() throws Exception {
        this(strDefaultKey);
    }
	
	 public jiemi(String strKey) throws Exception {
	        if (strKey == null)
	            return;
	        java.security.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);
	    }
	 
	 public String jiemi(String strKey,String code) throws Exception {
	        if (strKey == null)
	            return null;
	        java.security.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);
	         return decrypt(code);
	    }
	


    public String decrypt(String strIn) throws Exception {
        return new String(decrypt(hexStr2ByteArr(strIn)));
    }
    
    private Key getKey(byte[] arrBTmp) throws Exception {
        byte[] arrB = new byte[8];

        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;
    }
    
    public static String decode(String key, String str) {
        jiemi des = null;
        try {
            des = new jiemi(key);
            return des.decrypt(str);
        } catch (Exception ex) {
        }
        return "";
    }
    
    public static byte[] hexStr2ByteArr(String strIn) throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;

        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i = i + 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
        }
        return arrOut;
    }
	public String jiami(String key, String str) {
		// TODO Auto-generated method stub
		return null;
	}

	
    

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值