1. 程式人生 > >AES,BigInteger,MD5加密

AES,BigInteger,MD5加密

buffer padding result des bank rom pri acc ble

package cn.com.gome.cashier.web;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class MyTestDemo {

/**
* @Title: encrypt
* @Description: AES對稱加密
* @param @param password
* @param @return 設定文件
* @return String 返回類型
* @throws
*/
public static String encrypt(String password,String securityKey) {

byte[] crypted = null;
try {
SecretKeySpec skey = new SecretKeySpec(securityKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//調用靜態工廠方法得到Cipher對象
cipher.init(Cipher.ENCRYPT_MODE, skey);//ENCRYPT_MODE,加密數據
crypted = cipher.doFinal(password.getBytes());
} catch (Exception e) {
System.out.println(e.toString());
}
try {
return new String(encodeBase64(crypted)).replace(" ", "");
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

/**
* @Title: decrypt
* @Description: 對稱解密
* @param @param encrypted
* @param @return 設定文件
* @return String 返回類型
* @throws
*/
public static String decrypt(String input,String securityKey) {
byte[] output = null;
try {
SecretKeySpec skey = new SecretKeySpec(securityKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey);//DECRYPT_MODE,解密數據
output = cipher.doFinal(decodeBase64(input));
} catch (Exception e) {
System.out.println(e.toString());
return "";
}
return new String(output);
}
private static final int RADIX = 16;
private static final String SEED = "0933910847463829232312312";


/**
* @Title: encrypt
* @Description: 對稱加密
* @param @param password
* @param @return 設定文件
* @return String 返回類型
* @throws
*/
public static final String encrypt(String password) {

// if(isEmpty(password)){
// return "";
// }

BigInteger bi_passwd = new BigInteger(password.getBytes());

BigInteger bi_r0 = new BigInteger(SEED);
BigInteger bi_r1 = bi_r0.xor(bi_passwd);//位運算

return bi_r1.toString(RADIX);
}

/**
* @Title: decrypt
* @Description: 對稱解密
* @param @param encrypted
* @param @return 設定文件
* @return String 返回類型
* @throws
*/
public static final String decrypt(String encrypted) {

// if(isEmpty(encrypted)){
// return "";
// }

BigInteger bi_confuse = new BigInteger(SEED);

try {
BigInteger bi_r1 = new BigInteger(encrypted, RADIX);
BigInteger bi_r0 = bi_r1.xor(bi_confuse);
return new String(bi_r0.toByteArray());
} catch (Exception e) {
return "";
}

}

/**
* @Title: encodeMessage
* @Description: md5簽名
* @param @param data
* @param @return
* @param @throws Exception 設定文件
* @return String 返回類型
* @throws
*/
public static String encodeMessage(String data) throws Exception {

// if(isEmpty(data)){
// return "";
// }

MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(data.getBytes());
return toHex(md5.digest());

}

private static String toHex(byte[] buffer) {
byte[] result = new byte[buffer.length * 2];

for (int i = 0; i < buffer.length; i++) {
byte[] temp = getHexValue(buffer[i]);
result[(i * 2)] = temp[0];
result[(i * 2 + 1)] = temp[1];
}
return new String(result).toUpperCase();
}

private static byte[] getHexValue(byte b) {
int value = b;
if (value < 0) {
value = 256 + b;
}
String s = Integer.toHexString(value);
if (s.length() == 1) {
return new byte[] { 48, (byte) s.charAt(0) };
}
return new byte[] { (byte) s.charAt(0), (byte) s.charAt(1) };
}
/***
* encode by Base64
*/
private static String encodeBase64(byte[]input) throws Exception{
Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
Method mainMethod= clazz.getMethod("encode", byte[].class);
mainMethod.setAccessible(true);
Object retObj=mainMethod.invoke(null, new Object[]{input});
return (String)retObj;
}
/***
* decode by Base64
*/
private static byte[] decodeBase64(String input) throws Exception{
Class clazz=Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
Method mainMethod= clazz.getMethod("decode", String.class);
mainMethod.setAccessible(true);
Object retObj=mainMethod.invoke(null, input);
return (byte[])retObj;
}
/**
* @param bytes
* @return
*/
private static byte[] decode(final byte[] bytes) {
return Base64.decodeBase64(bytes);
}

/**
* 二進制數據編碼為BASE64字符串
*
* @param bytes
* @return
* @throws Exception
*/
private static String encode(final byte[] bytes) {
return new String(Base64.encodeBase64(bytes));
}
/**
* 編碼
* @param bstr
* @return String
*/
private static String encodes(byte[] bstr){
return new sun.misc.BASE64Encoder().encode(bstr);
}

/**
* 解碼
* @param str
* @return string
*/
private static byte[] decodes(String str){
byte[] bt = null;
try {
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
bt = decoder.decodeBuffer( str );
} catch (Exception e) {
e.printStackTrace();
}
return bt;
}

public static void main(String[] args) {
// String str = "移動端訂單支付請求接口 /cashier-mobile/src/main/java/cn/com/gome/cashier/mobile/dubbo/impl/MobileOrderPayReqFacadeServiceImpl.java"
//+"移動端網關確認支付接口 /cashier-mobile/src/main/java/cn/com/gome/cashier/mobile/dubbo/impl/GatewayConfirmPayFacadeServiceImpl.java"
//+"銀行分期信息與快捷卡查詢接口 /cashier-mobile/src/main/java/cn/com/gome/cashier/mobile/dubbo/impl/BankPartAndQuickQueryFacadeServiceImpl.java 移動端只針對二次支付,未綁卡提示pc端綁卡"
//+"移動端收銀臺活動刷新任務 /cashier-task/src/main/java/cn/com/gome/cashier/task/step/impl/MobileCashierActivityTask.java MOBILE_ACTIVITY_ supplierNo+ + cashierVersion 網關確認支付"
//+"移動端促銷語自動刷新緩存任務 /cashier-task/src/main/java/cn/com/gome/cashier/task/step/impl/MobileSupperCacheTask.java MOBILE_PROMOTION_ supplierNo+ + cashierVersion 訂單支付請求 "
//+"1,配置完收銀臺銀行分發不配置供應商關聯銀行,可以勾選設備支付方式配置,可以進行供應商分發及商戶號匹配";

//秘鑰必須為16位
String str = "12345678901234567890";
String encr = encrypt(str,"qazxswedcqazwsxe");
System.out.println(encr);
String dec = decrypt(encr,"qazxswedcqazwsxe");
System.out.println("dec:"+dec);

// String enc = encrypt(str);
// System.out.println(enc);
// String dec = decrypt(enc);
// System.out.println(dec);
// BigInteger bi = new BigInteger("21312");
// System.out.println(bi.toString());

// String en = "";
// String en2 = "";
// String en3 = "";
// try {
// en = encodeMessage("xmh128");
// en2 = encodeMessage(str);
// en3 = encodeMessage(str);
// } catch (Exception e) {
// e.printStackTrace();
// }
// System.out.println(en.toString());
// System.out.println(en2.toString());
// System.out.println(en3.toString());
}
}

AES,BigInteger,MD5加密