Java|Java JDK 实现 DES1 加解密
一, 各种模式的DES-1加密.(直接上代码)
public final static String DES = "DES";
public interface DesModeType{
public final static String DES_MODE_CFB8 = "DES/CFB8/NoPadding";
// 需要 iv
public final static String DES_MODE_CFB64 = "DES/CFB64/NoPadding";
// 需要 iv
public final static String DES_MODE_OFB8 = "DES/OFB8/NoPadding";
// 需要 iv
public final static String DES_MODE_OFB64 = "DES/OFB64/NoPadding";
// 需要 iv
public final static String DES_MODE_CBC = "DES/CBC/PKCS5Padding";
// 需要 iv
public final static String DES_MODE_ECB = "DES/ECB/PKCS5Padding";
}public void testDES(){
Log.d(TAG, "testDES: 测试DES");
String key = "123456789";
String content = "JustTestDataForDES";
try {
byte[] iv = { 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31};
String mode = DES_MODE_CFB8;
Log.d(TAG, "解密数据CFB8 : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
mode = DES_MODE_CFB64;
Log.d(TAG, "解密数据CFB64 : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
mode = DES_MODE_OFB8;
Log.d(TAG, "解密数据OFB8 : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
mode = DES_MODE_OFB64;
Log.d(TAG, "解密数据OFB64 : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
mode = DES_MODE_CBC;
Log.d(TAG, "解密数据CBC : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
mode = DES_MODE_ECB;
Log.d(TAG, "解密数据ECB : " + new String(decryptAesBase(encryptAesBase(content.getBytes("utf-8"),key.getBytes("utf-8"),mode,iv),key.getBytes("utf-8"),mode,iv)));
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "testDES: 发生异常" );
}}
/**
* AES 加密.
* @param data 待加密数据.
* @param key加密键byte数组
* @param mode 加密模式.
* @param iv向量 8 位.
* @return
*/
private static byte[] encryptAesBase(byte[] data, byte[] key , String mode , byte[] iv){
try {
// 1. 创建秘钥对象.
DESKeySpec keySpec = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(keySpec);
// 创建加密对象.
Cipher cipher = Cipher.getInstance(mode);
// 初始化加密器.
if (mode.equals(DES_MODE_ECB)){
cipher.init(Cipher.ENCRYPT_MODE, securekey);
}else{
cipher.init(Cipher.ENCRYPT_MODE, securekey,new IvParameterSpec(iv));
}
return cipher.doFinal(data);
}catch (Exception e){
return null;
}
}/**
* AES 解密
* @param data 待解密数据.
* @param key加密键byte数组
* @param mode 解密模式.
* @param iv向量.
* @return
*/
private static byte[] decryptAesBase(byte[] data, byte[] key, String mode, byte[] iv){
try{
// 1. 创建秘钥.
DESKeySpec keySpec = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(keySpec);
// 2. 创建加密器.
Cipher cipher = Cipher.getInstance(mode);
// 3. 初始化加密器.
if (mode.equals(DES_MODE_ECB)){
cipher.init(Cipher.DECRYPT_MODE, securekey);
}else{
cipher.init(Cipher.DECRYPT_MODE, securekey,new IvParameterSpec(iv));
}
return cipher.doFinal(data);
}catch (Exception e){
return null;
}
}
推荐阅读
- 客户端JavaScript的线程池设计详解
- JavaScript获取时区实现过程解析
- java启动时自定义配置文件路径|java启动时自定义配置文件路径,自定义log4j2.xml位置方式
- axios|axios 拦截器管理类链式调用手写实现及原理剖析
- Java|基于Vue的仿小米商城
- 左邻云资管|助力园区实现资产360°管控
- clickhouse|clickhouse join_HDFS+ClickHouse+Spark(从0到1实现一款轻量级大数据分析系统)
- 后端|【python】Jenkins实现携带commit_log钉钉/企微机器人通知
- {调取该文章的TAG关键词}|中国人寿寿险发半年报,实现总保费4399.69亿元 | 品牌
- Java解决代码重复的三个绝招分享