- 浏览: 149862 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
package net.iegames.crm.util; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; /** * MD5加密 */ public class Md5 { private static final int BUFFER_SIZE = 1024; private static final int S11 = 7; private static final int S12 = 12; private static final int S13 = 17; private static final int S14 = 22; private static final int S21 = 5; private static final int S22 = 9; private static final int S23 = 14; private static final int S24 = 20; private static final int S31 = 4; private static final int S32 = 11; private static final int S33 = 16; private static final int S34 = 23; private static final int S41 = 6; private static final int S42 = 10; private static final int S43 = 15; private static final int S44 = 21; private static byte padding[] = { (byte) 0x80, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; private InputStream in = null; private boolean stringp = false; private int state[] = null; private long count = 0; private byte buffer[] = null; private byte digest[] = null; public static String stringify(byte buf[]) { StringBuffer sb = new StringBuffer(2 * buf.length); for (int i = 0; i < buf.length; i++) { int h = (buf[i] & 0xf0) >> 4; int l = (buf[i] & 0x0f); sb.append(new Character((char) ((h > 9) ? 'a' + h - 10 : '0' + h))); sb.append(new Character((char) ((l > 9) ? 'a' + l - 10 : '0' + l))); } return sb.toString(); } private final int F(int x, int y, int z) { return ((x & y) | ((~x) & z)); } private final int G(int x, int y, int z) { return ((x & z) | (y & (~z))); } private final int H(int x, int y, int z) { return (x ^ y ^ z); } private final int I(int x, int y, int z) { return (y ^ (x | (~z))); } private final int rotate_left(int x, int n) { return ((x << n) | (x >>> (32 - n))); } private final int FF(int a, int b, int c, int d, int x, int s, int ac) { a += (F(b, c, d) + x + ac); a = rotate_left(a, s); a += b; return a; } private final int GG(int a, int b, int c, int d, int x, int s, int ac) { a += (G(b, c, d) + x + ac); a = rotate_left(a, s); a += b; return a; } private final int HH(int a, int b, int c, int d, int x, int s, int ac) { a += (H(b, c, d) + x + ac); a = rotate_left(a, s); a += b; return a; } private final int II(int a, int b, int c, int d, int x, int s, int ac) { a += (I(b, c, d) + x + ac); a = rotate_left(a, s); a += b; return a; } private final void decode(int output[], byte input[], int off, int len) { int i = 0; int j = 0; for (; j < len; i++, j += 4) { output[i] = (((int) (input[off + j] & 0xff)) | (((int) (input[off + j + 1] & 0xff)) << 8) | (((int) (input[off + j + 2] & 0xff)) << 16) | (((int) (input[off + j + 3] & 0xff)) << 24)); } } private final void transform(byte block[], int offset) { int a = state[0]; int b = state[1]; int c = state[2]; int d = state[3]; int x[] = new int[16]; decode(x, block, offset, 64); /* Round 1 */ a = FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */ d = FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */ c = FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */ b = FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */ a = FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */ d = FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */ c = FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */ b = FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */ a = FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */ d = FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */ c = FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ b = FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ a = FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ d = FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ c = FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ b = FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ /* Round 2 */ a = GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */ d = GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */ c = GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */ a = GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */ d = GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */ c = GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */ a = GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */ d = GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ c = GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */ b = GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */ a = GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */ c = GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */ b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ /* Round 3 */ a = HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */ d = HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */ c = HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ b = HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ a = HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */ d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */ c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */ b = HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ a = HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ d = HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */ c = HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */ b = HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */ a = HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */ d = HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ b = HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */ /* Round 4 */ a = II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */ d = II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */ c = II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ b = II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */ a = II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ d = II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */ c = II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ b = II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */ a = II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */ d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ c = II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */ b = II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ a = II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */ d = II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ c = II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */ b = II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */ state[0] += a; state[1] += b; state[2] += c; state[3] += d; } private final void update(byte input[], int len) { int index = ((int) (count >> 3)) & 0x3f; count += (len << 3); int partLen = 64 - index; int i = 0; if (len >= partLen) { System.arraycopy(input, 0, buffer, index, partLen); transform(buffer, 0); for (i = partLen; i + 63 < len; i += 64) { transform(input, i); } index = 0; } else { i = 0; } System.arraycopy(input, i, buffer, index, len - i); } private byte[] end() { byte bits[] = new byte[8]; for (int i = 0; i < 8; i++) { bits[i] = (byte) ((count >>> (i * 8)) & 0xff); } int index = ((int) (count >> 3)) & 0x3f; int padlen = (index < 56) ? (56 - index) : (120 - index); update(padding, padlen); update(bits, 8); return encode(state, 16); } // Encode the content.state array into 16 bytes array 加密啊。 private byte[] encode(int input[], int len) { byte output[] = new byte[len]; int i = 0; int j = 0; for (; j < len; i++, j += 4) { output[j] = (byte) ((input[i]) & 0xff); output[j + 1] = (byte) ((input[i] >> 8) & 0xff); output[j + 2] = (byte) ((input[i] >> 16) & 0xff); output[j + 3] = (byte) ((input[i] >> 24) & 0xff); } return output; } /** * Get the digest for our input stream. This method constructs the input * stream digest, and return it, as a a String, following the MD5 (rfc1321) * algorithm, * * @return An instance of String, giving the message digest. * @exception IOException * Thrown if the digestifier was unable to read the input * stream. */ public byte[] getDigest() throws IOException { byte buffer[] = new byte[BUFFER_SIZE]; int got = -1; if (digest != null) { return digest; } while ((got = in.read(buffer)) > 0) { update(buffer, got); } this.digest = end(); return digest; } /** * Get the digest, for this string digestifier. This method doesn't throw * any IOException, since it knows that the underlying stream ws built from * a String. * * @return the digest get from the string */ public byte[] processString() { if (!stringp) { throw new RuntimeException(this.getClass().getName() + "[processString]" + " not a string."); } try { return getDigest(); } catch (IOException ex) { } throw new RuntimeException(this.getClass().getName() + "[processString]" + ": implementation error."); } /** * Get the digest, as a proper string. * * @return a digest string */ public String getStringDigest() { if (digest == null) { throw new RuntimeException(this.getClass().getName() + "[getStringDigest]" + ": called before processing."); } return stringify(digest); } public Md5(byte[] buf) { this.stringp = true; this.in = new ByteArrayInputStream(buf); this.state = new int[4]; this.buffer = new byte[64]; this.count = 0; state[0] = 0x67452301; state[1] = 0xefcdab89; state[2] = 0x98badcfe; state[3] = 0x10325476; } /** * Construct a digestifier for the given string. * * @param input * The string to be digestified. * @param enc * the encoding name used (such as UTF8) */ public Md5(String input, String enc) { byte bytes[] = null; try { bytes = input.getBytes(enc); } catch (UnsupportedEncodingException e) { throw new RuntimeException("no " + enc + " encoding!!!"); } this.stringp = true; this.in = new ByteArrayInputStream(bytes); this.state = new int[4]; this.buffer = new byte[64]; this.count = 0; state[0] = 0x67452301; state[1] = 0xefcdab89; state[2] = 0x98badcfe; state[3] = 0x10325476; } /** * Construct a digestifier for the given string. * * @param input * The string to be digestified. */ public Md5(String input) { this(input, "UTF8"); } /** * Construct a digestifier for the given input stream. * * @param in * The input stream to be digestified. */ public Md5(InputStream in) { this.stringp = false; this.in = in; this.state = new int[4]; this.buffer = new byte[64]; this.count = 0; state[0] = 0x67452301; state[1] = 0xefcdab89; state[2] = 0x98badcfe; state[3] = 0x10325476; } public static String encode(String s){ Md5 md5 = new Md5("".getBytes()); byte b[]=null; try { b = md5.getDigest(); } catch (IOException e) { e.printStackTrace(); } return stringify(b); } public static void main(String args[]) throws IOException { System.out.println(Md5.encode("youyouhao")); } }
发表评论
-
获取同目录下的properties文件
2013-03-22 15:22 766InputStream in=UserHelper.cl ... -
java反射学习
2013-03-20 11:25 775http://zc0604.iteye.com/blog/1 ... -
装饰者与动态代理
2013-01-08 00:11 681package ch7; /** * @auth ... -
序列化与反序列化
2012-12-05 14:15 891import java.io.ByteArrayIn ... -
java回调
2012-11-21 18:29 773看过spring、hibernate源码的朋友对callbac ... -
http get post
2012-11-13 00:12 770package org.crazyit.net; ... -
HeadFirst设计模式笔记
2012-10-26 15:49 775设计原则 3、多用组合、少用继承 ... -
java解惑笔记
2012-10-12 14:32 792一、表达式谜题 1、x=x+i并不一定等于 ... -
java责任链 附件
2012-10-09 16:42 627原文: http://www.51testing.com/h ... -
读取property文件
2012-09-04 09:46 651import java.io.IOException; ... -
形参,实参
2012-06-04 14:25 990函数中修改形参的值对实参是没有影响的如下: 1.如果是其他类 ... -
环境变量设置
2012-05-16 22:57 737JAVA_HOME=G:\Program Files\Jav ... -
javamail
2012-05-15 17:25 721package com.util.mail; /** ... -
相差8小时处理解决方法
2011-07-02 17:09 881java(MyEclipse)相差8小时处理解决方法 Date ...
相关推荐
MD5(Message-Digest Algorithm 5)是一种广泛使用的哈希函数,它能够将任意长度的数据转化为固定长度的摘要,通常用于数据完整性校验和密码加密。在C#编程语言中,MD5加密也是一项常用的技术。下面我们将深入探讨C#...
然而,由于其存在碰撞漏洞(即不同的输入可能会产生相同的输出),MD5现在更多地被用于验证文件完整性,而非敏感数据的加密。 在Java中实现MD5加密通常涉及以下步骤: 1. 引入库:Java标准库提供`java.security....
在给定的"MD5加密.zip"文件中,可能包含了一个C#项目(MD5EnPWD.sln),该项目可能实现了一个MD5加密和解密的工具。`.sln`文件是Visual Studio的解决方案文件,它包含了项目的配置信息。`.v11.suo`是用户特定的解决...
在提供的压缩包文件中,`md5宏.xla`可能包含了一个宏的实现,而`MD5加密2种方式.xlsx`则可能是包含VBA函数或者演示如何使用VBA函数进行MD5加密的Excel工作簿。为了安全起见,使用外部来源的宏时应谨慎,确保其来源...
js宏
JavaScript中的MD5加密工具类是用于对数据进行安全哈希的一种方法,广泛应用于密码存储、数据完整性校验等场景。MD5(Message-Digest Algorithm 5)是一种广泛使用的哈希函数,它能将任意长度的输入转化为固定长度的...
相当于给excel加了一个md5()函数,使用时需要加一个lower()函数,可获得小写的32位md5加密字符串。 附2013版office加载方法:在选项中调开开发者工具,然后在开发工具中点击加载宏就可以了。
md5Encrypt16方法用于将输入字符串转换为16位的MD5加密结果,md5Encrypt32方法用于将输入字符串转换为32位的MD5加密结果,而md5EncryptBase64方法则是将输入字符串转换为MD5加密结果,然后将其转换为Base64编码的...
Python中的MD5加密是一种常用的哈希函数,用于对数据进行单向加密,广泛应用于密码存储、数据完整性校验等领域。MD5全称为Message-Digest Algorithm 5,它将任意长度的二进制数据映射为固定长度的128位(16字节)的...
在Java中,MD5加密可以通过特定的库或API来实现,这些库或API通常封装了MD5的计算过程,使得开发者能够方便地对字符串或其他数据进行加密。 "Md5加密jar包"可能包含了两种不同的MD5加密实现,这可能是因为每个jar包...
在Visual C++环境下实现MD5加密算法,通常涉及到以下几个关键知识点: 1. **MD5算法原理**:MD5由美国计算机科学家Ronald Rivest设计,它将任意长度的数据转化为固定长度的128位(16字节)摘要。这个过程通过四个...
在前端开发中,有时候为了增强数据安全性,可能需要在客户端对敏感信息进行MD5加密。jQuery是一个流行的JavaScript库,虽然它主要用于DOM操作和事件处理,但通过扩展,我们可以在jQuery环境下实现MD5加密。 首先,...
在描述中提到的"MD5加密解密demo",实际上MD5并不具备可逆的加密特性。MD5是一个单向函数,即给定任意输入,可以很容易地计算出固定的输出(摘要),但无法根据输出反推出原始输入。因此,我们通常不会说"MD5解密",...
MD5加密jar包是将MD5算法封装到Java程序中的库,方便开发者在Java应用中快速实现MD5加密功能。 MD5的特点: 1. **不可逆性**:MD5算法是单向的,即给定一个输入,可以得到一个固定长度的摘要,但不能通过摘要反推出...
2. **Delphi中的MD5库**:在Delphi中,可以使用第三方库如`dcutils`或`md5unit`来实现MD5加密。这些库通常提供了封装好的函数,如`MD5String`或`CalculateMD5`,可以直接接受字符串作为输入,返回MD5哈希值。例如,`...
在.NET Core 2.0中,我们可以利用内置的安全性和加密功能来实现MD5加密。以下将详细介绍如何在.NET Core 2.0环境下使用MD5进行数据加密。 首先,让我们了解MD5的基本原理。MD5的主要作用是生成一个数字指纹,这个...
pb开发的MD5加密源码 导入sru文件 nvo_md5 lv_md5 lv_md5 = create nvo_md5 ls_psw = lv_md5.of_encrypt(ls_psw) destroy lv_md5 输入:456aaa 输出:40e256c04310e3c606433e5ef39fbd3b
MD5.DLL是MD5加密算法的一个动态链接库,它封装了MD5的计算逻辑,供应用程序调用。在PowerBuilder中,我们可以通过引入外部DLL的方式来利用这些功能。以下是一个详细的步骤和知识点: 1. **引入MD5.DLL**:在Power...
本文将深入探讨MySQL和Microsoft SQL Server(简称MSSQL)中关于MD5加密语句的使用,帮助那些需要这方面知识的朋友更好地理解和应用。 首先,我们来看MySQL中的MD5加密。MySQL提供了一个内置的函数`MD5()`,它用于...
批量MD5加密工具,如"苏苏MD5批量加密工具.exe",是为了方便用户处理大量文件的MD5计算而设计的。这样的工具通常具有以下功能: 1. **多文件处理**:一次可以处理多个文件,节省了逐个手动计算的时间。 2. **快速...