一、獲取code
成都創(chuàng)新互聯(lián)專注于萬年企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,電子商務(wù)商城網(wǎng)站建設(shè)。萬年網(wǎng)站建設(shè)公司,為萬年等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
將code作為參數(shù)傳遞過來
//如果有code,說明是微信小程序,根據(jù)code獲取openId
//classify用于標(biāo)識(shí)是哪個(gè)小程序
if (!CheckUtil.checkNulls( keUser.getCode(),keUser.getClassify())){
//
String openid = OpenIdUtil.oauth2GetOpenid(keUser.getCode(),keUser.getClassify());
printParamsLog(openid, logger);
keUser.setUserId(openid);
}1234567812345678
二、工具類
package com.util;
import net.sf.json.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import java.util.HashMap;
import java.util.Map;
/**
* @author xsx
*/
public class OpenIdUtil {
public static String oauth2GetOpenid(String code,String classify) {
String appid="";
String appsecret="";
switch (classify){
case "1":
//自己的配置appid
appid = "**********";
//自己的配置APPSECRET;
appsecret = "**********";
break;
case "2":
appid = "**********";
appsecret = "************";
break;
case "3":
appid = "**********";
appsecret = "************";
break;
case "4":
appid = "**********";
appsecret = "************";
break;
case "5":
appid = "**********";
appsecret = "************";
}
//授權(quán)(必填)
String grant_type = "authorization_code";
//URL
String requestUrl = "";
//請(qǐng)求參數(shù)
String params = "appid=" + appid + "secret=" + appsecret + "js_code=" + code + "grant_type=" + grant_type;
//發(fā)送請(qǐng)求
String data = HttpUtil.get(requestUrl, params);
//解析相應(yīng)內(nèi)容(轉(zhuǎn)換成json對(duì)象)
JSONObject json = JSONObject.fromObject(data);
//用戶的唯一標(biāo)識(shí)(openid)
String Openid =String.valueOf(json.get("openid"));
//System.out.println(Openid);
return Openid;
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
三、發(fā)送請(qǐng)求的工具類
package com.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* @author xsx
*/
public class HttpUtil {
/**
* 向指定URL發(fā)送GET方法的請(qǐng)求
*
* @param url
* 發(fā)送請(qǐng)求的URL
* @param param
* 請(qǐng)求參數(shù),請(qǐng)求參數(shù)應(yīng)該是 name1=value1name2=value2 的形式。
* @return String 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果
*/
public static String get(String url,String param){
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
//System.out.println(urlNameString);
URL realUrl = new URL(urlNameString);
// 打開和URL之間的連接
URLConnection connection = realUrl.openConnection();
// 設(shè)置通用的請(qǐng)求屬性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立實(shí)際的連接
connection.connect();
// 獲取所有響應(yīng)頭字段
MapString, ListString map = connection.getHeaderFields();
// 遍歷所有的響應(yīng)頭字段
/*for (String key : map.keySet()) {
System.out.println(key + "---" + map.get(key));
}*/
// 定義 BufferedReader輸入流來讀取URL的響應(yīng)
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發(fā)送GET請(qǐng)求出現(xiàn)異常!" + e);
e.printStackTrace();
}
// 使用finally塊來關(guān)閉輸入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
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)化為字符串,獲得加密后的字符串密碼解密與之差不多
以下兩個(gè)類可以很方便的完成字符串的加密和解密
加密 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
按照你同學(xué)的加密算法,只對(duì)英文小寫字母進(jìn)行了加密,原文和密文的對(duì)應(yīng)關(guān)系如下:
原文:a b c d e f g h i j k l m n o p q r s t u v w x y z
密文:f g h i j k l m n o p q r s t u v w x y z { | } a b
怎么得到上述關(guān)系呢,其實(shí)是所有的字符都有一個(gè)對(duì)應(yīng)的ASCII碼,相關(guān)ASCII碼如下:
可以看到v的ASCII碼為118,+5為123,所以變?yōu)閧
97 a
98 b
99 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z
123 {
124 |
125 }
下面為解密代碼
import?java.io.*;
class?FileIo2?{
public?static?void?main(String?args[])?{
//?聲明輸入流引用
FileInputStream?fis?=?null;
//?聲明輸出流引用
FileOutputStream?fos?=?null;
try?{
//?生成代表輸入流的對(duì)象
fis?=?new?FileInputStream("D:/test1.txt");
//?生成代表輸出流的對(duì)象
fos?=?new?FileOutputStream("D:/test2.txt");
//?生成一個(gè)字節(jié)數(shù)組
byte[]?buffer?=?new?byte[100];
//?調(diào)用輸入對(duì)象的read方法,讀取字節(jié)數(shù)組的數(shù)據(jù)
int?temp?=?fis.read(buffer,?0,?buffer.length);
for?(int?i?=?0;?i??temp;?i++)?{
if?(buffer[i]?=?'f'??buffer[i]?=?'z')?{
buffer[i]?-=?5;
System.out.printf("%c",?buffer[i]);
}else?if?(buffer[i]?==?'a')?{
buffer[i]?=?'y';
System.out.printf("%c",?buffer[i]);
}else?if?(buffer[i]?==?'b')?{
buffer[i]?=?'z';
System.out.printf("%c",?buffer[i]);
}else?if(buffer[i]?==?'{'){
buffer[i]?=?'v';
System.out.printf("%c",?buffer[i]);
}else?if(buffer[i]?==?'|'){
buffer[i]?=?'w';
System.out.printf("%c",?buffer[i]);
}else?if(buffer[i]?==?'}'){
buffer[i]?=?'x';
System.out.printf("%c",?buffer[i]);
}
}
//?System.out.printf("temp=%d",temp);
//?temp臨時(shí)定義用來接收read返回值類型,從而判斷寫入多少數(shù)據(jù)
fos.write(buffer,?0,?temp);
}?catch?(Exception?e)?{
System.out.println(e);
}
}
}
下面這段代碼是既有加密也有解密:
import?java.io.*;
class?FileIo2?{
public?static?void?jiemi(){
//?聲明輸入流引用
FileInputStream?fis?=?null;
//?聲明輸出流引用
FileOutputStream?fos?=?null;
try?{
//?生成代表輸入流的對(duì)象
fis?=?new?FileInputStream("D:/test1.txt");
//?生成代表輸出流的對(duì)象
fos?=?new?FileOutputStream("D:/test2.txt");
//?生成一個(gè)字節(jié)數(shù)組
byte[]?buffer?=?new?byte[100];
//?調(diào)用輸入對(duì)象的read方法,讀取字節(jié)數(shù)組的數(shù)據(jù)
int?temp?=?fis.read(buffer,?0,?buffer.length);
for?(int?i?=?0;?i??temp;?i++)?{
if?(buffer[i]?=?'f'??buffer[i]?=?'z')?{
buffer[i]?-=?5;
System.out.printf("%c",?buffer[i]);
}else?if?(buffer[i]?==?'a')?{
buffer[i]?=?'y';
System.out.printf("%c",?buffer[i]);
}else?if?(buffer[i]?==?'b')?{
buffer[i]?=?'z';
System.out.printf("%c",?buffer[i]);
}else?if(buffer[i]?==?'{'){
buffer[i]?=?'v';
System.out.printf("%c",?buffer[i]);
}else?if(buffer[i]?==?'|'){
buffer[i]?=?'w';
System.out.printf("%c",?buffer[i]);
}else?if(buffer[i]?==?'}'){
buffer[i]?=?'x';
System.out.printf("%c",?buffer[i]);
}
}
//?System.out.printf("temp=%d",temp);
//?temp臨時(shí)定義用來接收read返回值類型,從而判斷寫入多少數(shù)據(jù)
fos.write(buffer,?0,?temp);
}?catch?(Exception?e)?{
System.out.println(e);
}
}
public?static?void?jiami(){
//?聲明輸入流引用
FileInputStream?fis?=?null;
//?聲明輸出流引用
FileOutputStream?fos?=?null;
try?{
//?生成代表輸入流的對(duì)象
fis?=?new?FileInputStream("D:/test.txt");
//?生成代表輸出流的對(duì)象
fos?=?new?FileOutputStream("D:/test1.txt");
//?生成一個(gè)字節(jié)數(shù)組
byte[]?buffer?=?new?byte[100];
//?調(diào)用輸入對(duì)象的read方法,讀取字節(jié)數(shù)組的數(shù)據(jù)
int?temp?=?fis.read(buffer,?0,?buffer.length);
for?(int?i?=?0;?i??temp;?i++)?{
if?(buffer[i]?=?'a'??buffer[i]?=?'x')?{
buffer[i]?+=?5;
//?buffer[i]--;
System.out.printf("%c",?buffer[i]);
}
if?(buffer[i]?==?'y')?{
buffer[i]?=?'a';
System.out.printf("%c",?buffer[i]);
}
if?(buffer[i]?==?'z')?{
buffer[i]?=?'b';
System.out.printf("%c",?buffer[i]);
}
}
//?System.out.printf("temp=%d",temp);
//?temp臨時(shí)定義用來接收read返回值類型,從而判斷寫入多少數(shù)據(jù)
fos.write(buffer,?0,?temp);
}?catch?(Exception?e)?{
System.out.println(e);
}
}
public?static?void?main(String?args[])?{
jiami();//先加密
jiemi();//再解密
}
}
網(wǎng)頁名稱:解密代碼Java 解密代碼證
網(wǎng)頁路徑:http://muchs.cn/article0/doeeooo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、商城網(wǎng)站、Google、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)