---
title: "javabean实现MD5加密"
author: "iammonster"
date: "2007-10-18T10:01:00Z"
tags: []
slug: "javabean-md5"
draft: false
lastmod: "2007-10-18T10:01:00Z"
id: 273291
url: "https://jiajun.iteye.com/blog/273291"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\** \*//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MD5 算法的Java Bean  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)@author:Topcat Tuppin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Last Modified:10,Mar,2001  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package com.shop.bean;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.lang.reflect.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\** \*//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)md5 类实现了RSA Data Security, Inc.在提交给IETF  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)的RFC1321中的MD5 message-digest 算法。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class MD5 ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\*\*//* 下面这些S11-S44实际上是一个4\*4的矩阵，在原始的C实现中是用#define 实现的，  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    这里把它们实现成为static final是表示了只读，切能在同一个进程空间内的多个  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    Instance间共享\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S11 = 7;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S12 = 12;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S13 = 17;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S14 = 22;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S21 = 5;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S22 = 9;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S23 = 14;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S24 = 20;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S31 = 4;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S32 = 11;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S33 = 16;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S34 = 23;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S41 = 6;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S42 = 10;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S43 = 15;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        static final int S44 = 21;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        static final byte\[] PADDING = ...{ -128, 0, 0, 0, 0, 0, 0, 0, 0,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        /\*\*//* 下面的三个成员是MD5计算过程中用到的3个核心数据，在原始的C实现中  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           被定义到MD5\_CTX结构中  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)         \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private long\[] state = new long\[4];  // state (ABCD)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private long\[] count = new long\[2];  // number of bits, modulo 2^64 (lsb first)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private byte\[] buffer = new byte\[64]; // input buffer  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\*\*//* digestHexStr是MD5的唯一一个公共成员，是最新一次计算结果的  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    　 16进制ASCII表示.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        public String digestHexStr;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        /\*\*//* digest,是最新一次计算结果的2进制内部表示，表示128bit的MD5值.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private byte\[] digest = new byte\[16];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      getMD5ofStr是类MD5最主要的公共方法，入口参数是你想要进行MD5变换的字符串  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      返回的是变换完的结果，这个结果是从公共成员digestHexStr取得的．  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        public String getMD5ofStr(String inbuf) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                md5Init();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                md5Update(inbuf.getBytes(), inbuf.length());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                md5Final();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                digestHexStr = "";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                for (int i = 0; i &lt; 16; i++) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                        digestHexStr += byteHEX(digest\[i]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                return digestHexStr;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 这是MD5这个类的标准构造函数，JavaBean要求有一个public的并且没有参数的构造函数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        public MD5() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                md5Init();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        /\*\*//* md5Init是一个初始化函数，初始化核心变量，装入标准的幻数 \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        private void md5Init() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                count\[0] = 0L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                count\[1] = 0L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                ///* Load magic initialization constants.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                state\[0] = 0x67452301L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                state\[1] = 0xefcdab89L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                state\[2] = 0x98badcfeL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                state\[3] = 0x10325476L;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        /\*\*//* F, G, H ,I 是4个基本的MD5函数，在原始的MD5的C实现中，由于它们是  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        简单的位运算，可能出于效率的考虑把它们实现成了宏，在java中，我们把它们  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     　　实现成了private方法，名字保持了原来C中的。 \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        private long F(long x, long y, long z) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                return (x & y) | ((~x) & z);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        private long G(long x, long y, long z) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                return (x & z) | (y & (~z));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        private long H(long x, long y, long z) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                return x ^ y ^ z;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        private long I(long x, long y, long z) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                return y ^ (x | (~z));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)       /\*\*//*   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          FF,GG,HH和II将调用F,G,H,I进行近一步变换  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          Rotation is separate from addition to prevent recomputation.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       \*/    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        private long FF(long a, long b, long c, long d,