随笔-221  评论-148  文章-8  trackbacks-0

只贴代码,看官自己理解

package com.easylotto.lottobar.util;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import util.Hex;

public class SafeHelper {
    
/**
     * 进行DES加密
     * 
@param clearText String 明文
     * 
@param secretKey String 密钥
     * 
@return String 密文
     
*/
    
public static String encrypt(String srcMsg, String sKey) throws Exception{
        String encMsg;
        SecretKey secretKey;
        BASE64Encoder encoder 
= new BASE64Encoder();

        
try {
            SecretKeyFactory keyFac 
= SecretKeyFactory.getInstance("DES");
            DESKeySpec desKeySpec;

            desKeySpec   
= new DESKeySpec(sKey.getBytes());
            secretKey 
= keyFac.generateSecret(desKeySpec);

            Cipher desCipher 
= Cipher.getInstance("DES");
            desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

            
byte[] encText = desCipher.doFinal(srcMsg.getBytes());
            encMsg 
= encoder.encode(encText);
        } 
catch(Exception ex) {
            System.out.println(ex);
            
throw new Exception("无法进行加密!");
        }
        
return encMsg;

    }
    
/**
     * 进行DES解密
     * 
@param cipherText String 密文
     * 
@param secretKey String 密钥
     * 
@return String 原文
     
*/
    
public static String decrypt(String encMsg, String sKey) throws Exception {
        String srcMsg;
        SecretKey secretKey;
        BASE64Decoder decoder 
= new BASE64Decoder();

        
if(encMsg==null || encMsg.length()<=0throw new Exception("指定的密文格式不正确!");
        
try {
            SecretKeyFactory keyFac 
= SecretKeyFactory.getInstance("DES");
            DESKeySpec desKeySpec;

            desKeySpec   
= new DESKeySpec(sKey.getBytes());
            secretKey 
= keyFac.generateSecret(desKeySpec);

            Cipher desCipher 
= Cipher.getInstance("DES");
            desCipher.init(Cipher.DECRYPT_MODE, secretKey);

            
byte[] srcText = desCipher.doFinal(decoder.decodeBuffer(encMsg));
            srcMsg 
= new String(srcText);

        } 
catch(Exception ex) {
            
throw new Exception("无法对信息进行解密!");
        }
        
return srcMsg;
    }
    
/**
     * 对指定的文本进行MD5摘要
     * 
@param text String 文本
     * 
@return String 摘要
     
*/
    
public static String digest(String clearText) throws Exception {
        MessageDigest md 
= MessageDigest.getInstance("MD5");
        
byte[] digestText = md.digest(clearText.getBytes());

        BASE64Encoder encoder 
= new BASE64Encoder();
        
return encoder.encode(digestText).toString();
    }
    
/**
     * 对指定文本进行MD5摘要,但不进行Base64编码
     * 
@param clearText
     * 
@return
     * 
@throws Exception
     
*/
    
public static String digestNotBase64(String clearText) throws Exception {
        MessageDigest md 
= MessageDigest.getInstance("MD5");
        
byte[] digestText = md.digest(clearText.getBytes());
        
return new String(digestText);
    }

    
public static String digestHex(String clearText) throws Exception {
        MessageDigest md 
= MessageDigest.getInstance("MD5");
        
byte[] digestText = md.digest(clearText.getBytes());
        
byte[] bytes = Hex.encode(digestText);
        
return new String(bytes);
    }
    
/**
     * 对指定的文本进行BASE64编码
     * 
@param text String 文本
     * 
@return String 文本的BASE64编码
     
*/
    
public static String encode(String text) {
        BASE64Encoder encoder 
=new BASE64Encoder();
        
return encoder.encode(text.getBytes()).toString();
    }
    
/**
     * 对字节数组进行64位编码
     * 
@param bytes
     * 
@return
     
*/
    
public static String encodeBase64(byte[] bytes) {
        BASE64Encoder encoder 
=new BASE64Encoder();
        
return encoder.encode(bytes).toString();
    }
    
/**
     * 取得BASE64编码的原文
     * 
@param text String BASE64编码文本
     * 
@return String 原文
     
*/
    
public static String decode(String text) throws Exception {
        BASE64Decoder decoder 
=new BASE64Decoder();
        
return new String(decoder.decodeBuffer(text)).toString();
    }
    
public static String decode2Hex(String text) throws Exception {
        BASE64Decoder decoder 
=new BASE64Decoder();
        
byte[] ary_byte=decoder.decodeBuffer(text);
        
byte[] ary_Hexbyte=Hex.encode(ary_byte);
        
        
return new String(ary_Hexbyte);
    }    
    
public static void main(String args[]) throws Exception{
        
/*
        String pass="JI9g9d14/3Z91dYVEt1VlQ=="; //冯的账号数据库password字符串
        String passRemoveBase64=decode(pass); //去base64后 ,MD5密文
        String MD5_2time_str=digestHex(passRemoveBase64);
        System.out.println("passRemoveBase64->"+passRemoveBase64);
        System.out.println("2次MD冯的密码->"+MD5_2time_str);
        
*/
        String tmp
=digestHex(decode2Hex("4QrcOUm6Wau+VuBX8g+IPg=="));
        System.out.println(
"tmp-->"+tmp);
        
    }
}
16进制转换器
package util;


/**
 *  字节数组转换成16进制
*/

public class Hex
{
    
private static HexTranslator   encoder = new HexTranslator();

    
public static byte[] encode(
        
byte[]  array)
    {
        
return encode(array, 0, array.length);
    }

    
public static byte[] encode(
        
byte[]  array,
        
int     off,
        
int     length)
    {
        
byte[]      enc = new byte[length * 2];

        encoder.encode(array, off, length, enc, 
0);

        
return enc;
    }

    
public static byte[] decode(
        String  string)
    {
        
byte[]          bytes = new byte[string.length() / 2];
        String          buf 
= string.toLowerCase();

        
for (int i = 0; i < buf.length(); i += 2)
        {
            
char    left  = buf.charAt(i);
            
char    right = buf.charAt(i+1);
            
int     index = i / 2;

            
if (left < 'a')
            {
                bytes[index] 
= (byte)((left - '0'<< 4);
            }
            
else
            {
                bytes[index] 
= (byte)((left - 'a' + 10<< 4);
            }
            
if (right < 'a')
            {
                bytes[index] 
+= (byte)(right - '0');
            }
            
else
            {
                bytes[index] 
+= (byte)(right - 'a' + 10);
            }
        }

        
return bytes;
    }

    
public static byte[] decode(
        
byte[]  array)
    {
        
byte[]          bytes = new byte[array.length / 2];

        encoder.decode(array, 
0, array.length, bytes, 0);

        
return bytes;
    }
}



public class HexTranslator

    
implements Translator
{
    
private static final byte[]   hexTable =
        {
            (
byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
            (
byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
        };

    
/**
     * size of the output block on encoding produced by getDecodedBlockSize()
     * bytes.
     
*/
    
public int getEncodedBlockSize()
    {
        
return 2;
    }

    
public int encode(
        
byte[]  in,
        
int     inOff,
        
int     length,
        
byte[]  out,
        
int     outOff)
    {
        
for (int i = 0, j = 0; i < length; i++, j += 2)
        {
            out[outOff 
+ j] = hexTable[(in[inOff] >> 4& 0x0f];
            out[outOff 
+ j + 1= hexTable[in[inOff] & 0x0f];

            inOff
++;
        }

        
return length * 2;
    }

    
/**
     * size of the output block on decoding produced by getEncodedBlockSize()
     * bytes.
     
*/
    
public int getDecodedBlockSize()
    {
        
return 1;
    }

    
public int decode(
        
byte[]  in,
        
int     inOff,
        
int     length,
        
byte[]  out,
        
int     outOff)
    {
        
int halfLength = length / 2;
        
byte left, right;
        
for (int i = 0; i < halfLength; i++)
        {
            left  
= in[inOff + i * 2];
            right 
= in[inOff + i * 2 + 1];

            
if (left < (byte)'a')
            {
                out[outOff] 
= (byte)((left - '0'<< 4);
            }
            
else
            {
                out[outOff] 
= (byte)((left - 'a' + 10<< 4);
            }
            
if (right < (byte)'a')
            {
                out[outOff] 
+= (byte)(right - '0');
            }
            
else
            {
                out[outOff] 
+= (byte)(right - 'a' + 10);
            }

            outOff
++;
        }

        
return halfLength;
    }

posted on 2008-06-19 11:45 有猫相伴的日子 阅读(136) 评论(0)  编辑  收藏 所属分类: jdk

标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-07-01 17:41 编辑过
 
 

相关链接:
网站导航: