aes加解密

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
* 2010年9月1日
* @author 张守磊
*/
public class DigicanAES {

public static void main(String[] args) throws Exception {
//getAutoCreateAESKey("e:/aes.txt");
//加密
byte[] text = "hello world中国".getBytes("utf-8");
byte[] bm = getAESEncode("e:/aes.txt", text);
String encryptResultStr = parseByte2HexStr(bm);
System.out.println(encryptResultStr);
byte[] decryptFrom = parseHexStr2Byte(encryptResultStr);
//解密
/*File file = new File("d:\\testAESjiamji");
byte[] bm = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(bm);*/
byte[] b = getAESDecode("e:/aes.txt", decryptFrom);
System.out.println(b);
System.out.println(new String(b));
}

/**
* 生成密钥
* 自动生成AES128位密钥
* 传入保存密钥文件路径
* filePath 表示文件存储路径加文件名;例如d:\aes.txt
* @throws NoSuchAlgorithmException
* @throws IOException
*/
public static void getAutoCreateAESKey(String filePath)
throws NoSuchAlgorithmException, IOException {
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128);//要生成多少位,只需要修改这里即可128, 192或256
SecretKey sk = kg.generateKey();
byte[] b = sk.getEncoded();
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(b);
fos.flush();
fos.close();
}

/**
* 加密
* 使用对称密钥进行加密
* keyFilePath 密钥存放路径
* text 要加密的字节数组
* 加密后返回一个字节数组
* @throws IOException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static byte[] getAESEncode(String keyFilePath, byte[] text)
throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
File file = new File(keyFilePath);
byte[] key = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(key);
SecretKeySpec sKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
byte[] bjiamihou = cipher.doFinal(text);
return bjiamihou;
}

/**
* 解密
* 使用对称密钥进行解密
* keyFilePath 密钥存放路径
* text 要解密的字节数组
* 解密后返回一个字节数组
* @throws IOException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static byte[] getAESDecode(String keyFilePath, byte[] text)
throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
File file = new File(keyFilePath);
byte[] key = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(key);
SecretKeySpec sKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sKeySpec);
byte[] bjiemihou = cipher.doFinal(text);
return bjiemihou;
}

/**将二进制转换成16进制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}

/**将16进制转换为二进制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
//解密C语言的AES加密后数据,要把JAVA中的AES改成AES/ECB/NoPadding
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值