javamd5加密源碼
import java.security.*;
import javax.crypto.*;
/**
* 本例解釋如何利用DES私鑰加密演算法加解密
*
* @author Devon
* @version 1.0 04/03/10
*/
public class SingleKeyExample {
public static void main(String[] args) {
try {
String algorithm = "DES"; //定義加密演算法,可用 DES,DESede,Blowfish
String message = "Hello World. 這是待加密的信息";
// 生成個DES密鑰
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
keyGenerator.init(56); //選擇DES演算法,密鑰長度必須為56位
Key key = keyGenerator.generateKey(); //生成密鑰
// 生成Cipher對象
Cipher cipher = Cipher.getInstance("DES");
//用密鑰加密明文(message),生成密文(cipherText)
cipher.init(Cipher.ENCRYPT_MODE, key); //操作模式為加密(Cipher.ENCRYPT_MODE),key為密鑰
byte[] cipherText = cipher.doFinal(message.getBytes()); //得到加密後的位元組數組
System.out.println("加密後的信息: " + new String(cipherText));
//用密鑰加密明文(plainText),生成密文(cipherByte)
cipher.init(Cipher.DECRYPT_MODE, key); //操作模式為解密,key為密鑰
byte[] sourceText = cipher.doFinal(cipherText); //獲得解密後位元組數組
System.out.println("解密後的信息: " + new String(sourceText));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**
* @author Devon
*/
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
public class PairKeyExample {
public static void main(String argv[]) {
try {
String algorithm = "RSA"; //定義加密演算法,可用 DES,DESede,Blowfish
String message = "張三,你好,我是李四";
//產生張三的密鑰對(keyPairZhang)
KeyPairGenerator keyGeneratorZhang =
KeyPairGenerator.getInstance(algorithm); //指定採用的演算法
keyGeneratorZhang.initialize(1024); //指定密鑰長度為1024位
KeyPair keyPairZhang = keyGeneratorZhang.generateKeyPair(); //產生密鑰對
System.out.println("生成張三的公鑰對");
// 張三生成公鑰(publicKeyZhang)並發送給李四,這里發送的是公鑰的數組位元組
byte[] publicKeyZhangEncode = keyPairZhang.getPublic().getEncoded();
//通過網路或磁碟等方式,把公鑰編碼傳送給李四
//李四接收到張三編碼後的公鑰,將其解碼
KeyFactory keyFacoryLi = KeyFactory.getInstance(algorithm); //得到KeyFactory對象
X509EncodedKeySpec x509KeySpec =
new X509EncodedKeySpec(publicKeyZhangEncode); //公鑰採用X.509編碼
PublicKey publicKeyZhang = keyFacoryLi.generatePublic(x509KeySpec); //將公鑰的KeySpec對象轉換為公鑰
System.out.println("李四成功解碼,得到張三的公鑰");
//李四用張三的公鑰加密信息,並發送給李四
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); //得到Cipher對象
cipher.init(Cipher.ENCRYPT_MODE, publicKeyZhang); //用張三的公鑰初始化Cipher對象
byte[] cipherMessage = cipher.doFinal(message.getBytes()); //得到加密信息
System.out.println("加密後信息:" + new String(cipherMessage));
System.out.println("加密完成,發送給李四...");
//張三用自己的私鑰解密從李四處收到的信息
cipher.init(Cipher.DECRYPT_MODE, keyPairZhang.getPrivate()); //張三用其私鑰初始化Cipher對象
byte[] originalMessage = cipher.doFinal(cipherMessage); //得到解密後信息
System.out.println("張三收到信息,解密後為:" + new String(originalMessage));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
② java md5加密怎麼實現源碼 視頻介紹下
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
private MessageDigest md;
private static MD5 md5;
private MD5()
{
try {
md=MessageDigest.getInstance("md5");
} catch (NoSuchAlgorithmException e) {
System.out.println("沒有這種演算法");
}
}
//產生一個MD5實例
public static MD5 getInstance()
{
if(null!=md5)
return md5;
else
{
makeInstance();
return md5;
}
}
//保證同一時間只有一個線程在使用MD5加密
private static synchronized void makeInstance()
{
if(null==md5)
md5=new MD5();
}
public String createMD5(String pass)
{
md.update(pass.getBytes());
byte[] b=md.digest();
return byteToHexString(b);
}
private String byteToHexString(byte[] b)
{
StringBuffer sb=new StringBuffer();
String temp="";
for(int i=0;i<b.length;i++)
{
temp=Integer.toHexString(b[i]&0Xff);
if(temp.length()==1)
temp="0"+temp;
sb.append(temp);
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(MD5.getInstance().createMD5("123")); }}
③ JAVA簡單文件加密 求JAVA源代碼
md5加密:
package com.ncs.pki.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Test {
private static MessageDigest digest = null;
public synchronized static final String hash(String data) {
if (digest == null) {
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsae) {
System.err.println(
"Failed to load the MD5 MessageDigest. "
+ "Jive will be unable to function normally.");
nsae.printStackTrace();
}
}
// Now, compute hash.
digest.update(data.getBytes());
return encodeHex(digest.digest());
}
public static final String encodeHex(byte[] bytes) {
StringBuffer buf = new StringBuffer(bytes.length * 2);
int i;
for (i = 0; i < bytes.length; i++) {
if (((int) bytes[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString((int) bytes[i] & 0xff, 16));
}
return buf.toString();
}
public static String test(){
return null;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(MD5Test.hash("123456"));
}
}
3des加密:
package com.ncs.pki.util;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DesEncrypt {
/**
*
* 使用DES加密與解密,可對byte[],String類型進行加密與解密 密文可使用String,byte[]存儲.
*
* 方法: void getKey(String strKey)從strKey的字條生成一個Key
*
* String getEncString(String strMing)對strMing進行加密,返回String密文 String
* getDesString(String strMi)對strMin進行解密,返回String明文
*
*byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]
* byteD)byte[]型的解密
*/
Key key;
/**
* 根據參數生成KEY
*
* @param strKey
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密String明文輸入,String密文輸出
*
* @param strMing
* @return
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文輸入,String明文輸出
*
* @param strMi
* @return
*/
public String getDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文輸入,byte[]密文輸出
*
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
/**
* 解密以byte[]密文輸入,以byte[]明文輸出
*
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}
public static void main(String[] args) {
System.out.println("des demo");
DesEncrypt des = new DesEncrypt();// 實例化一個對像
des.getKey("MYKEY");// 生成密匙
System.out.println("key=MYKEY");
String strEnc = des.getEncString("111111");// 加密字元串,返回String的密文
System.out.println("密文=" + strEnc);
String strDes = des.getDesString(strEnc);// 把String 類型的密文解密
System.out.println("明文=" + strDes);
}
}
④ 如何使用Java生成MD5代碼
這是我以前做的一個小項目時用到md5寫的
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//將用戶密碼進行md5加密 並返回加密後的32位十六進制密碼
public class MD5Util {
public static String md5(String password) {
try {
// 獲取md5對象
MessageDigest md = MessageDigest.getInstance("md5");
// 獲取加密後的密碼並返回十進制位元組數組
byte[] bytes = md.digest(password.getBytes());
// 遍歷數組得到每個十進制數並轉換成十六進制
StringBuffer sb = new StringBuffer();
for (byte b : bytes) {
// 把每個數轉成十六進制 存進字元中
sb.append(toHex(b));
}
String finish = sb.toString();
return finish;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
// 十進制轉十六進制方法
private static String toHex(byte b) {
int target = 0;
if (b < 0) {
target = 255 + b;
} else {
target = b;
}
int first = target / 16;
int second = target % 16;
return Hex[first] + Hex[second];
}
static String[] Hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f" };
/*public static void main(String[] args) {
String a = MD5Util.md5("1234");
System.out.println(a);
}*/
}
⑤ java怎麼把字元串進行md5加密
給你看源代碼,我自己寫的
public static String md5(String src){
try{
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] output = md.digest(src.getBytes());//加密處理
//將加密結果output利用Base64轉換成字元串輸出
String ret = Base64.encodeBase64String(output);
return ret;
}catch(Exception e){
throw new NoteException("密碼加密失敗",e);
}
}
public static void main(String[] args) {
System.out.println(md5("123456"));
}
⑥ 求Java的MD5加密解密實現類。 要實現對用戶的密碼進行加密! 然後驗證用戶的密碼!
我簡單說下吧,加密就是存進資料庫的時候變成MD5存進去,解密,就是對比的時候,將用戶輸入的密碼轉換成MD5和資料庫裡面的對比。
⑦ 可變MD5加密(Java實現)
可變在這里含義很簡單 就是最終的加密結果是可變的 而非必需按標准MD 加密實現 Java類庫security中的MessageDigest類就提供了MD 加密的支持 實現起來非常方便 為了實現更多效果 我們可以如下設計MD 工具類
Java代碼
package ** ** util;
import java security MessageDigest;
/**
* 標准MD 加密方法 使用java類庫的security包的MessageDigest類處理
* @author Sarin
*/
public class MD {
/**
* 獲得MD 加密密碼的方法
*/
public static String getMD ofStr(String origString) {
String origMD = null;
try {
MessageDigest md = MessageDigest getInstance( MD );
byte[] result = md digest(origString getBytes());
origMD = byteArray HexStr(result);
} catch (Exception e) {
e printStackTrace();
}
return origMD ;
}
/**
* 處理位元組數組得到MD 密碼的方法
*/
private static String byteArray HexStr(byte[] bs) {
StringBuffer *** = new StringBuffer();
for (byte b : bs) {
*** append(byte HexStr(b));
}
return *** toString();
}
/**
* 位元組標准移位轉十六進制方法
*/
private static String byte HexStr(byte b) {
String hexStr = null;
int n = b;
if (n < ) {
//若需要自定義加密 請修改這個移位演算法即可
n = b & x F + ;
}
hexStr = Integer toHexString(n / ) + Integer toHexString(n % );
return hexStr toUpperCase();
}
/**
* 提供一個MD 多次加密方法
*/
public static String getMD ofStr(String origString int times) {
String md = getMD ofStr(origString);
for (int i = ; i < times ; i++) {
md = getMD ofStr(md );
}
return getMD ofStr(md );
}
/**
* 密碼驗證方法
*/
public static boolean verifyPassword(String inputStr String MD Code) {
return getMD ofStr(inputStr) equals(MD Code);
}
/**
* 重載一個多次加密時的密碼驗證方法
*/
public static boolean verifyPassword(String inputStr String MD Code int times) {
return getMD ofStr(inputStr times) equals(MD Code);
}
/**
* 提供一個測試的主函數
*/
public static void main(String[] args) {
System out println( : + getMD ofStr( ));
System out println( : + getMD ofStr( ));
System out println( sarin: + getMD ofStr( sarin ));
System out println( : + getMD ofStr( ));
}
}
可以看出實現的過程非常簡單 因為由java類庫提供了處理支持 但是要清楚的是這種方式產生的密碼不是標準的MD 碼 它需要進行移位處理才能得到標准MD 碼 這個程序的關鍵之處也在這了 怎麼可變?調整移位演算法不就可變了么!不進行移位 也能夠得到 位的密碼 這就不是標准加密了 只要加密和驗證過程使用相同的演算法就可以了
MD 加密還是很安全的 像CMD 那些窮舉破解的只是針對標准MD 加密的結果進行的 如果自定義移位演算法後 它還有效么?可以說是無解的了 所以MD 非常安全可靠
為了更可變 還提供了多次加密的方法 可以在MD 基礎之上繼續MD 就是對 位的第一次加密結果再MD 恩 這樣去破解?沒有任何意義
這樣在MIS系統中使用 安全可靠 歡迎交流 希望對使用者有用
我們最後看看由MD 加密演算法實現的類 那是非常龐大的
Java代碼
import java lang reflect *;
/**
* **********************************************
* md 類實現了RSA Data Security Inc 在提交給IETF
* 的RFC 中的MD message digest 演算法
* ***********************************************
*/
public class MD {
/* 下面這些S S 實際上是一個 * 的矩陣 在原始的C實現中是用#define 實現的
這里把它們實現成為static final是表示了只讀 切能在同一個進程空間內的多個
Instance間共享*/
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final int S = ;
static final byte[] PADDING = {
};
/* 下面的三個成員是MD 計算過程中用到的 個核心數據 在原始的C實現中
被定義到MD _CTX結構中
*/
private long[] state = new long[ ]; // state (ABCD)
private long[] count = new long[ ]; // number of bits molo ^ (l *** first)
private byte[] buffer = new byte[ ]; // input buffer
/* digestHexStr是MD 的唯一一個公共成員 是最新一次計算結果的
進制ASCII表示
*/
public String digestHexStr;
/* digest 是最新一次計算結果的 進制內部表示 表示 bit的MD 值
*/
private byte[] digest = new byte[ ];
/*
getMD ofStr是類MD 最主要的公共方法 入口參數是你想要進行MD 變換的字元串
返回的是變換完的結果 這個結果是從公共成員digestHexStr取得的.
*/
public String getMD ofStr(String inbuf) {
md Init();
md Update(inbuf getBytes() inbuf length());
md Final();
digestHexStr = ;
for (int i = ; i < ; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
// 這是MD 這個類的標准構造函數 JavaBean要求有一個public的並且沒有參數的構造函數
public MD () {
md Init();
return;
}
/* md Init是一個初始化函數 初始化核心變數 裝入標準的幻數 */
private void md Init() {
count[ ] = L;
count[ ] = L;
///* Load magic initialization constants
state[ ] = x L;
state[ ] = xefcdab L;
state[ ] = x badcfeL;
state[ ] = x L;
return;
}
/* F G H I 是 個基本的MD 函數 在原始的MD 的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 and
Rotation is separate from addition to prevent reputation
*/
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 >>> ( 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 >>> ( 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 >>> ( 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 >>> ( s));
a += b;
return a;
}
/*
md Update是MD 的主計算過程 inbuf是要變換的位元組串 inputlen是長度 這個
函數由getMD ofStr調用 調用之前需要調用md init 因此把它設計成private的
*/
private void md Update(byte[] inbuf int inputLen) {
int i index partLen;
byte[] block = new byte[ ];
index = (int) (count[ ] >>> ) & x F;
// /* Update number of bits */
if ((count[ ] += (inputLen << )) < (inputLen << ))
count[ ]++;
count[ ] += (inputLen >>> );
partLen = index;
// Transform as many times as possible
if (inputLen >= partLen) {
md Memcpy(buffer inbuf index partLen);
md Transform(buffer);
for (i = partLen; i + < inputLen; i += ) {
md Memcpy(block inbuf i );
md Transform(block);
}
index = ;
} else
i = ;
///* Buffer remaining input */
md Memcpy(buffer inbuf index i inputLen i);
}
/*
md Final整理和填寫輸出結果
*/
private void md Final() {
byte[] bits = new byte[ ];
int index padLen;
///* Save number of bits */
Encode(bits count );
///* Pad out to mod
index = (int) (count[ ] >>> ) & x f;
padLen = (index < ) ? ( index) : ( index);
md Update(PADDING padLen);
///* Append length (before padding) */
md Update(bits );
///* Store state in digest */
Encode(digest state );
}
/* md Memcpy是一個內部使用的byte數組的塊拷貝函數 從input的inpos開始把len長度的
位元組拷貝到output的outpos位置開始
*/
private void md Memcpy(byte[] output byte[] input int outpos int inpos int len) {
int i;
for (i = ; i < len; i++)
output[outpos + i] = input[inpos + i];
}
/*
md Transform是MD 核心變換程序 有md Update調用 block是分塊的原始位元組
*/
private void md Transform(byte block[]) {
long a = state[ ] b = state[ ] c = state[ ] d = state[ ];
long[] x = new long[ ];
Decode(x block );
/* Round */
a = FF(a b c d x[ ] S xd aa L); /* */
d = FF(d a b c x[ ] S xe c b L); /* */
c = FF(c d a b x[ ] S x dbL); /* */
b = FF(b c d a x[ ] S xc bdceeeL); /* */
a = FF(a b c d x[ ] S xf c fafL); /* */
d = FF(d a b c x[ ] S x c aL); /* */
c = FF(c d a b x[ ] S xa L); /* */
b = FF(b c d a x[ ] S xfd L); /* */
a = FF(a b c d x[ ] S x d L); /* */
d = FF(d a b c x[ ] S x b f afL); /* */
c = FF(c d a b x[ ] S xffff bb L); /* */
b = FF(b c d a x[ ] S x cd beL); /* */
a = FF(a b c d x[ ] S x b L); /* */
d = FF(d a b c x[ ] S xfd L); /* */
c = FF(c d a b x[ ] S xa eL); /* */
b = FF(b c d a x[ ] S x b L); /* */
/* Round */
a = GG(a b c d x[ ] S xf e L); /* */
d = GG(d a b c x[ ] S xc b L); /* */
c = GG(c d a b x[ ] S x e a L); /* */
b = GG(b c d a x[ ] S xe b c aaL); /* */
a = GG(a b c d x[ ] S xd f dL); /* */
d = GG(d a b c x[ ] S x L); /* */
c = GG(c d a b x[ ] S xd a e L); /* */
b = GG(b c d a x[ ] S xe d fbc L); /* */
a = GG(a b c d x[ ] S x e cde L); /* */
d = GG(d a b c x[ ] S xc d L); /* */
c = GG(c d a b x[ ] S xf d d L); /* */
b = GG(b c d a x[ ] S x a edL); /* */
a = GG(a b c d x[ ] S xa e e L); /* */
d = GG(d a b c x[ ] S xfcefa f L); /* */
c = GG(c d a b x[ ] S x f d L); /* */
b = GG(b c d a x[ ] S x d a c aL); /* */
/* Round */
a = HH(a b c d x[ ] S xfffa L); /* */
d = HH(d a b c x[ ] S x f L); /* */
c = HH(c d a b x[ ] S x d d L); /* */
b = HH(b c d a x[ ] S xfde cL); /* */
a = HH(a b c d x[ ] S xa beea L); /* */
d = HH(d a b c x[ ] S x bdecfa L); /* */
c = HH(c d a b x[ ] S xf bb b L); /* */
b = HH(b c d a x[ ] S xbebfbc L); /* */
a = HH(a b c d x[ ] S x b ec L); /* */
d = HH(d a b c x[ ] S xeaa faL); /* */
c = HH(c d a b x[ ] S xd ef L); /* */
b = HH(b c d a x[ ] S x d L); /* */
a = HH(a b c d x[ ] S xd d d L); /* */
d = HH(d a b c x[ ] S xe db e L); /* */
c = HH(c d a b x[ ] S x fa cf L); /* */
b = HH(b c d a x[ ] S xc ac L); /* */
/* Round */
a = II(a b c d x[ ] S xf L); /* */
d = II(d a b c x[ ] S x aff L); /* */
c = II(c d a b x[ ] S xab a L); /* */
b = II(b c d a x[ ] S xfc a L); /* */
a = II(a b c d x[ ] S x b c L); /* */
d = II(d a b c x[ ] S x f ccc L); /* */
c = II(c d a b x[ ] S xffeff dL); /* */
b = II(b c d a x[ ] S x dd L); /* */
a = II(a b c d x[ ] S x fa e fL); /* */
d = II(d a b c x[ ] S xfe ce e L); /* */
c = II(c d a b x[ ] S xa L); /* */
b = II(b c d a x[ ] S x e a L); /* */
a = II(a b c d x[ ] S xf e L); /* */
d = II(d a b c x[ ] S xbd af L); /* */
c = II(c d a b x[ ] S x ad d bbL); /* */
b = II(b c d a x[ ] S xeb d L); /* */
state[ ] += a;
state[ ] += b;
state[ ] += c;
state[ ] += d;
}
/*Encode把long數組按順序拆成byte數組 因為java的long類型是 bit的
只拆低 bit 以適應原始C實現的用途
*/
private void Encode(byte[] output long[] input int len) {
int i j;
for (i = j = ; j < len; i++ j += ) {
output[j] = (byte) (input[i] & xffL);
output[j + ] = (byte) ((input[i] >>> ) & xffL);
output[j + ] = (byte) ((input[i] >>> ) & xffL);
output[j + ] = (byte) ((input[i] >>> ) & xffL);
}
}
/*Decode把byte數組按順序合成成long數組 因為java的long類型是 bit的
只合成低 bit 高 bit清零 以適應原始C實現的用途
*/
private void Decode(long[] output byte[] input int len) {
int i j;
for (i = j = ; j < len; i++ j += )
output[i] = b iu(input[j]) | (b iu(input[j + ]) << ) | (b iu(input[j + ]) << )
| (b iu(input[j + ]) << );
return;
}
/*
b iu是我寫的一個把byte按照不考慮正負號的原則的"升位"程序 因為java沒有unsigned運算
*/
public static long b iu(byte b) {
return b < ? b & x F + : b;
}
/*byteHEX() 用來把一個byte類型的數轉換成十六進制的ASCII表示
因為java中的byte的toString無法實現這一點 我們又沒有C語言中的
sprintf(outbuf % X ib)
*/
public static String byteHEX(byte ib) {
char[] Digit = { A B C D E F };
char[] ob = new char[ ];
ob[ ] = Digit[(ib >>> ) & X F];
ob[ ] = Digit[ib & X F];
String s = new String(ob);
return s;
}
public static void main(String args[]) {
MD m = new MD ();
if (Array getLength(args) == ) { //如果沒有參數 執行標準的Test Suite
System out println( MD Test suite: );
System out println( MD ( ): + m getMD ofStr( ));
System out println( MD ( a ): + m getMD ofStr( a ));
System out println( MD ( abc ): + m getMD ofStr( abc ));
System out println( MD ( ): + m getMD ofStr( ));
System out println( MD ( ): + m getMD ofStr( ));
System out println( MD ( message digest ): + m getMD ofStr( message digest ));
System out println( MD ( abcdefghijklmnopqrstuvwxyz ): + m getMD ofStr( abcdefghijklmnopqrstuvwxyz ));
System out println( MD ( ):
+ m getMD ofStr( ));
} else
System out println( MD ( + args[ ] + )= + m getMD ofStr(args[ ]));
}
lishixin/Article/program/Java/hx/201311/26604
⑧ java中md5加密
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class md5 {
public String str;
public void md5s(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
str = buf.toString();
System.out.println("result: " + buf.toString());// 32位的加密
System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String agrs[]) {
md5 md51 = new md5();
md51.md5s("4");//加密4
}
}
⑨ 如何在java中實現md5加密和解密
package endecrypt;
02.
03.import java.io.UnsupportedEncodingException;
04.import java.security.MessageDigest;
05.import java.security.NoSuchAlgorithmException;
06.
07./**
08. * 採用MD5加密解密
09. * @author tfq
10. * @datetime 2011-10-13
11. */
12.public class MD5Util {
13.
14. /***
15. * MD5加碼 生成32位md5碼
16. */
17. public static String string2MD5(String inStr){
18. MessageDigest md5 = null;
19. try{
20. md5 = MessageDigest.getInstance("MD5");
21. }catch (Exception e){
22. System.out.println(e.toString());
23. e.printStackTrace();
24. return "";
25. }
26. char[] charArray = inStr.toCharArray();
27. byte[] byteArray = new byte[charArray.length];
28.
29. for (int i = 0; i < charArray.length; i++)
30. byteArray[i] = (byte) charArray[i];
31. byte[] md5Bytes = md5.digest(byteArray);
32. StringBuffer hexValue = new StringBuffer();
33. for (int i = 0; i < md5Bytes.length; i++){
34. int val = ((int) md5Bytes[i]) & 0xff;
35. if (val < 16)
36. hexValue.append("0");
37. hexValue.append(Integer.toHexString(val));
38. }
39. return hexValue.toString();
40.
41. }
42.
43. /**
44. * 加密解密演算法 執行一次加密,兩次解密
45. */
46. public static String convertMD5(String inStr){
47.
48. char[] a = inStr.toCharArray();
49. for (int i = 0; i < a.length; i++){
50. a[i] = (char) (a[i] ^ 't');
51. }
52. String s = new String(a);
53. return s;
54.
55. }