首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

字段加密的有关问题

2011-11-28 
字段加密的问题现在在做一个jsp页面,数据库用的是oracle,想在注册用户页面的时候把密码保存在user表中的pa

字段加密的问题
现在在做一个jsp页面,数据库用的是oracle,想在注册用户页面的时候把密码保存在user表中的password字段里,但想在此字段中显示的密码进行加密,请问这个应该怎么实现

[解决办法]

import java.security.*;


public class MD5_ok {

public final static String MD5(String s) {
char hexDigits[] = { '0 ', '1 ', '2 ', '3 ', '4 ', '5 ', '6 ', '7 ', '8 ', '9 ',
'a ', 'b ', 'c ', 'd ', 'e ', 'f ' };
try {
byte[] strTemp = s.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance( "MD5 ");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 > > > 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}

public static void main(String[] args) {
// MD5_Test aa = new MD5_Test();

System.out.print(MD5_ok.MD5( "gg "));
}
}



[解决办法]
接上:
private static void DES(byte Out[], byte In[], byte pSubKey[][], boolean Type) {
ByteToBit(M, In, 64);
Transform(M, M, IP_Table, 64);
if (Type == ENCRYPT) {
for (int i = 0; i < 16; ++i) {
byte Ri[] = new byte[32];
byte Li[] = new byte[32];
for (int j = 0; j < 32; j++) {
Ri[j] = tmp[j] = M[32 + j];
Li[j] = M[j];
}
F_func(Ri, pSubKey[i]);
Xor(Ri, Li, 32);
for (int j = 0; j < 32; j++) {
Li[j] = tmp[j];
}
for (int j = 0; j < 32; j++) {
M[32 + j] = Ri[j];
M[j] = Li[j];
}
}
} else {
for (int i = 15; i > = 0; --i) {
byte Ri[] = new byte[32];
byte Li[] = new byte[32];
for (int j = 0; j < 32; j++) {
Li[j] = tmp[j] = M[j];
Ri[j] = M[j + 32];
}
F_func(Li, pSubKey[i]);
Xor(Li, Ri, 32);
for (int j = 0; j < 32; j++) {
Ri[j] = tmp[j];
}
for (int j = 0; j < 32; j++) {
M[32 + j] = Ri[j];
M[j] = Li[j];
}
}
}
Transform(M, M, IPR_Table, 64);
BitToByte(Out, M, 64);
}

private static void SetSubKey(byte pSubKey[][], byte Key[]) {
ByteToBit(K, Key, 64);
Transform(K, K, PC1_Table, 56);
for (int i = 0; i < 16; ++i) {
RotateL(K, 28, LOOP_Table[i]);
byte b[] = new byte[36];
for (int j = 0; j < 36; j++) {
b[j] = K[28 + j];
}
RotateL(b, 28, LOOP_Table[i]);
for (int j = 0; j < 36; j++) {
K[28 + j] = b[j];
}
Transform(pSubKey[i], K, PC2_Table, 48);
}
}

private static void F_func(byte In[], byte Ki[]) {
Transform(MR, In, E_Table, 48);
Xor(MR, Ki, 48);
S_func(In, MR);
Transform(In, In, P_Table, 32);
}

private static void S_func(byte Out[], byte In[]) {
int in = 0, out = 0;
for (int i = 0, j, k; i < 8; ++i, in += 6, out += 4) {
j = (In[in + 0] < < 1) + In[in + 5];


k = (In[in + 1] < < 3) + (In[in + 2] < < 2) + (In[in + 3] < < 1) + In[in + 4];
byte b1[] = new byte[4];
byte b2[] = new byte[1];
b2[0] = S_Box[i][j][k];
ByteToBit(b1, b2, 4);
for (int m = 0; m < 4; m++) {
Out[m + out] = b1[m];
}
}
}

private static void Transform(byte Out[], byte In[], byte Table[], int len) {
for (int i = 0; i < len; ++i) {
Tmp[i] = In[Table[i] - 1];
}
for (int i = 0; i < len; i++) {
Out[i] = Tmp[i];
}
}

private static void Xor(byte InA[], byte InB[], int len) {
for (int i = 0; i < len; ++i) {
InA[i] ^= InB[i];
}
}

private static void RotateL(byte In[], int len, int loop) {
for (int i = 0; i < loop; i++) {
Tmp[i] = In[i];
}
for (int i = 0; i < len - loop; i++) {
In[i] = In[i + loop];
}
for (int i = 0; i < loop; i++) {
In[i + len - loop] = Tmp[i];
}
}

private static void ByteToBit(byte Out[], byte In[], int bits) {
for (int i = 0; i < bits; ++i) {
int out = (In[i > > 3] > > (i & 7)) & 1;
Out[i] = (byte) out;
}
}

private static void BitToByte(byte Out[], byte In[], int bits) {
for (int i = 0; i < (bits > > 3); i++) {
Out[i] = 0x00;
}
for (int i = 0; i < bits; ++i) {
Out[i > > 3] |= In[i] < < (i & 7);
}
}
/**
* 把加密后的数据转换成字符串,所生成的字符串只包含了从0--9和从A--F16个字符。
* Out: 转换后的输出缓冲区, 其大小是len*2;
* In: 待转换的数据缓冲区
* len: 待转换的数据的长度
*/
public static void ConvertCode(byte Out[], byte In[], int len) {
for (int i = 0; i < len; i++) {
Out[2*i] = ((In[i]> > 4)&15) < 10 ? (byte)(0x30+((In[i]> > 4)&15)):(byte)(0x37+((In[i]> > 4)&15));
Out[2*i+1] = (In[i]&15) < 10 ? (byte)(0x30+(In[i]&15)):(byte)(0x37+(In[i]&15));
}
}
/**
* 加密并转换字符
* @param Out: 加密转换后的输出缓冲区
* @param In: 待加密的字符缓冲区
* @param len: 待加密的字符缓冲区的长度
*
* @return: 加密转换后的输出缓冲区的长度
*/
public static int ImEncrypt(byte Out[], byte In[], int len) {
byte key[] = {0, 2, 0, 0, 9, 3, 5, 1, 9, 8, 0, 0, 9, 1, 7, 9};
byte buf[] = new byte[255];

for (int i=0; i <buf.length; i++) buf[i] = 0x00; //append 0x00 to tail of source

if (len == 0) {
len = 8;
for (int i=0; i <len; i++) buf[i] = (byte) (0x31 + i);
} else {
for (int i=0; i <len; i++) buf[i] = In[i];
}
int ret = Des_Go(buf, buf, len, key, key.length, ENCRYPT);
ConvertCode(Out, buf, ret);
return 2*ret;
}

public static String ImEncrypt(String plaint) {
String cypher = " ";
byte bPlaint[] = plaint.getBytes();
byte bCypher[] = new byte[32];

int ret = ImEncrypt(bCypher, bPlaint, plaint.length());

cypher = new String(bCypher, 0, ret);
return cypher;
}

/////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
byte key[] = {0, 2, 0, 0, 9, 3, 5, 1, 9, 8, 0, 0, 9, 1, 7, 9};
byte buf[] = new byte[255];
byte str[] = "webmaster ".getBytes();

for (int i = 0; i < buf.length; i++) {
buf[i] = 0x00;
}
int ret = ImEncrypt(buf, str, str.length);
System.out.println(new String(buf));



/*for (int i = 0; i < str.length; i++) {
buf[i] = str[i];
}
System.out.println( "Before encrypting ");
System.out.println(new String(buf));

int len = DesEncrypt.Des_Go(buf, buf, str.length, key, key.length, ENCRYPT);
byte out[] = new byte[len*2];
DesEncrypt.ConvertCode(out, buf, len);
System.out.println(new String(out));

System.out.println( "After encrypting ");
System.out.println(new String(buf));

DesEncrypt.Des_Go(buf, buf, str.length, key, key.length, DECRYPT);
System.out.println( "After decrypting ");
System.out.println(new String(buf));*/
}

----------------------------
调用加密方法即可:
String encrypt = DesEncrypt.ImEncrypt(Password.trim());//

热点排行