java开发中常用的算法_总结一下项目开发过程中常用的到的一些加密算法。
一般常用的有:
MD5、SHA算法:代碼如下
Java代碼
/*
*?Copyright?(c)?2008
*?All?rights?reserved.
*/
packagecn.com.jody.wsclient;
/**
*?
*?功能描述?MD5\SHA加密類
*?
*?
*?file?name:EncryptCount?.java
*?@author:?JODY
*?@Date:?Jun?24,?2008
*?@Time:?12:11:02?PM
*/
importjava.io.IOException;
importjava.io.InputStream;
importjava.security.MessageDigest;
publicclassEncryptCount?{
/**
*?MD5
*?@param?fis
*?@return
*?String
*/
publicstaticString?getMD5(InputStream?fis)?{
try{
MessageDigest?md?=?MessageDigest.getInstance("MD5");
byte[]?buffer?=newbyte[2048];
intlength?=?-1;
while((length?=?fis.read(buffer))?!=?-1)?{
md.update(buffer,0,?length);
}
returnbytesToString(md.digest());
}catch(Exception?ex)?{
ex.printStackTrace();
returnnull;
}finally{
try{
fis.close();
}catch(IOException?ex)?{
ex.printStackTrace();
}
}
}
/**
*?SHA
*?@param?fis
*?@return
*?String
*/
publicstaticString?getSHA(InputStream?fis)?{
try{
MessageDigest?md?=?MessageDigest.getInstance("SHA");
byte[]?buffer?=newbyte[2048];
intlength?=?-1;
while((length?=?fis.read(buffer))?!=?-1)?{
md.update(buffer,0,?length);
}
returnbytesToString(md.digest());
}catch(Exception?ex)?{
ex.printStackTrace();
returnnull;
}finally{
try{
fis.close();
}catch(IOException?ex)?{
ex.printStackTrace();
}
}
}
/**
*?字節2字符串
*?@param?data
*?@return
*?String
*/
privatestaticString?bytesToString(byte[]?data)?{
charhexDigits[]?=?{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char[]?temp?=newchar[data.length?*2];
for(inti?=0;?i?
byteb?=?data[i];
temp[i?*2]?=?hexDigits[b?>>>4&0x0f];
temp[i?*2+1]?=?hexDigits[b?&0x0f];
}
returnnewString(temp);
}
}
/*
* Copyright (c) 2008
* All rights reserved.
*/
package cn.com.jody.wsclient;
/**
*
* 功能描述 MD5\SHA加密類
*
*
* file name:EncryptCount .java
* @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);
}
}
調用:
Java代碼
publicString?encryptMD5(String?password)?{
InputStream?is?=newByteArrayInputStream(password.getBytes());
String?res?=?EncryptCount.getMD5(is);
returnres;
}
public String encryptMD5(String password) {
InputStream is = new ByteArrayInputStream(password.getBytes());
String res = EncryptCount.getMD5(is);
return res;
}
加密前常常會轉換成十六進制:
字符串轉換成十六進制:
Java代碼
/**
*?byte數組轉16進制字符串
*?@param?b
*?@return
*/
staticpublicString?byteArrayToHexString(byteb[])?{
String?result?="";
for(inti?=0;?i?
result?=?result?+?byteToHexString(b[i]);
returnresult;
}
staticpublicString?byteToHexString(byteb)?{
intn?=?b;
if(n?<0)
n?=256+?n;
intd1?=?n?/16;
intd2?=?n?%16;
returnHexCode[d1]?+?HexCode[d2];
}
staticpublicString?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算法:
Java代碼
/*
*?Copyright?(c)?2008
*?All?rights?reserved.
*/
packagecn.com.jody.wsclient;
/**
*?
*?功能描述?加密解密類
*?
*?
*?file?name:Encrypt.java
*?@author:?JODY
*?@Date:?Jun?20,?2008
*?@Time:?12:11:02?PM
*/
publicclassEncrypt?{
privatestaticintMIN_ASC?=32;
privatestaticintMAX_ASC?=126;
privatestaticintNUM_ASC?=?MAX_ASC?-?MIN_ASC?+1;
privatestaticlongMYPRIMENUMBER?=100537;
privatestaticlongMYPRIMENUMBER2?=100609;
privatestaticString?KEYWORD?="12345678";//密匙
privatestaticString?decoder(String?from_text)?{
longkey;
doubleoffset;
intstr_len;
intch;
char[]?word?=?from_text.toCharArray();
String?to_text?="";
key?=?NumericPassword(KEYWORD);
str_len?=?from_text.length()?-1;
for(inti?=0;?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);
}
}
returnto_text;
}
privatestaticString?encoder(String?from_text)?{
longkey;
doubleoffset;
intstr_len;
intch;
char[]?word?=?from_text.toCharArray();
String?to_text?="";
key?=?NumericPassword(KEYWORD);
str_len?=?from_text.length()?-1;
for(inti?=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);
}
}
returnto_text?+"a";
}
privatestaticlongNumericPassword(String?password)?{
longvalue,?ch,?shift1,?shift2;
intstr_len;
shift1?=0;
shift2?=0;
value?=0;
str_len?=?password.length();
for(inti?=0;?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;
returnvalue;
}
privatestaticlongMyIndex(longshadow)?{
longi,?j;
j?=1;
for(i?=1;?i?<=?shadow;?i++)
j?=?j?*2;
returnj;
}
privatestaticString?base64Encoder(String?str){
String?returnstr?=?(newsun.misc.BASE64Encoder()).encode(str.getBytes());
//System.out.println(returnstr);
returnreturnstr;
}
privatestaticString?base64Decoder(String?str)throwsException{
String?returnstr?=newString((newsun.misc.BASE64Decoder()).decodeBuffer(str));
//System.out.println(returnstr);
returnreturnstr;
}
/**
*?加密
*?@param?from_text
*?@return
*?String
*/
publicstaticString?Cipher(String?password){
password?=?Encrypt.encoder(password);
password?=?Encrypt.base64Encoder(password);
returnpassword;
}
/**
*?解密
*?@param?from_text
*?@return
*?String
*/
publicstaticString?Decipher(String?password)throwsException{
password?=?Encrypt.base64Decoder(password);
password?=?Encrypt.decoder(password);
returnpassword;
}
/**
*?調用實例
*?@param?args
*?void
*?@throws?Exception
*/
publicstaticvoidmain(String[]?args)throwsException?{
String?password?="abcdefghijklmnobqrstuvwxyz1234567890";
password?=?Encrypt.Cipher(password);
System.out.println(password);
password?=?Encrypt.Decipher(password);
System.out.println(password);
}
}
/*
* Copyright (c) 2008
* All rights reserved.
*/
package cn.com.jody.wsclient;
/**
*
* 功能描述 加密解密類
*
*
* file name:Encrypt.java
* @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);
}
}
以上的幾種是比較常用的加密方法。
總結
以上是生活随笔為你收集整理的java开发中常用的算法_总结一下项目开发过程中常用的到的一些加密算法。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 科目一技巧总结
- 下一篇: [小代码]通过IP和端口连接到远程摄像机