本节讲述加密算法和加密算法的使用研究
如果有疑问请看源代码:
轻松一刻哦
o(∩_∩)o...哈哈
徘徊在牛A和牛B之间的人
o(∩_∩)o...哈哈
package cn.com.huawei.opensource.common.codecs;
import java.util.logging.Logger;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.language.Metaphone;
import org.apache.commons.codec.language.RefinedSoundex;
import org.apache.commons.codec.language.Soundex;
import org.apache.commons.codec.digest.DigestUtils;
/**
*Commons-codec组件设置各种编码是算法的信息
* @author bailonggang
* 2009-2-8
* 下午03:58:14
*/
public class CodeCUtil {
private static Logger logger=Logger.getLogger(CodeCUtil.class.getName());
/**
* MD5加密的过程的应用
* @param crbyte
* @return
*/
public static String encodeMD5(String crbyte)
{
byte[] bytes=crbyte.getBytes();
return DigestUtils.md5Hex(bytes);
}
/**
* MD5加密的过程的应用
* @param crbyte
* @return
*/
public static String encodeSHA1(String crbyte)
{
byte[] bytes=crbyte.getBytes();
return DigestUtils.shaHex(bytes);
}
/**
* 字符串的解码过程
* @param enbytes
* @return
*/
public static Object decodeBase64(String enbytes)
{
Base64 base64 = new Base64();
Object obj=null;
try {
obj = base64.decode(enbytes);
} catch (DecoderException e) {
logger.info("base64 decode ["+enbytes+"] error:"+e.getMessage());
}
return obj;
}
/**
* base64编码的过程的信息
* @param crbty
* @return
*/
public static String encodeBase64(String crbty)
{
Base64 base64 = new Base64();
byte[] enbytes =base64.encode(crbty.getBytes());
return new String(enbytes);
}
/**
* 十六进制加密的过程
* @param enbyte
* @return
* @throws DecoderException
*/
public static String encodeHex(String enbyte) throws DecoderException
{
char[] enbytes = Hex.encodeHex(enbyte.getBytes());
return new String(enbytes);
}
/**
* 十六进制解密的过程
* @param enbytes
* @return
* @throws DecoderException
*/
public static String decodeHex(String enbytes) throws DecoderException
{
byte[] bytes=Hex.decodeHex(enbytes.toCharArray());
return new String(bytes);
}
/**
* Metaphone 建立出相同的key给发音相似的单字, 比 Soundex 还要准确, 但是 Metaphone
* 没有固定长度, Soundex 则是固定第一个英文字加上3个数字. 这通常是用在类似音比对,
* 也可以用在 MP3 的软件开发.
*
*/
public static void languageEncoding()
{
Metaphone metaphone = new Metaphone();
RefinedSoundex refinedSoundex = new RefinedSoundex();
Soundex soundex = new Soundex();
for (int i = 0; i < 2; i++) {
String str = (i == 0) ? "resume" : "resin";
String mString = null;
String rString = null;
String sString = null;
try {
mString = metaphone.encode(str);
rString = refinedSoundex.encode(str);
sString = soundex.encode(str);
} catch (Exception ex){
;
}
System.out.println("Original:" + str);
System.out.println("Metaphone:" + mString);
System.out.println("RefinedSoundex:" + rString);
System.out.println("Soundex:" + sString + "\n");
}
}
}
如果有疑问请看源代码:
轻松一刻哦
o(∩_∩)o...哈哈
徘徊在牛A和牛B之间的人
o(∩_∩)o...哈哈
package cn.com.huawei.opensource.common.codecs;
import java.util.logging.Logger;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.language.Metaphone;
import org.apache.commons.codec.language.RefinedSoundex;
import org.apache.commons.codec.language.Soundex;
import org.apache.commons.codec.digest.DigestUtils;
/**
*Commons-codec组件设置各种编码是算法的信息
* @author bailonggang
* 2009-2-8
* 下午03:58:14
*/
public class CodeCUtil {
private static Logger logger=Logger.getLogger(CodeCUtil.class.getName());
/**
* MD5加密的过程的应用
* @param crbyte
* @return
*/
public static String encodeMD5(String crbyte)
{
byte[] bytes=crbyte.getBytes();
return DigestUtils.md5Hex(bytes);
}
/**
* MD5加密的过程的应用
* @param crbyte
* @return
*/
public static String encodeSHA1(String crbyte)
{
byte[] bytes=crbyte.getBytes();
return DigestUtils.shaHex(bytes);
}
/**
* 字符串的解码过程
* @param enbytes
* @return
*/
public static Object decodeBase64(String enbytes)
{
Base64 base64 = new Base64();
Object obj=null;
try {
obj = base64.decode(enbytes);
} catch (DecoderException e) {
logger.info("base64 decode ["+enbytes+"] error:"+e.getMessage());
}
return obj;
}
/**
* base64编码的过程的信息
* @param crbty
* @return
*/
public static String encodeBase64(String crbty)
{
Base64 base64 = new Base64();
byte[] enbytes =base64.encode(crbty.getBytes());
return new String(enbytes);
}
/**
* 十六进制加密的过程
* @param enbyte
* @return
* @throws DecoderException
*/
public static String encodeHex(String enbyte) throws DecoderException
{
char[] enbytes = Hex.encodeHex(enbyte.getBytes());
return new String(enbytes);
}
/**
* 十六进制解密的过程
* @param enbytes
* @return
* @throws DecoderException
*/
public static String decodeHex(String enbytes) throws DecoderException
{
byte[] bytes=Hex.decodeHex(enbytes.toCharArray());
return new String(bytes);
}
/**
* Metaphone 建立出相同的key给发音相似的单字, 比 Soundex 还要准确, 但是 Metaphone
* 没有固定长度, Soundex 则是固定第一个英文字加上3个数字. 这通常是用在类似音比对,
* 也可以用在 MP3 的软件开发.
*
*/
public static void languageEncoding()
{
Metaphone metaphone = new Metaphone();
RefinedSoundex refinedSoundex = new RefinedSoundex();
Soundex soundex = new Soundex();
for (int i = 0; i < 2; i++) {
String str = (i == 0) ? "resume" : "resin";
String mString = null;
String rString = null;
String sString = null;
try {
mString = metaphone.encode(str);
rString = refinedSoundex.encode(str);
sString = soundex.encode(str);
} catch (Exception ex){
;
}
System.out.println("Original:" + str);
System.out.println("Metaphone:" + mString);
System.out.println("RefinedSoundex:" + rString);
System.out.println("Soundex:" + sString + "\n");
}
}
}