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
分享到:
相关推荐
C 语言实现aes加解密
根据AES加解密的原理,利用C语言编写了能实现文件加密功能的AES加解密代码。
lua aes 加解密
用C语言实现的AES加密解密算法,用C语言实现的AES加密解密算法
AES加解密源码,可供大家在动态库或源码中使用。
VB.NET实现AES加密解密代码,可直接使用
' 功能: AES加密解密 password<==>hwWpiegaWDeE8kTlb/5BQg== , 8705a989e81a583784f244e56ffe4142 ' [Str] 预备转换的byte ' [StrKey] 16位的密钥 ' [StrIv] 16位的偏移 ' [Encrypt] 加密(缺省值)还是解密 ' ...
2.领域:AES加解密。 3.内容:AES加解密算法的matlab仿真。 S_box=Complete_Sbox(); M_data= { '00' '00' '00' '00'; '00' '00' '00' '00'; '00' '00' '00' '00'; '00' '00' '00' '26'; } Cipher_key={'2b' '...
本文实例为大家分享了python实现AES加密解密的具体代码,供大家参考,具体内容如下 (1)对于AES加密解密相关知识 (2)实现的功能就是输入0-16个字符,然后经过AES的加密解密最后可以得到原先的输入,运行的结果...
c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案c# aes 加解密方案
AES加密解密轮转算法工具,最大支持256bit长度的密钥,明文支持HEX和字串。可供学习设计参考。
基于c++builder 10.2 调用 两个delphi AES加解密源代码实现c++下的aes加解密,实现特别简单,不光支持字符串加解密,还支持数据流、文件加解密,特别方便。我的博客有详细使用方法说明。...
是标准的AES算法,支持在线AES加解密网站互解。 本源码从CSDN一位前辈的源码基础上做了更改,增加支持加密返回BASE64,更加完善。在此感谢原作者: http://download.csdn.net/download/qiaohaidong/9477264 需要注意...
1. 在深入理解AES加密/解密算法理论的基础上,设计一个AES加密/解密软件系统; 2. 完成CBC和ECB模式加密解密
MFC开发的 aes加解密与mac计算,输入的秘钥,明文,密文长度正确时,右边字体变绿色
基于matlab实现AES加解密和基于AES的cmac
java语言实现AES加解密
支持 CFB, OFB, CBC, ECB 模式,支持256字节以下任意长度数据加解密,也可以放开成任意长度数据加解密。 其中:AES.cpp 中为算法的实现代码;Test.cpp为测试程序
用Velirog语言实现AES加密解密,可在FPGA上实现
C# AES 加解密 源文件