A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 侯伟浩 黑马帝   /  2011-12-22 12:00  /  3504 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 侯伟浩 于 2011-12-23 09:37 编辑

怎么对一个数据加密?加密后怎么取回原来的数据?

评分

参与人数 1技术分 +1 收起 理由
杨强 + 1

查看全部评分

7 个回复

倒序浏览
Md5加密原理
    MD5以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法的输出由四个32位分组组成,将这四个32位分组级联后将生成一个128位散列值。
    在MD5算法中,首先需要对信息进行填充,使其字节长度对512求余数的结果等于448。因此,信息的字节长度(Bits Length)将被扩展至N*512+448,即N*64+56个字节(Bytes),N为一个正整数。填充的方法如下,在信息的后面填充一个1和无数个0,直到满足上面的条件时才停止用0对信息的填充。然后再在这个结果后面附加一个以64位二进制表示的填充前的信息长度。经过这两步的处理,现在的信息字节长度=N*512+448+64=(N+1)*512,即长度恰好是512的整数倍数。这样做的原因是为满足后面处理中对信息长度的要求。MD5中有四个32位被称作链接变量(Chaining Variable)的整数参数,他们分别为:A=0x01234567,B=0x89abcdef,C=0xfedcba98,D=0x76543210。 当设置好这四个链接变量后,就开始进入算法的四轮循环运算,循环的次数是信息中512位信息分组的数目。
     将上面四个链接变量复制到另外四个变量中:A到a,B到b,C到c,D到d。 主循环有四轮(MD4只有三轮),每轮循环都很相似。第一轮进行16次操作。每次操作对a、b、c和d中的其中三个作一次非线性函数运算,然后将所得结果加上第四个变量(文本中的一个子分组和一个常数)。
     再将所得结果向右环移一个不定的数,并加上a、b、c或d中之一。最后用该结果取代a、b、c或d中之一。 以一下是每次操作中用到的四个非线性函数(每轮一个)。
  F(X,Y,Z)=(X∧Y)∨(( X)∧Z)
   G(X,Y,Z)=(X∧Z)∨(Y∧( Z))
   H(X,Y,Z)=X⊕Y⊕Z
  I(X,Y,Z)=Y⊕(X∨( Z))
其中,⊕是异或,∧是与,∨是或, 是反符号。如果X、Y和Z的对应位是独立和均匀的,那么结果的每一位也应是独立和均匀的。F是一个逐位运算的函数。即,如果X,那么Y,否则Z。函数H是逐位奇偶操作符。所有这些完成之后,将A,B,C,D分别加上a,b,c,d。然后用下一分组数据继续运行算法,最后的输出是A,B,C和D的级联。最后得到的A,B,C,D就是输出结果,A是低位,D为高位,DCBA组成128位输出结果。

评分

参与人数 1技术分 +1 收起 理由
杨强 + 1

查看全部评分

回复 使用道具 举报
不要讲那么多理论,有代码加详细注释最好

评分

参与人数 1黑马币 +1 收起 理由
王德云 + 1 非常赞同!!

查看全部评分

回复 使用道具 举报
package csl.md5;
import java.lang.reflect.*;
/*************************************************
keyBean 类实现了RSA Data Security, Inc.在提交给IETF
的RFC1321中的keyBean message-digest 算法。
*************************************************/
public class keyBean {
/* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的,
这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个
Instance间共享*/
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 };
/* 下面的三个成员是keyBean计算过程中用到的3个核心数据,在原始的C实现中
被定义到keyBean_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是keyBean的唯一一个公共成员,是最新一次计算结果的
  16进制ASCII表示.
*/
public String digestHexStr;
/* digest,是最新一次计算结果的2进制内部表示,表示128bit的keyBean值.
*/
private byte[] digest = new byte[16];
/*
getkeyBeanofStr是类keyBean最主要的公共方法,入口参数是你想要进行keyBean变换的字符串
返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.
*/
public String getkeyBeanofStr(String inbuf) {
keyBeanInit();
keyBeanUpdate(inbuf.getBytes(), inbuf.length());
keyBeanFinal();
digestHexStr = "";
for (int i = 0; i < 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
// 这是keyBean这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数
public keyBean() {
keyBeanInit();
return;
}

/* keyBeanInit是一个初始化函数,初始化核心变量,装入标准的幻数 */
private void keyBeanInit() {
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个基本的keyBean函数,在原始的keyBean的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;
}
/*
keyBeanUpdate是keyBean的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个
函数由getkeyBeanofStr调用,调用之前需要调用keyBeaninit,因此把它设计成private的
*/
private void keyBeanUpdate(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) {
keyBeanMemcpy(buffer, inbuf, index, 0, partLen);
keyBeanTransform(buffer);
for (i = partLen; i + 63 < inputLen; i += 64) {
keyBeanMemcpy(block, inbuf, 0, i, 64);
keyBeanTransform (block);
}
index = 0;
} else
i = 0;
///* Buffer remaining input */
keyBeanMemcpy(buffer, inbuf, index, i, inputLen - i);
}
/*
keyBeanFinal整理和填写输出结果
*/
private void keyBeanFinal () {
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);
keyBeanUpdate (PADDING, padLen);
///* Append length (before padding) */
keyBeanUpdate(bits, 8);
///* Store state in digest */
Encode (digest, state, 16);
}
/* keyBeanMemcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的
      字节拷贝到output的outpos位置开始
*/
private void keyBeanMemcpy (byte[] output, byte[] input,
int outpos, int inpos, int len)
{
int i;
for (i = 0; i < len; i++)
output[outpos + i] = input[inpos + i];
}
/*
keyBeanTransform是keyBean核心变换程序,有keyBeanUpdate调用,block是分块的原始字节
*/
private void keyBeanTransform (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;
}

一次发送不完,分两次发

评分

参与人数 1技术分 +2 收起 理由
杨强 + 2

查看全部评分

回复 使用道具 举报
本帖最后由 陈帅雷 于 2011-12-22 14:19 编辑

[*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 & 0xffL);
output[j + 1] = (byte)((input >>>8) & 0xffL);
output[j + 2] = (byte)((input >>>16) & 0xffL);
output[j + 3] = (byte)((input >>>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 = 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 void main(String args[]) {

keyBean m = new keyBean();
if
(Array.getLength(args) == 0)
{ //如果没有参数,执行标准的Test Suite
System.out.println("keyBean Test suite:");
System.out.print(m.getkeyBeanofStr("woaiguqianmeng"));
}
else
System.out.println("keyBean(" + args[0] + ")=" + m.getkeyBeanofStr(args[0]));

}
}

以上是MD5加密算法的实现,也可以直接使用。我有这个实现的java类, keyBean.rar (3.98 KB, 下载次数: 186)

评分

参与人数 1技术分 +2 收起 理由
杨强 + 2

查看全部评分

回复 使用道具 举报
不知道我写的是不是加密?请高手们指点迷津。
  1. package md5.demo;

  2. import java.security.MessageDigest;
  3. import java.util.Scanner;

  4. public class MD5Demo {

  5.         private final static String[] hexDigits = {"0", "1", "2", "3", "4",     
  6.                         "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};     

  7.         public static void main(String[] args) throws Exception {

  8.                 Scanner sc=new Scanner(System.in);
  9.                         System.out.println("输入用户名:");
  10.                         String inputName=sc.next();
  11.                         System.out.println("输入密码:");
  12.                         String inputPass=sc.next();
  13.                         boolean flage=isTrue(inputName, inputPass);
  14.                         if(flage)
  15.                         {
  16.                                 System.out.println("登陆成功!");
  17.                         }
  18.                         else
  19.                         {
  20.                                 System.out.println("登录失败!");
  21.                         }
  22.                
  23.         }
  24.         /**
  25.          * 模拟从数据库中查找加密后的姓名
  26.          * @return
  27.          * @throws Exception
  28.          */
  29.         public static String findName() throws Exception {
  30.                 String name="侯伟浩";
  31.                 String md5Name=md5Code(name);
  32.                 return md5Name;
  33.         }
  34.         /**
  35.          * 模拟从数据库中查找加密后的密码
  36.          * @return
  37.          * @throws Exception
  38.          */
  39.         public static String findPassWord() throws Exception
  40.         {
  41.                 String password="123456";
  42.                 String md5Pass=md5Code(password);
  43.                 return md5Pass;
  44.         }
  45.         /*
  46.          * 得到加密后的source
  47.          */
  48.         public static String md5Code(String source)throws Exception {
  49.                 StringBuilder sb=new StringBuilder();
  50.                 MessageDigest md5=MessageDigest.getInstance("MD5");
  51.                 byte[] by=md5.digest(source.getBytes());
  52.                 for(int i=0;i<by.length;i++)
  53.                 {
  54.                         sb.append(toHexString(by[i]));
  55.                 }
  56.                 return sb.toString();
  57.         }
  58.         /**
  59.          * 转成16进制的方法
  60.          * @param by
  61.          * @return
  62.          */
  63.         public static String toHexString(byte by)
  64.         {
  65.                 StringBuilder sb=new StringBuilder();
  66.                 int source=by;
  67.                 for(int i=0;i<8;i++)
  68.                 {
  69.                         int temp=(source>>>i*4)&0xf;
  70.                         sb.insert(0, hexDigits[temp]);
  71.                 }
  72.                 return sb.toString();
  73.         }
  74.         /**
  75.          * 判断是否查找到
  76.          * @param inputName
  77.          * @param inputPass
  78.          * @return
  79.          * @throws Exception
  80.          */
  81.         public static boolean isTrue(String inputName,String inputPass) throws Exception
  82.         {
  83.                 boolean flage=false;
  84.                 if(md5Code(inputName).equals(findName())&&md5Code(inputPass).equals(findPassWord()))
  85.                 {
  86.                         flage=true;
  87.                 }
  88.                 return flage;
  89.         }

  90. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨强 + 1

查看全部评分

回复 使用道具 举报
侯伟浩 黑马帝 2011-12-23 09:36:45
7#
我已经学会了,主要是用digest方法对数据加密。昨天成功写出了一个web工程,id和密码加密后存入数据库,登录的时候再去进行判断。
很高兴又学会了一门新技术。
回复 使用道具 举报
张绍成 黑马帝 2011-12-23 20:40:41
8#
MD5  对数据进行加密 是一种不可逆的行为(据说是可以破解,不过要花费很大代价)。
在 java中已经有了 对 MD5 的支持:
代码如下:


  1. import java.security.MessageDigest;

  2. public class MD5 {

  3.         public static String md5(String text)
  4.         {
  5.            if(text == null)
  6.             return "";
  7.            StringBuffer hexString = new StringBuffer();
  8.            try
  9.            {
  10.           
  11.             MessageDigest md = MessageDigest.getInstance("MD5");
  12.             md.update(text.getBytes());

  13.             byte[] digest = md.digest();
  14.           
  15.             for(int i = 0;i <digest.length;i++)
  16.             {
  17.              text = Integer.toHexString(0xFF&digest[i]);
  18.              if(text.length()<2)
  19.              {
  20.               text = "0"+text;
  21.              }
  22.              hexString.append(text);
  23.             }
  24.            }catch(Exception e)
  25.            {
  26.             e.printStackTrace();
  27.            }
  28.            return hexString.toString();
  29.         }
  30. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马