/*
* @(#)DigestUtil.java Project:com.sinaapp.msdxblog.androidkit
* Date:2012-12-18
*
* Copyright (c) 2011 CFuture09, Institute of Software,
* Guangdong Ocean University, Zhanjiang, GuangDong, China.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lurencun.cfuture09.commons.security;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author Geek_Soledad (66704238@51uc.com)
*/
public class DigestUtil {
/**
* 获取一个字符串加密后的16进制值
*
* @param algorithm
* 摘要算法
* @param message
* 进行加密的String对象
*
* @return String 计算后的结果
*/
public static String doDigest(String algorithm, String message) {
return doDigest(algorithm, message.getBytes());
}
/**
* 获取一个字符串加密后的16进制值
*
* @param algorithm
* 摘要算法
* @param message
* 进行加密的byte数组
*
* @return String 计算后的结果
*/
public static String doDigest(String algorithm, byte[] message) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] result = md.digest(message);
return StringUtil.bytesToHexString(result);
}
/**
* 获取一个字符串加密后的16进制值
*
* @param algorithm
* 摘要算法名称
* @param message
* 进行加密的byte数组
*
* @return String 计算后的结果
*/
public static String doDigest(String algorithm, char[] message) {
return doDigest(algorithm, new String(message));
}
}
/*
* @(#)StringUtil.java Project:com.sinaapp.msdxblog.androidkit
* Date:2012-12-18
*
* Copyright (c) 2011 CFuture09, Institute of Software,
* Guangdong Ocean University, Zhanjiang, GuangDong, China.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lurencun.cfuture09.commons.security;
/**
* @author Geek_Soledad (66704238@51uc.com)
*/
public class StringUtil {
/**
* byte[]数组转换为16进制的字符串。
*
* @param data
* 要转换的字节数组。
* @return 转换后的结果。
*/
public static final String bytesToHexString(byte[] data) {
StringBuilder valueHex = new StringBuilder();
for (int i = 0, tmp; i < data.length; i++) {
tmp = data[i] & 0xff;
if (tmp < 16) {
valueHex.append(0);
}
valueHex.append(Integer.toHexString(tmp));
}
return valueHex.toString();
}
/**
* 16进制表示的字符串转换为字节数组。
*
* @param hexString
* 16进制表示的字符串
* @return byte[] 字节数组
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
char[] hexChars = hexString.toCharArray();
int length = hexString.length();
byte[] d = new byte[length >>> 1];
for (int n = 0; n < length; n += 2) {
String item = new String(hexChars, n, 2);
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节
d[n >>> 1] = (byte) Integer.parseInt(item, 16);
}
return d;
}
}
分享到:
相关推荐
md5加密技术封装的函数,可用在c或c++中,已测试使用过
C++ MD5加密封装类
附件包含一个.h文件和一个.cpp, 直接封装好了一个md5加密的类,直接调用即使用
python超级方便的md5加密,以封装。md5的值是不能反解的,那怎么判断现在的输入和数据库中加密的内容一致呢,只能将现在的输入加密,拿加密后的md5值和数据库中的md5值做判断,md5或许可以被黑客暴力破解
MD5加密封装 简单实用! 希望可以帮助各位大侠!!
vb中,md5加密程序封装德国大使馆是大方过是德国
MD5加密单独封装一个类,可以生成16位大写(默认)或小写的(需修改源码)的密钥串。与在线MD5加密结果一致。VS2005工程。
这个是以md5加密32位小写具体算法实例。可直接用于云mas的HTTP、和webservice接口。
加密解密md5的js封装代码
MD5加密封装,实用的加密算法功能。。。。。。。
包含MD5的 lib 需要的jar包,main类文件,文档,自己封装的jar包,比较详细
java中的md5加密函数,已经封装成类和函数了,非常好用。
用java封装的MD5加密工具包,创建对象,传递要加密的字符串,即可返回32位大写的MD5加密后的字符串
javascript md5加密算法库函数。封装了基本的HEX2MD5的加密算法函数,如需在js中调用,直接引入后调用函数即可。
iOS的MD5加密方法封装, 简单易懂易调用 供学习分享使用
本程序实现了md5加密,md5算法封装在dll中。有界面。
MD5加密类,Java对字符串加密,MD5加密方法
免积分下载超有用的app框架 自己总结整理的 包含activity栈管理消息管理 自己封装图片下载 和Imageloader第三方可供选择 封装MD5加密 封装联网解析接口回调 封装本地缓存机制 封装欢迎界面预读取下一个界面的数据 ...
这是一个MD5加密的算法,可以是16位,也可以是32位。算法不需C#的内核引用即可实现,是学习MD5算法的一个很好的例子。