Java实现简单的DES加密解密

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_MODEthis.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_MODEthis.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);

}

}

运行结果:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

E%3Dmc%B2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值