DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开来。
DES算法的入口参数有三个:Key、Data、Mode。其中Key为7个字节共56位,是DES算法的工作密钥;Data为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。
Java实现简单的DES加密解密如下:
package com.pifeng.util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* @function 使用DES加密与解密
* @author 皮锋
* @Date 2015-4-10
*/
public class EncryptionAndDecryption {
private Key key;// 秘钥
private String cleartextPasswords;// 明文密码
private String encryptionPassword;// 加密密码
private byte[] cleartextPwd;// 明文密码
private byte[] encryptionPwd;// 加密密码
/**
* 加密
*/
public String encrypt(String strPassword) {
BASE64Encoder encoder = new BASE64Encoder();
this.setKey("pifengpifeng");// 根据参数生成KEY
try {
// 使用指定的字符集将此 String 编码为 byte 序列
this.cleartextPwd = strPassword.getBytes("UTF-8");
// 加密以byte[]明文输入,byte[]密文输出
this.encryptionPwd = this.getEnDesCode(this.cleartextPwd);
// 编码
this.encryptionPassword = encoder.encode(this.encryptionPwd);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
encoder = null;
this.cleartextPwd = null;
this.encryptionPwd = null;
}
return this.encryptionPassword;
}
/**
* 解密
*/
public String decrypt(String strPassword) {
BASE64Decoder decoder = new BASE64Decoder();
// if (strPassword.trim().length() <= 0) {
// return "";
// }
this.setKey("pifengpifeng");// 根据参数生成KEY
try {
// 解码
this.encryptionPwd = decoder.decodeBuffer(strPassword);
// 解密以byte[]密文输入,byte[]明文输出
this.cleartextPwd = this.getDeDesCode(this.encryptionPwd);
// 设置编码,转成String
this.cleartextPasswords = new String(cleartextPwd, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
decoder = null;
this.cleartextPwd = null;
this.encryptionPwd = null;
}
return this.cleartextPasswords;
}
/**
* 根据参数生成KEY
*/
private void setKey(String strKey) {
KeyGenerator keyGenerator = null;
try {
// 返回生成指定算法的秘密密钥的 KeyGenerator 对象
keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(new SecureRandom(strKey.getBytes()));// 初始化此密钥生成器
this.key = keyGenerator.generateKey();// 生成一个密钥
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} finally {
keyGenerator = null;
}
}
/**
* 加密以byte[]明文输入,byte[]密文输出
*/
private byte[] getEnDesCode(byte[] myByte) {
byte[] finalByte = null;
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DES");// 返回实现指定转换的 Cipher 对象
cipher.init(Cipher.ENCRYPT_MODE, this.key);// 用密钥初始化此 Cipher
finalByte = cipher.doFinal(myByte);// 按单部分操作加密或解密数据,或者结束一个多部分操作
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} finally {
cipher = null;
}
return finalByte;
}
/**
* 解密以byte[]密文输入,byte[]明文输出
*/
private byte[] getDeDesCode(byte[] myByte) {
byte[] finalByte = null;
Cipher cipher = null;
try {
cipher = Cipher.getInstance("DES");// 返回实现指定转换的 Cipher 对象
cipher.init(Cipher.DECRYPT_MODE, this.key);// 用密钥初始化此 Cipher
finalByte = cipher.doFinal(myByte);// 按单部分操作加密或解密数据,或者结束一个多部分操作
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} finally {
cipher = null;
}
return finalByte;
}
/**
* 测试代码
* @param args
*/
public static void main(String[] args) {
EncryptionAndDecryption aa = new EncryptionAndDecryption();
String password = "love";// 密码
String encryptPwd = aa.encrypt(password);// 加密
System.out.println(encryptPwd);
String decryptPwd = aa.decrypt(encryptPwd);// 解密
System.out.println(decryptPwd);
}
}
运行结果: