- 浏览: 507146 次
- 性别:
- 来自: 山东
-
文章分类
最新评论
-
wjpiao:
...
HttpClient4模拟登录回贴 -
spring_springmvc:
可以参考最新的文档:如何在eclipse jee中检出项目并转 ...
Maven-pom.xml -Dependency Scope 区别 -
7454103:
京东通信此次推出的特权卡名为“自由行”,主打无套餐、无合约、无 ...
判断手机号码是否合法--正则 -
di1984HIT:
谢谢啊~学习了~
Maven Nexus 管理员密码重置 -
BigCat2013:
XMLFileUtil.getAttributeValue() ...
java调用sql XML配置文件 笔记
在JAVA开发过程中,不少地方运用关于加密的情况,
以下是我个人在以前的开发项目中运用到的关于MD5加密的代码,贴出来,以供大家参考,
如有更好的方法,请提出意见..
package com.util;
public class MD5 {
static final int S11 = 7;
static final int S12 = 12;
static final int S13 = 17;
static final int S14 = 22;
static final int S21 = 5;
static final int S22 = 9;
static final int S23 = 14;
static final int S24 = 20;
static final int S31 = 4;
static final int S32 = 11;
static final int S33 = 16;
static final int S34 = 23;
static final int S41 = 6;
static final int S42 = 10;
static final int S43 = 15;
static final int S44 = 21;
static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0 };
/* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中
被定义到MD5_CTX结构中
*/
private long[] state = new long[4]; // state (ABCD)
private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb first)
private byte[] buffer = new byte[64]; // input buffer
/* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的
16进制ASCII表示.
*/
public String digestHexStr;
/* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.
*/
private byte[] digest = new byte[16];
/*
getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串
返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.
*/
public String getMD5ofStr(String inbuf) {
md5Init();
md5Update(inbuf.getBytes(), inbuf.length());
md5Final();
digestHexStr = "";
for (int i = 0; i < 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
// 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数
public MD5() {
md5Init();
return;
}
/* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */
private void md5Init() {
count[0] = 0L;
count[1] = 0L;
///* Load magic initialization constants.
state[0] = 0x67452301L;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0x10325476L;
return;
}
/* F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是
简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们
实现成了private方法,名字保持了原来C中的。 */
private long F(long x, long y, long z) {
return (x & y) | ((~x) & z);
}
private long G(long x, long y, long z) {
return (x & z) | (y & (~z));
}
private long H(long x, long y, long z) {
return x ^ y ^ z;
}
private long I(long x, long y, long z) {
return y ^ (x | (~z));
}
/*
FF,GG,HH和II将调用F,G,H,I进行近一步变换
FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation.
*/
private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += F(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long GG(long a, long b, long c, long d, long x, long s, long ac) {
a += G(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long HH(long a, long b, long c, long d, long x, long s, long ac) {
a += H(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long II(long a, long b, long c, long d, long x, long s, long ac) {
a += I(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
/*
md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个
函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的
*/
private void md5Update(byte[] inbuf, int inputLen) {
int i, index, partLen;
byte[] block = new byte[64];
index = (int) (count[0] >>> 3) & 0x3F;
// /* Update number of bits */
if ((count[0] += (inputLen << 3)) < (inputLen << 3))
count[1]++;
count[1] += (inputLen >>> 29);
partLen = 64 - index;
// Transform as many times as possible.
if (inputLen >= partLen) {
md5Memcpy(buffer, inbuf, index, 0, partLen);
md5Transform(buffer);
for (i = partLen; i + 63 < inputLen; i += 64) {
md5Memcpy(block, inbuf, 0, i, 64);
md5Transform(block);
}
index = 0;
} else
i = 0;
///* Buffer remaining input */
md5Memcpy(buffer, inbuf, index, i, inputLen - i);
}
/*
md5Final整理和填写输出结果
*/
private void md5Final() {
byte[] bits = new byte[8];
int index, padLen;
///* Save number of bits */
Encode(bits, count, 8);
///* Pad out to 56 mod 64.
index = (int) (count[0] >>> 3) & 0x3f;
padLen = (index < 56) ? (56 - index) : (120 - index);
md5Update(PADDING, padLen);
///* Append length (before padding) */
md5Update(bits, 8);
///* Store state in digest */
Encode(digest, state, 16);
}
/* md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的
字节拷贝到output的outpos位置开始
*/
private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
int len) {
int i;
for (i = 0; i < len; i++)
output[outpos + i] = input[inpos + i];
}
/*
md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节
*/
private void md5Transform(byte block[]) {
long a = state[0], b = state[1], c = state[2], d = state[3];
long[] x = new long[16];
Decode(x, block, 64);
/* Round 1 */
a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */
d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */
b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */
a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */
d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */
c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */
b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */
/* Round 2 */
a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */
d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */
c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */
b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */
a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */
d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */
c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */
b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */
a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */
d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */
c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */
b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */
a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */
d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */
c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */
b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */
/* Round 3 */
a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */
d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */
c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */
b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */
a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */
d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */
c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */
b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */
a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */
d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */
c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */
b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */
a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */
d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */
c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */
b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */
/* Round 4 */
a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */
d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */
c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */
b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */
a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */
d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */
c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */
b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */
a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */
d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */
c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */
b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */
a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */
d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */
c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */
b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
/*Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的,
只拆低32bit,以适应原始C实现的用途
*/
private void Encode(byte[] output, long[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (byte) (input[i] & 0xffL);
output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
}
}
/*Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的,
只合成低32bit,高32bit清零,以适应原始C实现的用途
*/
private void Decode(long[] output, byte[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)
| (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
return;
}
/*
b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算
*/
public static long b2iu(byte b) {
return b < 0 ? b & 0x7F + 128 : b;
}
/*byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示,
因为java中的byte的toString无法实现这一点,我们又没有C语言中的
sprintf(outbuf,"%02X",ib)
*/
public static String byteHEX(byte ib) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] ob = new char[2];
ob[0] = Digit[(ib >>> 4) & 0X0F];
ob[1] = Digit[ib & 0X0F];
String s = new String(ob);
return s;
}
//加密
public static String toMD5(String source) {
MD5 md5 = new MD5();
return md5.getMD5ofStr(source).toLowerCase();
}
}
评论
public static String md5(String source) { StringBuffer sb = new StringBuffer(32); try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] array = md.digest(source.getBytes("utf-8")); for (int i = 0; i < array.length; i++) { sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3)); } } catch (Exception e) { logger.error("Can not encode the string '" + source + "' to MD5!", e); return null; } return sb.toString(); }
这是我用的加密代码.是用jdk自带功能完成的.

发表评论
-
easyui combobox下拉效果 可输入 模糊查询
2015-12-24 13:03 18217<link rel="stylesheet&q ... -
字符串编码生成实现
2015-08-06 09:28 853//为长度为22的字符串 编码生成实现 //格式:XAAA ... -
eclipse设置默认指向/src/main/webapp目录
2015-02-11 09:28 3507改配置文件, 打开.setting 文件夹下的 org.ecl ... -
判断手机号码是否合法--正则
2014-10-30 16:26 1752import java.util.regex.Matc ... -
JAVA集合
2014-09-30 13:04 708ArrayList和LinkedList区别 1.Ar ... -
乱码???
2014-09-12 16:57 1003http://blog.csdn.net/youyue/art ... -
APACHE+TOMCAT配置负载均衡
2014-09-09 09:37 1329目标: 使用 apache 和 tomcat 配置一个可以应 ... -
日期格式化取月日
2014-08-13 14:18 873日期格式化 SimpleDateFormat sdf = n ... -
java实现获取各种浏览器版本号及类型
2014-07-22 13:28 4666引用public Map<String,String&g ... -
两个数的百分比
2014-02-25 11:50 1059public static void main(String[ ... -
java补全
2014-02-21 13:27 1017import java.text.NumberFormat; ... -
oracle日期相减
2014-02-12 16:43 1541oracle日期相减 --MONTHS_BETWEEN(dat ... -
PermGen space及其解决方法
2014-01-23 10:00 1247-Xms256m -Xmx1024m -XX:PermSize ... -
webservcie-wsimport
2013-08-30 12:32 828wsimport wsimport是在JDK的bin目录下的 ... -
tomcat服务启动取IP4地址设置
2013-06-17 15:59 13281.Open launch configuration 2. ... -
Windows反向代理安装配置(双机)笔记
2013-06-07 09:05 3364引用 下载 httpd-2.2.22-win32-x86-no ... -
Maven-pom.xml -Dependency Scope 区别
2013-06-06 09:38 6104如下图报错,找错 1. 找 ... -
webservice调用异常
2013-06-03 16:27 1311Exception in thread "mai ... -
spring防止内存泄露配置
2013-05-07 15:43 897<!-- Spring 刷新Introspector ... -
删除字符串中带中文的指定字符
2013-04-23 08:50 1106public class Test { /** ...
相关推荐
这个名为"Java加密算法学习笔记的源码包"的压缩文件包含了关于Java中几种主要加密算法的实践代码和相关库,这对于理解和应用这些技术非常有帮助。 1. **Base64编码**: Base64是一种将二进制数据转化为可打印字符的...
【标题】"究极版.zip 本人笔记-已加密-不要下载" 提示我们这是一个包含个人笔记的压缩文件,而且已经进行了加密保护,这可能是为了保护作者的知识产权或隐私。通常,加密的ZIP文件用于存储敏感信息,确保只有知道...
这份资料集合了J2EE框架、MVC模式、线程管理、Struts框架、MD5加密算法以及Java常用类的使用等多个核心主题。下面将对这些关键知识点进行详细解释。 1. **J2EE**:Java 2 Platform, Enterprise Edition(J2EE)是...
标题 "有道云笔记内容下载java版,有道云笔记备份到本地" 指的是一项使用Java编程语言实现的功能,旨在将用户在有道云笔记中的内容下载并备份到本地计算机。这个功能的主要目标是确保用户的数据安全,防止因网络问题...
Apache Commons Digest是一个Java库,主要功能是提供各种散列(Hash)和消息认证码(MAC)算法的实现,这些算法包括MD5、SHA-1、SHA-256等。它在软件开发中广泛应用于数据校验、密码存储以及文件完整性检查等领域。...
- 消息摘要:如MD5(Message-Digest Algorithm 5)和SHA(Secure Hash Algorithm)家族,用于生成固定长度的唯一数据指纹,确保信息未被篡改。 - 消息认证码(MACs):MAC是通过共享密钥来验证消息完整性的方法,...
补充:MD5加密算法 - **MD5算法**:一种常用的哈希函数,用于加密存储密码。 ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public final static String MD5...
- **实现示例**:提供了一个简单的MD5加密实现示例,通过Java中的`MessageDigest`类实现。 通过以上内容,我们可以看到MVC模式的引入有效提升了系统的结构化程度和可维护性;同时,会话技术和加密算法的应用增强了...
- **安全措施**: 加密传输敏感信息,确保用户资金和个人信息安全。 ##### 4. 搜索功能 - **技术实现**: 采用搜索引擎技术(如Elasticsearch)构建全文检索系统,提高搜索效率和准确性。 - **功能扩展**: 支持...
【谷粒商城项目课程笔记】是一份详细的电商项目学习资料,涵盖了从项目设计到实现的全过程,旨在帮助学习者理解并掌握电商平台的开发技术。在这个压缩包中,我们可以找到一系列关于"谷粒商城"项目的笔记内容,这将是...
4. 加密解密:了解基本的加密算法,如MD5或SHA,以及如何在Java中实现。 5. 异常处理:学习如何在代码中添加异常处理,以确保程序的健壮性。 6. 用户输入验证:学会如何验证用户输入的密码,以提高程序的安全性。 7....
MD5是一种广泛使用的加密散列函数,可以对字符串进行加密。下面是一个简单的MD5加密方法: ```csharp public static string MD5(string strmsg) { string str = ""; try { MD5 md5 = new MD5...
Apache Shiro 是一个强大且易用的Java安全框架,提供了认证、授权、加密和会话管理功能,可以非常轻松地开发出足够安全的应用。Shiro 不仅可以用于Java Web 应用,也可以用于任何Java应用,如Java桌面应用或者...
这些文件可能包括Java类文件(.java或.class)、配置文件(如.xml或.properties)、数据库脚本(.sql)或者系统文档(如README或DESIGN.md)等。 综合以上信息,我们可以推测这个系统的核心架构可能包含以下几个...
5. **自己实现的工具类**:这可能是一个代码文件夹,包含了作者自行实现的一些实用工具类,如日志工具、线程池、加密解密工具等。通过实践编写这样的工具类,可以加深对编程语言特性和设计模式的理解。 6. **项目**...
- **README.md**或**readme.txt**: 可能包含了项目的安装、运行指南和开发者笔记。 项目的核心功能可能包括: 1. **用户管理**: 注册、登录、密码重置等功能。 2. **角色管理**: 创建、编辑和删除角色,以及分配...
Java提供了多种加密库,如`java.security.MessageDigest`用于MD5或SHA哈希,或使用Bcrypt、Argon2等更安全的算法。 3. **唯一性验证**:用户名和邮箱必须在数据库中是唯一的。在注册过程中,需要查询数据库确认新...
- `MD5.class`:这可能是一个实现MD5哈希算法的类,用于加密和验证用户密码或其他敏感信息,确保数据安全。 - `SysInfo.class`:系统信息类,可能包含了获取服务器环境信息或应用程序运行状态的功能。 - `SysUtil...