當前位置:編程學習大全網 - 源碼下載 - java加密解密代碼

java加密解密代碼

package com.cube.limail.util;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;/**

* 加密解密類

*/

public class Eryptogram

{

private static String Algorithm ="DES";

private String key="CB7A92E3D3491964";

//定義 加密算法,可用 DES,DESede,Blowfish

static boolean debug = false ;

/**

* 構造子註解.

*/

public Eryptogram ()

{

} /**

* 生成密鑰

* @return byte[] 返回生成的密鑰

* @throws exception 扔出異常.

*/

public static byte [] getSecretKey () throws Exception

{

KeyGenerator keygen = KeyGenerator.getInstance (Algorithm );

SecretKey deskey = keygen.generateKey ();

System.out.println ("生成密鑰:"+bytesToHexString (deskey.getEncoded ()));

if (debug ) System.out.println ("生成密鑰:"+bytesToHexString (deskey.getEncoded ()));

return deskey.getEncoded ();

} /**

* 將指定的數據根據提供的密鑰進行加密

* @param input 需要加密的數據

* @param key 密鑰

* @return byte[] 加密後的數據

* @throws Exception

*/

public static byte [] encryptData (byte [] input ,byte [] key ) throws Exception

{

SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );

if (debug )

{

System.out.println ("加密前的二進串:"+byte2hex (input ));

System.out.println ("加密前的字符串:"+new String (input ));

} Cipher c1 = Cipher.getInstance (Algorithm );

c1.init (Cipher.ENCRYPT_MODE ,deskey );

byte [] cipherByte =c1.doFinal (input );

if (debug ) System.out.println ("加密後的二進串:"+byte2hex (cipherByte ));

return cipherByte ;

} /**

* 將給定的已加密的數據通過指定的密鑰進行解密

* @param input 待解密的數據

* @param key 密鑰

* @return byte[] 解密後的數據

* @throws Exception

*/

public static byte [] decryptData (byte [] input ,byte [] key ) throws Exception

{

SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );

if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));

Cipher c1 = Cipher.getInstance (Algorithm );

c1.init (Cipher.DECRYPT_MODE ,deskey );

byte [] clearByte =c1.doFinal (input );

if (debug )

{

System.out.println ("解密後的二進串:"+byte2hex (clearByte ));

System.out.println ("解密後的字符串:"+(new String (clearByte )));

} return clearByte ;

} /**

* 字節碼轉換成16進制字符串

* @param byte[] b 輸入要轉換的字節碼

* @return String 返回轉換後的16進制字符串

*/

public static String byte2hex (byte [] b )

{

String hs ="";

String stmp ="";

for (int n =0 ;n <b.length ;n ++)

{

stmp =(java.lang.Integer.toHexString (b [n ] & 0XFF ));

if (stmp.length ()==1 ) hs =hs +"0"+stmp ;

else hs =hs +stmp ;

if (n <b.length -1 ) hs =hs +":";

} return hs.toUpperCase ();

}

/**

* 字符串轉成字節數組.

* @param hex 要轉化的字符串.

* @return byte[] 返回轉化後的字符串.

*/

public static byte[] hexStringToByte(String hex) {

int len = (hex.length() / 2);

byte[] result = new byte[len];

char[] achar = hex.toCharArray();

for (int i = 0; i < len; i++) {

int pos = i * 2;

result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));

}

return result;

}

private static byte toByte(char c) {

byte b = (byte) "0123456789ABCDEF".indexOf(c);

return b;

}

/**

* 字節數組轉成字符串.

* @param String 要轉化的字符串.

* @return 返回轉化後的字節數組.

*/

public static final String bytesToHexString(byte[] bArray) {

StringBuffer sb = new StringBuffer(bArray.length);

String sTemp;

for (int i = 0; i < bArray.length; i++) {

sTemp = Integer.toHexString(0xFF & bArray[i]);

if (sTemp.length() < 2)

sb.append(0);

sb.append(sTemp.toUpperCase());

}

return sb.toString();

}

/**

* 從數據庫中獲取密鑰.

* @param deptid 企業id.

* @return 要返回的字節數組.

* @throws Exception 可能拋出的異常.

*/

public static byte[] getSecretKey(long deptid) throws Exception {

byte[] key=null;

String value=null;

//CommDao dao=new CommDao();

// List list=dao.getRecordList("from Key k where k.deptid="+deptid);

//if(list.size()>0){

//value=((com.csc.sale.bean.Key)list.get(0)).getKey();

value = "CB7A92E3D3491964";

key=hexStringToByte(value);

//}

if (debug)

System.out.println("密鑰:" + value);

return key;

}

public String encryptData2(String data) {

String en = null;

try {

byte[] key=hexStringToByte(this.key);

en = bytesToHexString(encryptData(data.getBytes(),key));

} catch (Exception e) {

e.printStackTrace();

}

return en;

}

public String decryptData2(String data) {

String de = null;

try {

byte[] key=hexStringToByte(this.key);

de = new String(decryptData(hexStringToByte(data),key));

} catch (Exception e) {

e.printStackTrace();

}

return de;

}

} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //獲得鑰匙(字節數組)

byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //傳入密碼和鑰匙,獲得加密後的字節數組的密碼

password=Eryptogram.bytesToHexString(tmp); //將字節數組轉化為字符串,獲得加密後的字符串密碼解密與之差不多

  • 上一篇:我想學習c++找高人提供些學習方法及參考資料
  • 下一篇:何時可以打抄底
  • copyright 2024編程學習大全網