java的AES、DES加密,怎么使用,我需要代码
时间: 2024-06-10 12:04:43 浏览: 4
好的,关于使用Java的AES、DES加密,您可以采用Java自带的加密库JCE(Javacryptographic Extension),下面给您提供一些示例代码:
1、使用AES加密:
```
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
public class AESUtil {
private static final String KEY_ALGORITHM = "AES";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默认的加密算法
/**
* 初始化密钥
*
* @return byte[] 密钥
* @throws Exception
*/
public static byte[] initSecretKey() throws Exception {
//返回生成指定算法的秘密密钥的 KeyGenerator 对象
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
//初始化此密钥生成器,使其具有确定的密钥大小
//AES 要求密钥长度为 128
kg.init(128, new SecureRandom());
//生成一个密钥
SecretKey secretKey = kg.generateKey();
return secretKey.getEncoded();
}
/**
* AES 加密操作
*
* @param content 待加密内容
* @param key 密钥
* @return byte[] 加密内容
* @throws Exception
*/
public static byte[] encrypt(byte[] content, byte[] key) throws Exception {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, KEY_ALGORITHM));// 初始化为加密模式的密码器
return cipher.doFinal(content);// 加密
}
/**
* AES 解密操作
*
* @param content 待解密内容
* @param key 密钥
* @return byte[] 解密内容
* @throws Exception
*/
public static byte[] decrypt(byte[] content, byte[] key) throws Exception {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, KEY_ALGORITHM));// 初始化为解密模式的密码器
return cipher.doFinal(content);// 解密
}
}
```
使用示例:
```
public class Test {
public static void main(String[] args) throws Exception {
String content = "hello, AES!";
byte[] key = AESUtil.initSecretKey();
byte[] encrypt = AESUtil.encrypt(content.getBytes(), key);
System.out.println("加密后:" + new String(encrypt));
byte[] decrypt = AESUtil.decrypt(encrypt, key);
System.out.println("解密后:" + new String(decrypt));
}
}
```
2、使用DES加密:
```
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
public class DESUtil {
private static final String KEY_ALGORITHM = "DES";
private static final String DEFAULT_CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";//默认的加密算法
/**
* 初始化密钥
*
* @return byte[] 密钥
* @throws Exception
*/
public static byte[] initSecretKey() throws Exception {
//返回生成指定算法的秘密密钥的 KeyGenerator 对象
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
//初始化此密钥生成器,使其具有确定的密钥大小
kg.init(56, new SecureRandom());
//生成一个密钥
SecretKey secretKey = kg.generateKey();
return secretKey.getEncoded();
}
/**
* DES 加密操作
*
* @param content 待加密内容
* @param key 密钥
* @return byte[] 加密内容
* @throws Exception
*/
public static byte[] encrypt(byte[] content, byte[] key) throws Exception {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, KEY_ALGORITHM));// 初始化为加密模式的密码器
return cipher.doFinal(content);// 加密
}
/**
* DES 解密操作
*
* @param content 待解密内容
* @param key 密钥
* @return byte[] 解密内容
* @throws Exception
*/
public static byte[] decrypt(byte[] content, byte[] key) throws Exception {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, KEY_ALGORITHM));// 初始化为解密模式的密码器
return cipher.doFinal(content);// 解密
}
}
```
使用示例:
```
public class Test {
public static void main(String[] args) throws Exception {
String content = "hello, DES!";
byte[] key = DESUtil.initSecretKey();
byte[] encrypt = DESUtil.encrypt(content.getBytes(), key);
System.out.println("加密后:" + new String(encrypt));
byte[] decrypt = DESUtil.decrypt(encrypt, key);
System.out.println("解密后:" + new String(decrypt));
}
}
```
以上就是Java中使用AES、DES加密的示例代码,供您参考。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)