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;
}
}
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
- 加密解密。(aes, java)
- AES加密解密类
- AES 加密/解密备忘
- java,AES加密,解密
- AES 加密解密 c#
- AES加密解密程序。
- AES加密和解密
- java AES加密解密
- java AES加密解密
- AES加密解密 应用
- AES 加密解密API
- php AES加密解密
- AES加密解密
- AES加密与解密
- java AES加密解密
- c# aes 加密解密
- java AES 加密/解密
- MySQL AES加密解密
- binder 1 kernel开篇
- ubuntu 16.10 安装 openjdk 7
- Java 链式调用
- socket服务器
- hihocoder 字符消除
- AES加密解密
- Java学习笔记---反射
- Android-times-square自定义样式
- Python-时间处理
- 如何免费使用webstorm
- junit的错误 No tests found matching [{ExactMatcher:fDisplayName=test2],
- RMI远程服务操作
- Jsonp实现跨域详解
- js Date 获取 年 月 日 具体的值