AES加密解密

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;


import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;


public class text2 {


public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException {


String key = "11222dadadaewewedwdsdsfsdfsfcdxcx";
String result = "hello";
text2 text = new text2();
byte[] k = text.javaEncode(result, key);
System.out.println(k);
System.out.println(text.Hexadecimal(k));
System.out.println(text.myByte(text.Hexadecimal(k)));
byte[] k1 = text.javadecrypt(text.hexString2Bytes(text.Hexadecimal(k)), key);
System.out.println(new String(k1));
}


/**
* 加密

* @return
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
*/
public byte[] javaEncode(String result, String key) throws NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, InvalidKeyException {
// 选择加密方式
KeyGenerator k = KeyGenerator.getInstance("AES");
// 初始化
k.init(new SecureRandom(key.getBytes()));
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, k.generateKey());


return cipher.doFinal(result.getBytes());


}


/**
* 解密

* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public byte[] javadecrypt(byte[] result, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// 选择加密方式
KeyGenerator k = KeyGenerator.getInstance("AES");
// 初始化
k.init(new SecureRandom(key.getBytes()));
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, k.generateKey());


return cipher.doFinal(result);


}


public String Hexadecimal(byte[] by) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < by.length; i++) {


int temp = by[i] & 0xff;
String t = Integer.toHexString(temp);
if (t.length() < 2) {
builder.append(0);
}
builder.append(t);
}
return builder.toString();


}


public byte[] myByte(String by) {
String myby = "0123456789ABCDEF";


char[] y = by.toUpperCase().toCharArray();
int h = by.length() / 2;
byte b[] = new byte[h];
for (int i = 0; i < b.length; i++) {
int pos = i * 2;
b[i] = (byte) (myby.indexOf((y[pos])) << 4 | myby.indexOf((y[pos + 1])));


}
return b;


}


/**
* @Title:hexString2Bytes
* @Description:16进制字符串转字节数组
* @param src
*        16进制字符串
* @return 字节数组
* @throws
*/
public static byte[] hexString2Bytes(String src) {
int l = src.length() / 2;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
ret[i] = (byte) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
}
return ret;
}


/**
* @Title:hexString2String
* @Description:16进制字符串转字符串
* @param src
*        16进制字符串
* @return 字节数组
* @throws
*/
public static String hexString2String(String src) {
String temp = "";
for (int i = 0; i < src.length() / 2; i++) {
temp = temp + (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
}
return temp;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值