对称加密中DES的加密与解密算法
来源:互联网 发布:淘宝买轮毂靠谱吗 编辑:程序博客网 时间:2024/06/08 06:39
package Encoder.symmetry;
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;
/**
* 对称加密算法 可逆加密算法
* DES/3DES/AES的不同加密算法
* @author CUICHUNCHI
*
*DES:通过keygenerator获取密钥生成器实例后,设置des的算法密钥为56,便可生成密钥
*并且每次生成的密钥都不相同,为了存储,将其密钥通过base64编码转为字符串,将密钥字符串解码后实例化SecretKey
*,只需要实例化SecretKeySpec即可,返回一个SecretKey
*加密与解密都需要Cipher对象,初始化时,需要传入加密与解密对应的模式,(Cipher.ENCRYPT_MODE/Cipher.DECRYPT_MODE)
*以及传入对应的SecretKey密钥即可加密与解密
*/
public class DESUtil {
private static final String DES = "DES";
/**
* 生成 DES 的密钥
* @throws NoSuchAlgorithmException
*/
public static byte[] getKeyDES() throws NoSuchAlgorithmException{
KeyGenerator key = KeyGenerator.getInstance(DES);
key.init(56);
SecretKey generateKey = key.generateKey();
byte[] encoded = generateKey.getEncoded();
// StringBuffer hexValue = new StringBuffer();
/* for (int i = 0; i < encoded.length; i++) {
int val = ((int) encoded[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}*/
return encoded;
}
//加载生成一个SecrectKeySpec的实例,返回一个密钥SecretKey
private static SecretKey loadKeyDES(byte [] desStr){
SecretKey key = new SecretKeySpec(desStr,"DES");
return key;
}
/**
* 对原始字符进行DES加密
* @param str
* @param key
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] encryptDES(String str,SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(str.getBytes());
return doFinal;
}
/**
* 解密
* @param b
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static String decryptDES(byte [] b,SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(b);
return new String(doFinal);
}
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String str ="崔春驰";
//生成密钥
byte[] keyStr = DESUtil.getKeyDES();
SecretKey key = loadKeyDES(keyStr);
System.out.println("生成的密钥:"+key);
//调用加密
byte[] encryptDES = encryptDES(str, key);
System.out.println("加密后:"+encryptDES);
String decryptDES = decryptDES(encryptDES, key);
System.out.println("解密:"+decryptDES);
}
}
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;
/**
* 对称加密算法 可逆加密算法
* DES/3DES/AES的不同加密算法
* @author CUICHUNCHI
*
*DES:通过keygenerator获取密钥生成器实例后,设置des的算法密钥为56,便可生成密钥
*并且每次生成的密钥都不相同,为了存储,将其密钥通过base64编码转为字符串,将密钥字符串解码后实例化SecretKey
*,只需要实例化SecretKeySpec即可,返回一个SecretKey
*加密与解密都需要Cipher对象,初始化时,需要传入加密与解密对应的模式,(Cipher.ENCRYPT_MODE/Cipher.DECRYPT_MODE)
*以及传入对应的SecretKey密钥即可加密与解密
*/
public class DESUtil {
private static final String DES = "DES";
/**
* 生成 DES 的密钥
* @throws NoSuchAlgorithmException
*/
public static byte[] getKeyDES() throws NoSuchAlgorithmException{
KeyGenerator key = KeyGenerator.getInstance(DES);
key.init(56);
SecretKey generateKey = key.generateKey();
byte[] encoded = generateKey.getEncoded();
// StringBuffer hexValue = new StringBuffer();
/* for (int i = 0; i < encoded.length; i++) {
int val = ((int) encoded[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}*/
return encoded;
}
//加载生成一个SecrectKeySpec的实例,返回一个密钥SecretKey
private static SecretKey loadKeyDES(byte [] desStr){
SecretKey key = new SecretKeySpec(desStr,"DES");
return key;
}
/**
* 对原始字符进行DES加密
* @param str
* @param key
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] encryptDES(String str,SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(str.getBytes());
return doFinal;
}
/**
* 解密
* @param b
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static String decryptDES(byte [] b,SecretKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] doFinal = cipher.doFinal(b);
return new String(doFinal);
}
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String str ="崔春驰";
//生成密钥
byte[] keyStr = DESUtil.getKeyDES();
SecretKey key = loadKeyDES(keyStr);
System.out.println("生成的密钥:"+key);
//调用加密
byte[] encryptDES = encryptDES(str, key);
System.out.println("加密后:"+encryptDES);
String decryptDES = decryptDES(encryptDES, key);
System.out.println("解密:"+decryptDES);
}
}
阅读全文
0 0
- 对称加密中DES的加密与解密算法
- DES加密与解密算法的实现
- JAVA中DES对称加密解密2
- DES加密与解密算法
- DES算法加密与解密
- 对文件压缩加密,解密解压缩,对称加密,DES算法
- PHP DES 对称-加密解密
- des对称加密和解密
- java实现对称加密AES和DES的加密、解密
- 对称加密----AES和DES加密解密
- 对称加密----AES和DES加密、解密
- 对称加密----AES和DES加密、解密
- 对称加密----AES和DES加密、解密
- 对称加密----AES和DES加密、解密
- 对称加密----AES和DES加密、解密
- 对称加密----AES和DES加密、解密
- 【加密】DES加密解密算法
- DES加密解密算法
- Vue+Webpack+node构建web和App项目
- MySQL Sysbench压力测试
- Android小技巧——圆角背景
- Android调用系统相册、拍照以及裁剪最简单的实现(兼容7.0)
- 大数据企业学习篇06----Oozie详解
- 对称加密中DES的加密与解密算法
- ToolBar的使用
- Winsock2.h、Windows.h和WIN32_LEAN_AND_MEAN
- 【TensorFlow 学习】Session.run()和Tensor.eval()的区别
- HQL之初探
- 设计模式学习-享元模式
- Linux:Ldd命令介绍及使用方法
- 一样造电动车,贾跃亭成了孤家寡人,李斌的朋友圈遍布巨头
- Android6.0(包括)以后获取Wifi 扫描信息列表即getScanResults()为空