一般常用的有:
MD5、SHA算法:代码如下
-
-
-
-
- package cn.com.jody.wsclient;
-
-
-
-
-
-
-
-
-
-
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.security.MessageDigest;
-
- public class EncryptCount {
-
-
-
-
-
-
-
- public static String getMD5(InputStream fis) {
-
- try {
- MessageDigest md = MessageDigest.getInstance("MD5");
- byte[] buffer = new byte[2048];
- int length = -1;
- while ((length = fis.read(buffer)) != -1) {
- md.update(buffer, 0, length);
- }
- return bytesToString(md.digest());
- } catch (Exception ex) {
- ex.printStackTrace();
- return null;
- } finally {
- try {
- fis.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
-
-
-
-
-
-
- public static String getSHA(InputStream fis) {
- try {
- MessageDigest md = MessageDigest.getInstance("SHA");
- byte[] buffer = new byte[2048];
- int length = -1;
- while ((length = fis.read(buffer)) != -1) {
- md.update(buffer, 0, length);
- }
- return bytesToString(md.digest());
- } catch (Exception ex) {
- ex.printStackTrace();
- return null;
- } finally {
- try {
- fis.close();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
-
-
-
-
-
-
-
- private static String bytesToString(byte[] data) {
- char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
- char[] temp = new char[data.length * 2];
- for (int i = 0; i < data.length; i++) {
- byte b = data[i];
- temp[i * 2] = hexDigits[b >>> 4 & 0x0f];
- temp[i * 2 + 1] = hexDigits[b & 0x0f];
- }
- return new String(temp);
- }
- }
/*
* Copyright (c) 2008
* All rights reserved.
*/
package cn.com.jody.wsclient;
/**
* <p>
* 功能描述 MD5\SHA加密类
* </p>
* <br>
* file name:EncryptCount .java<br>
* @author: JODY
* @Date: Jun 24, 2008
* @Time: 12:11:02 PM
*/
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
public class EncryptCount {
/**
* MD5
* @param fis
* @return
* String
*/
public static String getMD5(InputStream fis) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[2048];
int length = -1;
while ((length = fis.read(buffer)) != -1) {
md.update(buffer, 0, length);
}
return bytesToString(md.digest());
} catch (Exception ex) {
ex.printStackTrace();
return null;
} finally {
try {
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* SHA
* @param fis
* @return
* String
*/
public static String getSHA(InputStream fis) {
try {
MessageDigest md = MessageDigest.getInstance("SHA");
byte[] buffer = new byte[2048];
int length = -1;
while ((length = fis.read(buffer)) != -1) {
md.update(buffer, 0, length);
}
return bytesToString(md.digest());
} catch (Exception ex) {
ex.printStackTrace();
return null;
} finally {
try {
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
* 字节2字符串
* @param data
* @return
* String
*/
private static String bytesToString(byte[] data) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
char[] temp = new char[data.length * 2];
for (int i = 0; i < data.length; i++) {
byte b = data[i];
temp[i * 2] = hexDigits[b >>> 4 & 0x0f];
temp[i * 2 + 1] = hexDigits[b & 0x0f];
}
return new String(temp);
}
}
调用:
- public String encryptMD5(String password) {
- InputStream is = new ByteArrayInputStream(password.getBytes());
- String res = EncryptCount.getMD5(is);
- return res;
- }
public String encryptMD5(String password) {
InputStream is = new ByteArrayInputStream(password.getBytes());
String res = EncryptCount.getMD5(is);
return res;
}
加密前常常会转换成十六进制:
字符串转换成十六进制:
-
-
-
-
-
- static public String byteArrayToHexString(byte b[]) {
- String result = "";
- for (int i = 0; i < b.length; i++)
- result = result + byteToHexString(b[i]);
- return result;
- }
-
- static public String byteToHexString(byte b) {
- int n = b;
- if (n < 0)
- n = 256 + n;
- int d1 = n / 16;
- int d2 = n % 16;
- return HexCode[d1] + HexCode[d2];
- }
-
- static public String HexCode[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
- "9", "A", "B", "C", "D", "E", "F" };
/**
* byte数组转16进制字符串
* @param b
* @return
*/
static public String byteArrayToHexString(byte b[]) {
String result = "";
for (int i = 0; i < b.length; i++)
result = result + byteToHexString(b[i]);
return result;
}
static public String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return HexCode[d1] + HexCode[d2];
}
static public String HexCode[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "A", "B", "C", "D", "E", "F" };
在网络传输过程中需要用到加密解密的
Encrypt算法:
/*
* Copyright (c) 2008
* All rights reserved.
*/
package cn.com.jody.wsclient;
/**
* <p>
* 功能描述 加密解密类
* </p>
* <br>
* file name:Encrypt.java<br>
* @author: JODY
* @Date: Jun 20, 2008
* @Time: 12:11:02 PM
*/
public class Encrypt {
private static int MIN_ASC = 32;
private static int MAX_ASC = 126;
private static int NUM_ASC = MAX_ASC - MIN_ASC + 1;
private static long MYPRIMENUMBER = 100537;
private static long MYPRIMENUMBER2 = 100609;
private static String KEYWORD = "12345678"; //密匙
private static String decoder(String from_text) {
long key;
double offset;
int str_len;
int ch;
char[] word = from_text.toCharArray();
String to_text = "";
key = NumericPassword(KEYWORD);
str_len = from_text.length() - 1;
for (int i = 0; i < str_len; i++) {
word[i] = from_text.charAt(i);
ch = word[i];
if (ch >= MIN_ASC && ch <= MAX_ASC) {
i = i + 1;
ch = ch - MIN_ASC;
offset = (NUM_ASC + 1) * ((double) (((key * i) % MYPRIMENUMBER)) / (double) (MYPRIMENUMBER));
ch = ((ch - (int) (offset)) % NUM_ASC);
if (ch < 0)
ch = ch + NUM_ASC;
ch = ch + MIN_ASC;
i = i - 1;
to_text += (char) (ch);
}
}
return to_text;
}
private static String encoder(String from_text) {
long key;
double offset;
int str_len;
int ch;
char[] word = from_text.toCharArray();
String to_text = "";
key = NumericPassword(KEYWORD);
str_len = from_text.length() - 1;
for (int i = 0; i <= str_len; i++) {
word[i] = from_text.charAt(i);
ch = word[i];
if (ch >= MIN_ASC && ch <= MAX_ASC) {
i = i + 1;
ch = ch - MIN_ASC;
offset = (NUM_ASC + 1) * ((double) (((key * i) % MYPRIMENUMBER)) / (double) (MYPRIMENUMBER));
ch = ((ch + (int) (offset)) % NUM_ASC);
ch = ch + MIN_ASC;
i = i - 1;
to_text = to_text + (char) (ch);
}
}
return to_text + "a";
}
private static long NumericPassword(String password) {
long value, ch, shift1, shift2;
int str_len;
shift1 = 0;
shift2 = 0;
value = 0;
str_len = password.length();
for (int i = 0; i < str_len; i++) {
ch = password.charAt(i);
value = value ^ (ch * MyIndex(shift1));
value = value ^ (ch * MyIndex(shift2));
shift1 = (shift1 + 7) % 19;
shift2 = (shift2 + 13) % 23;
}
value = (value ^ MYPRIMENUMBER2) % MYPRIMENUMBER;
return value;
}
private static long MyIndex(long shadow) {
long i, j;
j = 1;
for (i = 1; i <= shadow; i++)
j = j * 2;
return j;
}
private static String base64Encoder(String str){
String returnstr = (new sun.misc.BASE64Encoder()).encode(str.getBytes());
//System.out.println(returnstr);
return returnstr;
}
private static String base64Decoder(String str) throws Exception{
String returnstr = new String((new sun.misc.BASE64Decoder()).decodeBuffer(str));
//System.out.println(returnstr);
return returnstr;
}
/**
* 加密
* @param from_text
* @return
* String
*/
public static String Cipher(String password){
password = Encrypt.encoder(password);
password = Encrypt.base64Encoder(password);
return password;
}
/**
* 解密
* @param from_text
* @return
* String
*/
public static String Decipher(String password) throws Exception{
password = Encrypt.base64Decoder(password);
password = Encrypt.decoder(password);
return password;
}
/**
* 调用实例
* @param args
* void
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String password ="abcdefghijklmnobqrstuvwxyz1234567890";
password = Encrypt.Cipher(password);
System.out.println(password);
password = Encrypt.Decipher(password);
System.out.println(password);
}
}
以上的几种是比较常用的加密方法。