AES加密解密

来源:互联网 发布:linux解压gz文件命令 编辑:程序博客网 时间:2024/06/05 04:49
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;
}
}
原创粉丝点击