密码加密解密工具类(其中的keyValue/key为公钥)

import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
* 计算单位转换工具类
* @author Administrator
*
*/
public class AppUtil {


public static final String KEY_ALGORITHM= "AES";

public static final String CIPHER_ALGORITHM= "AES";
public static byte[] initkey() throws Exception{
// 实例化密钥生成器
KeyGenerator kg=KeyGenerator. getInstance( KEY_ALGORITHM);
kg.init( 256);
kg.init( 128);
SecretKey secretKey=kg.generateKey();
return secretKey.getEncoded();
}

public static byte[] initRootKey() throws Exception{

return new byte[] { 0x08, 0x08, 0x04, 0x0b, 0x02, 0x0f, 0x0b, 0x0c,
0x01, 0x03, 0x09, 0x07, 0x0c, 0x03, 0x07, 0x0a, 0x04, 0x0f,
0x06, 0x0f, 0x0e, 0x09, 0x05, 0x01, 0x0a, 0x0a, 0x01, 0x09,
0x06, 0x07, 0x09, 0x0d };
}

public static Key toKey( byte[] key) throws Exception{

SecretKey secretKey= new SecretKeySpec(key, KEY_ALGORITHM);
return secretKey;
}
public static byte[] encrypt( byte[] data,String keyValue) throws Exception{
byte[] key=AppUtil. MD5(keyValue).getBytes();
Key k= toKey(key);
Cipher cipher=Cipher. getInstance( CIPHER_ALGORITHM);

cipher.init(Cipher. ENCRYPT_MODE, k);

return cipher.doFinal(data);
}

public static byte[] decrypt( byte[] data, byte[] key) throws Exception{

Key k = toKey(key);
Cipher cipher=Cipher. getInstance( CIPHER_ALGORITHM);

cipher.init(Cipher. DECRYPT_MODE, k);

return cipher.doFinal(data);
}
public static String MD5( String sourceStr) {
String result = "";
try {
MessageDigest md = MessageDigest. getInstance( "MD5");
md.update(sourceStr.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer( "");
for ( int offset = 0; offset < b. length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append( "0");
buf.append(Integer. toHexString(i));
}
result = buf.toString();
} catch (NoSuchAlgorithmException e) {
System. out.println(e);
}
System. out.println( "md5:"+result);
return result;
}


private static byte[] MD52 (String sourceStr) {
try {
MessageDigest md = MessageDigest. getInstance( "MD5");
md.update(sourceStr.getBytes());
byte b[] = md.digest();
return b;
} catch (NoSuchAlgorithmException e) {
System. out.println(e);
}
return null;
}


public static String bytesToHexString( byte[] src){
StringBuilder stringBuilder = new StringBuilder( "");
if (src == null || src. length <= 0) {
return null;
}
for ( int i = 0; i < src. length; i++) {
int v = src[i] & 0xFF;
String hv = Integer. toHexString(v);
if (hv.length() < 2) {
stringBuilder.append( 0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}


public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals( "")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for ( int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = ( byte) ( charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}

private static byte charToByte( char c) {
return ( byte) "0123456789ABCDEF".indexOf(c);
}

// 解密成一个对象
public static < T> T decryptObject(String str,Class className,String keyValue) throws Exception{
byte[] key=AppUtil. MD5(keyValue).getBytes();
byte[] data= hexStringToBytes(str);
Key k = toKey(key);
Cipher cipher=Cipher. getInstance( CIPHER_ALGORITHM);
cipher.init(Cipher. DECRYPT_MODE, k);
String decryptString = new String(cipher.doFinal(data), "utf-8");
// byte[] temp=decryptString.getBytes("gbk");// 这里写原编码方式
// byte[] newtemp=new String(temp, "gbk").getBytes("utf-8");// 这里写转换后的编码方式
// String newStr=new String(newtemp,"utf-8");// 这里写转换后的编码方式
JSONObject obj=JSONObject. fromObject(decryptString);
return ( T) JSONObject. toBean(obj, className);
}

// 解密成一个 String
public static String decryptStr(String str,String keyValue) throws Exception{

byte[] key=AppUtil. MD5(keyValue).getBytes();
byte[] data= hexStringToBytes(str);
Key k = toKey(key);
Cipher cipher=Cipher. getInstance( CIPHER_ALGORITHM);
cipher.init(Cipher. DECRYPT_MODE, k);
String decryptString = new String(cipher.doFinal(data));
return decryptString;
}
public static String addSecureToStr(String str,String keyValue){
byte[] data= null;
try {
data = AppUtil. encrypt(str.getBytes( "utf-8"),keyValue);
} catch (Exception e1) {
e1.printStackTrace();
}
return AppUtil. bytesToHexString(data);
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值