AES,BigInteger,MD5加密
Posted code home
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AES,BigInteger,MD5加密相关的知识,希望对你有一定的参考价值。
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加密的主要内容,如果未能解决你的问题,请参考以下文章