真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java代碼加密解密 java實現(xiàn)des加密解密

java cipher

java cipher是什么,讓我們一起了解一下?

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供嫩江網(wǎng)站建設(shè)、嫩江做網(wǎng)站、嫩江網(wǎng)站設(shè)計、嫩江網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、嫩江企業(yè)網(wǎng)站模板建站服務(wù),十年嫩江做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

cipher是在javax.crypto包下,構(gòu)成了Java Cryptographic Extension (JCE) 框架的核心,Java的Cipher類提供了加密和解密的功能。

我們都知道,Cipher類是一個引擎類,它需要通過getInstance()工廠方法來實例化對象。那么該如何操作?

1、我們可以通過指定轉(zhuǎn)換模式的方式獲得實例化對象,方法如下所示:

// 返回實現(xiàn)指定轉(zhuǎn)換的 Cipher對象

public static Cipher getInstance(String transformation)

2、也可以在制定轉(zhuǎn)換模式的同時制定該轉(zhuǎn)換模式的提供者,方法如下所示:

// 返回實現(xiàn)指定轉(zhuǎn)換的 Cipher對象

public static Cipher getInstance(String transformation, Provider provider)

// 返回實現(xiàn)指定轉(zhuǎn)換的 Cipher對象

public static Cipher getInstance(String transformation, String provider) ?

注意這里的參數(shù)String transformation,通過如下代碼示例:

Cipher?c?=?Cipher.getInstance("DES");

上述實例化操作是一種最為簡單的實現(xiàn),并沒有考慮DES分組算法的工作模式和填充模式,可通過以下方式對其設(shè)定:

Cipher?c?=?Cipher.getInstance("DES/CBC/PKCS5Padding");

參數(shù)String transformation的格式是“算法/工作模式/填充模式”,不同的算法支持不同的工作模式以及填充模式。

另外,Java的Cipher類還提供了加密和解密的功能,那么JAVA是如何通過Cipher實現(xiàn)加密與解密的?

實戰(zhàn)操作:具體代碼如下 package?com.bsd.yx; import?java.security.Key; import?java.security.Security; import?java.text.SimpleDateFormat; import?java.util.Date; import?javax.crypto.Cipher; import?com.ibm.model.cxf.Safety; /** ?*?加密與解密 ?*?@author?tanf ?*?@date?2013-11-08 ?*/ public?class?EncryptionDecryption?{ /** *?默認(rèn)密鑰 */ private?static?String?strDefaultKey?=?"tandaly201124335"; /**?加密工具?*/ private?static?Cipher?encryptCipher?=?null; /**?解密工具?*/ private?static?Cipher?decryptCipher?=?null; /** *?將byte數(shù)組轉(zhuǎn)換為表示16進(jìn)制值的字符串,?如:byte[]{8,18}轉(zhuǎn)換為:0813,?和public?static?byte[] *?hexStr2ByteArr(String?strIn)?互為可逆的轉(zhuǎn)換過程 *? *?@param?arrB *????????????需要轉(zhuǎn)換的byte數(shù)組 *?@return?轉(zhuǎn)換后的字符串 *?@throws?Exception *? */ public?static?String?byteArr2HexStr(byte[]?arrB)?throws?Exception?{ int?iLen?=?arrB.length; //?每個byte用兩個字符才能表示,所以字符串的長度是數(shù)組長度的兩倍 StringBuffer?sb?=?new?StringBuffer(iLen?*?2); for?(int?i?=?0;?i?

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 ;

/**

* 構(gòu)造子注解.

*/

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 ();

} /**

* 將指定的數(shù)據(jù)根據(jù)提供的密鑰進(jìn)行加密

* @param input 需要加密的數(shù)據(jù)

* @param key 密鑰

* @return byte[] 加密后的數(shù)據(jù)

* @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 ("加密前的二進(jìn)串:"+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 ("加密后的二進(jìn)串:"+byte2hex (cipherByte ));

return cipherByte ;

} /**

* 將給定的已加密的數(shù)據(jù)通過指定的密鑰進(jìn)行解密

* @param input 待解密的數(shù)據(jù)

* @param key 密鑰

* @return byte[] 解密后的數(shù)據(jù)

* @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 ("解密后的二進(jìn)串:"+byte2hex (clearByte ));

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

} return clearByte ;

} /**

* 字節(jié)碼轉(zhuǎn)換成16進(jìn)制字符串

* @param byte[] b 輸入要轉(zhuǎn)換的字節(jié)碼

* @return String 返回轉(zhuǎn)換后的16進(jìn)制字符串

*/

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 ();

}

/**

* 字符串轉(zhuǎn)成字節(jié)數(shù)組.

* @param hex 要轉(zhuǎn)化的字符串.

* @return byte[] 返回轉(zhuǎn)化后的字符串.

*/

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;

}

/**

* 字節(jié)數(shù)組轉(zhuǎn)成字符串.

* @param String 要轉(zhuǎn)化的字符串.

* @return 返回轉(zhuǎn)化后的字節(jié)數(shù)組.

*/

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();

}

/**

* 從數(shù)據(jù)庫中獲取密鑰.

* @param deptid 企業(yè)id.

* @return 要返回的字節(jié)數(shù)組.

* @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); //獲得鑰匙(字節(jié)數(shù)組)

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

password=Eryptogram.bytesToHexString(tmp); //將字節(jié)數(shù)組轉(zhuǎn)化為字符串,獲得加密后的字符串密碼解密與之差不多

請問用java如何對文件進(jìn)行加密解密?

package?com.palic.pss.afcs.worldthrough.common.util;

import?javax.crypto.Cipher;

import?javax.crypto.spec.SecretKeySpec;

import?repack.com.thoughtworks.xstream.core.util.Base64Encoder;

/**

*?AES加密解密

*?@author?EX-CHENQI004

*

*/

public?class?AesUtils?{

public?static?final?String?cKey=?"assistant7654321";

??/**?

?*?加密--把加密后的byte數(shù)組先進(jìn)行二進(jìn)制轉(zhuǎn)16進(jìn)制在進(jìn)行base64編碼?

?*?@param?sSrc?

?*?@param?sKey?

?*?@return?

?*?@throws?Exception?

?*/??

public?static?String?encrypt(String?sSrc,?String?sKey)?throws?Exception?{??

if?(sKey?==?null)?{??

throw?new?IllegalArgumentException("Argument?sKey?is?null.");??

}??

if?(sKey.length()?!=?16)?{??

throw?new?IllegalArgumentException(??

"Argument?sKey'length?is?not?16.");??

}??

byte[]?raw?=?sKey.getBytes("ASCII");??

SecretKeySpec?skeySpec?=?new?SecretKeySpec(raw,?"AES");??

??

Cipher?cipher?=?Cipher.getInstance("AES");??

cipher.init(Cipher.ENCRYPT_MODE,?skeySpec);??

??

byte[]?encrypted?=?cipher.doFinal(sSrc.getBytes("UTF-8"));??

String?tempStr?=?parseByte2HexStr(encrypted);??

??

Base64Encoder?encoder?=?new?Base64Encoder();??

return?encoder.encode(tempStr.getBytes("UTF-8"));??

}??

??

/**?

?*解密--先?進(jìn)行base64解碼,在進(jìn)行16進(jìn)制轉(zhuǎn)為2進(jìn)制然后再解碼?

?*?@param?sSrc?

?*?@param?sKey?

?*?@return?

?*?@throws?Exception?

?*/??

public?static?String?decrypt(String?sSrc,?String?sKey)?throws?Exception?{??

??

if?(sKey?==?null)?{??

throw?new?IllegalArgumentException("499");??

}??

if?(sKey.length()?!=?16)?{??

throw?new?IllegalArgumentException("498");??

}??

??

byte[]?raw?=?sKey.getBytes("ASCII");??

SecretKeySpec?skeySpec?=?new?SecretKeySpec(raw,?"AES");??

??

Cipher?cipher?=?Cipher.getInstance("AES");??

cipher.init(Cipher.DECRYPT_MODE,?skeySpec);??

??

Base64Encoder?encoder?=?new?Base64Encoder();??

byte[]?encrypted1?=?encoder.decode(sSrc);??

??

String?tempStr?=?new?String(encrypted1,?"utf-8");??

encrypted1?=?parseHexStr2Byte(tempStr);??

byte[]?original?=?cipher.doFinal(encrypted1);??

String?originalString?=?new?String(original,?"utf-8");??

return?originalString;??

}??

??

/**?

?*?將二進(jìn)制轉(zhuǎn)換成16進(jìn)制?

?*??

?*?@param?buf?

?*?@return?

?*/??

public?static?String?parseByte2HexStr(byte?buf[])?{??

StringBuffer?sb?=?new?StringBuffer();??

for?(int?i?=?0;?i??buf.length;?i++)?{??

String?hex?=?Integer.toHexString(buf[i]??0xFF);??

if?(hex.length()?==?1)?{??

hex?=?'0'?+?hex;??

}??

sb.append(hex.toUpperCase());??

}??

return?sb.toString();??

}??

??

/**?

?*?將16進(jìn)制轉(zhuǎn)換為二進(jìn)制?

?*??

?*?@param?hexStr?

?*?@return?

?*/??

public?static?byte[]?parseHexStr2Byte(String?hexStr)?{??

if?(hexStr.length()??1)??

return?null;??

byte[]?result?=?new?byte[hexStr.length()?/?2];??

for?(int?i?=?0;?i??hexStr.length()?/?2;?i++)?{??

int?high?=?Integer.parseInt(hexStr.substring(i?*?2,?i?*?2?+?1),?16);??

int?low?=?Integer.parseInt(hexStr.substring(i?*?2?+?1,?i?*?2?+?2),??

16);??

result[i]?=?(byte)?(high?*?16?+?low);??

}??

return?result;??

}?

public?static?void?main(String[]?args)?throws?Exception?{

/*

?*?加密用的Key?可以用26個字母和數(shù)字組成,最好不要用保留字符,雖然不會錯,至于怎么裁決,個人看情況而定

?*/

String?cKey?=?"assistant7654321";

//?需要加密的字串

String?cSrc?=?"123456";

//?加密

long?lStart?=?System.currentTimeMillis();

String?enString?=?encrypt(cSrc,?cKey);

System.out.println("加密后的字串是:"?+?enString);

long?lUseTime?=?System.currentTimeMillis()?-?lStart;

System.out.println("加密耗時:"?+?lUseTime?+?"毫秒");

//?解密

lStart?=?System.currentTimeMillis();

String?DeString?=?decrypt(enString,?cKey);

System.out.println("解密后的字串是:"?+?DeString);

lUseTime?=?System.currentTimeMillis()?-?lStart;

System.out.println("解密耗時:"?+?lUseTime?+?"毫秒");

}

}

java密碼加密與解密

以下兩個類可以很方便的完成字符串的加密和解密

加密 CryptHelper encrypt(password)

解密 CrypHelper decrypt(password)

代碼如下

CryptUtils java

[java]

package gdie lab crypt;

import java io IOException;

import javax crypto Cipher;

import javax crypto KeyGenerator;

import javax crypto SecretKey;

import apache xerces internal impl dv util Base ;

public class CryptUtils {

private static String Algorithm = DES ;

private static byte[] DEFAULT_KEY=new byte[] { };

private static String VALUE_ENCODING= UTF ;

/**

* 生成密鑰

*

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

* @throws exception

*???????????? 扔出異常

*/

public static byte[] getSecretKey() throws Exception {

KeyGenerator keygen = KeyGenerator getInstance(Algorithm)

SecretKey deskey = keygen generateKey()

// if (debug ) System out println ( 生成密鑰 +byte hex (deskey getEncoded

// ()))

return deskey getEncoded()

}

/**

* 將指定的數(shù)據(jù)根據(jù)提供的密鑰進(jìn)行加密

*

* @param input

*??????????? 需要加密的數(shù)據(jù)

* @param key

*??????????? 密鑰

* @return byte[] 加密后的數(shù)據(jù)

* @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 ( 加密前的二進(jìn)串 +byte hex (input ))

// System out println ( 加密前的字符串 +new String (input ))

//

// }

Cipher c = Cipher getInstance(Algorithm)

c init(Cipher ENCRYPT_MODE deskey)

byte[] cipherByte = c doFinal(input)

// if (debug ) System out println ( 加密后的二進(jìn)串 +byte hex (cipherByte ))

return cipherByte;

}

public static byte[] encryptData(byte[] input) throws Exception {

return encryptData(input DEFAULT_KEY)

}

/**

* 將給定的已加密的數(shù)據(jù)通過指定的密鑰進(jìn)行解密

*

* @param input

*??????????? 待解密的數(shù)據(jù)

* @param key

*??????????? 密鑰

* @return byte[] 解密后的數(shù)據(jù)

* @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 ( 解密前的信息 +byte hex (input ))

Cipher c = Cipher getInstance(Algorithm)

c init(Cipher DECRYPT_MODE deskey)

byte[] clearByte = c doFinal(input)

// if (debug )

// {

// System out println ( 解密后的二進(jìn)串 +byte hex (clearByte ))

// System out println ( 解密后的字符串 +(new String (clearByte )))

//

// }

return clearByte;

}

public static byte[] decryptData(byte[] input) throws Exception {

return decryptData(input DEFAULT_KEY)

}

/**

* 字節(jié)碼轉(zhuǎn)換成 進(jìn)制字符串

*

* @param byte[] b 輸入要轉(zhuǎn)換的字節(jié)碼

* @return String 返回轉(zhuǎn)換后的 進(jìn)制字符串

*/

public static String byte hex(byte[] bytes) {

StringBuilder hs = new StringBuilder()

for(byte b : bytes)

hs append(String format( % $ X b))

return hs toString()

}

public static byte[] hex byte(String content) {

int l=content length()》 ;

byte[] result=new byte[l];

for(int i= ;il;i++) {

int j=i《 ;

String s=content substring(j j+ )

result[i]=Integer valueOf(s ) byteValue()

}

return result;

}

/**

* 將字節(jié)數(shù)組轉(zhuǎn)換為base 編碼字符串

* @param buffer

* @return

*/

public static String bytesToBase (byte[] buffer) {

//BASE Encoder en=new BASE Encoder()

return Base encode(buffer)

//????? return encoder encode(buffer)

}

/**

* 將base 編碼的字符串解碼為字節(jié)數(shù)組

* @param value

* @return

* @throws IOException

*/

public static byte[] base ToBytes(String value) throws IOException {

//return Base decodeToByteArray(value)

//????? System out println(decoder decodeBuffer(value))

//????? return decoder decodeBuffer(value)

return Base decode(value)

}

/**

* 加密給定的字符串

* @param value

* @return 加密后的base 字符串

*/

public static String encryptString(String value) {

return encryptString(value DEFAULT_KEY)

}

/**

* 根據(jù)給定的密鑰加密字符串

* @param value 待加密的字符串

* @param key 以BASE 形式存在的密鑰

* @return 加密后的base 字符串

* @throws IOException

*/

public static String encryptString(String value String key) throws IOException {

return encryptString(value base ToBytes(key))

}

/**

* 根據(jù)給定的密鑰加密字符串

* @param value 待加密的字符串

* @param key 字節(jié)數(shù)組形式的密鑰

* @return 加密后的base 字符串

*/

public static String encryptString(String value byte[] key) {

try {

byte[] data=value getBytes(VALUE_ENCODING)

data=CryptUtils encryptData(data key)

return bytesToBase (data)

} catch (Exception e) {

// TODO Auto generated catch block

e printStackTrace()

return null;

}

}

/**

* 解密字符串

* @param value base 形式存在的密文

* @return 明文

*/

public static String decryptString(String value) {

return decryptString(value DEFAULT_KEY)

}

/**

* 解密字符串

* @param value base 形式存在的密文

* @param key base 形式存在的密鑰

* @return 明文

* @throws IOException

*/

public static String decryptString(String value String key) throws IOException {

String s=decryptString(value base ToBytes(key))

return s;

}

/**

* 解密字符串

* @param value base 形式存在的密文

* @param key 字節(jié)數(shù)據(jù)形式存在的密鑰

* @return 明文

*/

public static String decryptString(String value byte[] key) {

try {

byte[] data=base ToBytes(value)

data=CryptUtils decryptData(data key)

return new String(data VALUE_ENCODING)

}catch(Exception e) {

e printStackTrace()

return null;

}

}

}

package gdie lab crypt;

import java io IOException;

import javax crypto Cipher;

import javax crypto KeyGenerator;

import javax crypto SecretKey;

import apache xerces internal impl dv util Base ;

public class CryptUtils {

private static String Algorithm = DES ;

private static byte[] DEFAULT_KEY=new byte[] { };

private static String VALUE_ENCODING= UTF ;

/**

* 生成密鑰

*

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

* @throws exception

*???????????? 扔出異常

*/

public static byte[] getSecretKey() throws Exception {

KeyGenerator keygen = KeyGenerator getInstance(Algorithm)

SecretKey deskey = keygen generateKey()

// if (debug ) System out println ( 生成密鑰 +byte hex (deskey getEncoded

// ()))

return deskey getEncoded()

}

/**

* 將指定的數(shù)據(jù)根據(jù)提供的密鑰進(jìn)行加密

*

* @param input

*??????????? 需要加密的數(shù)據(jù)

* @param key

*??????????? 密鑰

* @return byte[] 加密后的數(shù)據(jù)

* @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 ( 加密前的二進(jìn)串 +byte hex (input ))

// System out println ( 加密前的字符串 +new String (input ))

//

// }

Cipher c = Cipher getInstance(Algorithm)

c init(Cipher ENCRYPT_MODE deskey)

byte[] cipherByte = c doFinal(input)

// if (debug ) System out println ( 加密后的二進(jìn)串 +byte hex (cipherByte ))

return cipherByte;

}

public static byte[] encryptData(byte[] input) throws Exception {

return encryptData(input DEFAULT_KEY)

}

/**

* 將給定的已加密的數(shù)據(jù)通過指定的密鑰進(jìn)行解密

*

* @param input

*??????????? 待解密的數(shù)據(jù)

* @param key

*??????????? 密鑰

* @return byte[] 解密后的數(shù)據(jù)

* @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 ( 解密前的信息 +byte hex (input ))

Cipher c = Cipher getInstance(Algorithm)

c init(Cipher DECRYPT_MODE deskey)

byte[] clearByte = c doFinal(input)

// if (debug )

// {

// System out println ( 解密后的二進(jìn)串 +byte hex (clearByte ))

// System out println ( 解密后的字符串 +(new String (clearByte )))

//

// }

return clearByte;

}

public static byte[] decryptData(byte[] input) throws Exception {

return decryptData(input DEFAULT_KEY)

}

/**

* 字節(jié)碼轉(zhuǎn)換成 進(jìn)制字符串

*

* @param byte[] b 輸入要轉(zhuǎn)換的字節(jié)碼

* @return String 返回轉(zhuǎn)換后的 進(jìn)制字符串

*/

public static String byte hex(byte[] bytes) {

StringBuilder hs = new StringBuilder()

for(byte b : bytes)

hs append(String format( % $ X b))

return hs toString()

}

public static byte[] hex byte(String content) {

int l=content length()》 ;

byte[] result=new byte[l];

for(int i= ;il;i++) {

int j=i《 ;

String s=content substring(j j+ )

result[i]=Integer valueOf(s ) byteValue()

}

return result;

}

/**

* 將字節(jié)數(shù)組轉(zhuǎn)換為base 編碼字符串

* @param buffer

* @return

*/

public static String bytesToBase (byte[] buffer) {

//BASE Encoder en=new BASE Encoder()

return Base encode(buffer)

//? return encoder encode(buffer)

}

/**

* 將base 編碼的字符串解碼為字節(jié)數(shù)組

* @param value

* @return

* @throws IOException

*/

public static byte[] base ToBytes(String value) throws IOException {

//return Base decodeToByteArray(value)

//? System out println(decoder decodeBuffer(value))

//? return decoder decodeBuffer(value)

return Base decode(value)

}

/**

* 加密給定的字符串

* @param value

* @return 加密后的base 字符串

*/

public static String encryptString(String value) {

return encryptString(value DEFAULT_KEY)

}

/**

* 根據(jù)給定的密鑰加密字符串

* @param value 待加密的字符串

* @param key 以BASE 形式存在的密鑰

* @return 加密后的base 字符串

* @throws IOException

*/

public static String encryptString(String value String key) throws IOException {

return encryptString(value base ToBytes(key))

}

/**

* 根據(jù)給定的密鑰加密字符串

* @param value 待加密的字符串

* @param key 字節(jié)數(shù)組形式的密鑰

* @return 加密后的base 字符串

*/

public static String encryptString(String value byte[] key) {

try {

byte[] data=value getBytes(VALUE_ENCODING)

data=CryptUtils encryptData(data key)

return bytesToBase (data)

} catch (Exception e) {

// TODO Auto generated catch block

e printStackTrace()

return null;

}

}

/**

* 解密字符串

* @param value base 形式存在的密文

* @return 明文

*/

public static String decryptString(String value) {

return decryptString(value DEFAULT_KEY)

}

/**

* 解密字符串

* @param value base 形式存在的密文

* @param key base 形式存在的密鑰

* @return 明文

* @throws IOException

*/

public static String decryptString(String value String key) throws IOException {

String s=decryptString(value base ToBytes(key))

return s;

}

/**

* 解密字符串

* @param value base 形式存在的密文

* @param key 字節(jié)數(shù)據(jù)形式存在的密鑰

* @return 明文

*/

public static String decryptString(String value byte[] key) {

try {

byte[] data=base ToBytes(value)

data=CryptUtils decryptData(data key)

return new String(data VALUE_ENCODING)

}catch(Exception e) {

e printStackTrace()

return null;

}

}

}

CryptHelper java

[java]

package gdie lab crypt;

import javax crypto Cipher;

import javax crypto SecretKey;

import javax crypto SecretKeyFactory;

import javax crypto spec DESKeySpec;

import javax crypto spec IvParameterSpec;

import springframework util DigestUtils;

public class CryptHelper{

private static String CRYPT_KEY = zhongqian ;

//加密

private static Cipher ecip;

//解密

private static Cipher dcip;

static {

try {

String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()

KEY = KEY substring( )

byte[] bytes = KEY getBytes()

DESKeySpec ks = new DESKeySpec(bytes)

SecretKeyFactory skf = SecretKeyFactory getInstance( DES )

SecretKey sk = skf generateSecret(ks)

IvParameterSpec iv = new IvParameterSpec(bytes)

ecip = Cipher getInstance( DES/CBC/PKCS Padding )

ecip init(Cipher ENCRYPT_MODE sk iv )

dcip = Cipher getInstance( DES/CBC/PKCS Padding )

dcip init(Cipher DECRYPT_MODE sk iv )

}catch(Exception ex) {

ex printStackTrace()

}

}

public static String encrypt(String content) throws Exception {

byte[] bytes = ecip doFinal(content getBytes( ascii ))

return CryptUtils byte hex(bytes)

}

public static String decrypt(String content) throws Exception {

byte[] bytes? = CryptUtils hex byte(content)

bytes = dcip doFinal(bytes)

return new String(bytes ascii )

}

//test

public static void main(String[] args) throws Exception {

String password = gly ;

String en = encrypt(password)

System out println(en)

System out println(decrypt(en))

}

}

package gdie lab crypt;

import javax crypto Cipher;

import javax crypto SecretKey;

import javax crypto SecretKeyFactory;

import javax crypto spec DESKeySpec;

import javax crypto spec IvParameterSpec;

import springframework util DigestUtils;

public class CryptHelper{

private static String CRYPT_KEY = zhongqian ;

//加密

private static Cipher ecip;

//解密

private static Cipher dcip;

static {

try {

String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()

KEY = KEY substring( )

byte[] bytes = KEY getBytes()

DESKeySpec ks = new DESKeySpec(bytes)

SecretKeyFactory skf = SecretKeyFactory getInstance( DES )

SecretKey sk = skf generateSecret(ks)

IvParameterSpec iv = new IvParameterSpec(bytes)

ecip = Cipher getInstance( DES/CBC/PKCS Padding )

ecip init(Cipher ENCRYPT_MODE sk iv )

dcip = Cipher getInstance( DES/CBC/PKCS Padding )

dcip init(Cipher DECRYPT_MODE sk iv )

}catch(Exception ex) {

ex printStackTrace()

}

}

public static String encrypt(String content) throws Exception {

byte[] bytes = ecip doFinal(content getBytes( ascii ))

return CryptUtils byte hex(bytes)

}

public static String decrypt(String content) throws Exception {

byte[] bytes? = CryptUtils hex byte(content)

bytes = dcip doFinal(bytes)

return new String(bytes ascii )

}

//test

public static void main(String[] args) throws Exception {

String password = gly ;

String en = encrypt(password)

System out println(en)

System out println(decrypt(en))

}

lishixinzhi/Article/program/Java/hx/201311/26449


本文標(biāo)題:java代碼加密解密 java實現(xiàn)des加密解密
網(wǎng)頁鏈接:http://weahome.cn/article/dojsejc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部