package com.ego.util;

import java.security.Key;

import java.security.SecureRandom;

import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

import javax.crypto.spec.IvParameterSpec;

public class DESCryption {

private static AlgorithmParameterSpec iv = null;// 加密算法的参数接口,IvParameterSpec是它的一个实现

private static Key key = null;

// 带向量的DES加密方法
/**
*
* @param data 原文
* @param DESkey key
* @param DESIV 向量
* @return 加密后转换成hex形式的密文字符串
* @throws Exception
*/
public static String encode(String data, byte[] DESkey, byte[] DESIV)
throws Exception {
DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
iv = new IvParameterSpec(DESIV);// 设置向量
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
key = keyFactory.generateSecret(keySpec);// 得到密钥对象
Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量
byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
return byte2hex(pasByte);
} // 带向量的DES解密方法
/**
*
* @param data 加密后转换成hex形式的密文字符串
* @param DESkey key
* @param DESIV 向量
* @return 原文
* @throws Exception
*/
public static String decode(String data, byte[] DESkey, byte[] DESIV)
throws Exception {
DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
iv = new IvParameterSpec(DESIV);// 设置向量
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
key = keyFactory.generateSecret(keySpec);// 得到密钥对象
Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
deCipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] buf = hex2byte(data.getBytes());
byte[] pasByte = deCipher.doFinal(buf);
return new String(pasByte);
} //不带向量的DES加密方法
/**
*
* @param dataString 原文
* @param keyString key
* @return 加密后转换成hex形式的密文字符串
* @throws Exception
*/
public static String encrypt(String dataString, String keyString) throws Exception {
byte[] data = dataString.getBytes();
byte[] key =keyString.getBytes();
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密钥初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
byte[] bt=cipher.doFinal(data);
String strs=byte2hex(bt);
return strs;
}
//不带向量的DES解密方法
/**
*
* @param dataString 加密后转换成hex形式的密文字符串
* @param keyString key
* @return 原文
* @throws Exception
*/
public static String decrypt(String dataString, String keyString)throws Exception{
if (dataString == null)
return null;
byte[] dataHex = dataString.getBytes();
byte[] key =keyString.getBytes();
byte[] data = hex2byte(dataHex);
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
byte[] bt = cipher.doFinal(data);
return new String(bt);
}
/**
* 十六进制字符转换成byte数组
* @param string.getByte
* @return
*/
public static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
/**
* byte数组转换成16进制字符串
* @param b
* @return
*/
public static String byte2hex(byte[] b) {
String hs = "";
String stmp = ""; for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
// 测试
public static void main(String[] args) throws Exception {

// String result=DESCryption.decode("59782316F0DA48F6C2CD6F4E0BE6580A37A98DF27B3E623B2D07D651FA9ED1204AA6ECC637A45725126D6F177F3ACA1EF0FB5AE27B230664","20161108".getBytes(),"20161108".getBytes());

// String result=DESCryption.decode("5C77BDA7EE8096AF5E8DE236DAD59038DB7B8C7A856B09E5703191843F1A4AA0AC36BB835057C661F61F40E945DBE786513ACA1BFDC7AB876FC49E0879C6D07B","20161108".getBytes(),"20161108".getBytes());

String result=DESCryption.encode("{'uid':'admin','pwd':'21232f297a57a5a743894a0e4a801fc3'}","20161108".getBytes(),"20161108".getBytes());

System.out.println(result);

}

}

备忘DES带向量的加密和解密与DES简单加密与解密的更多相关文章

  1. 备忘DES简单加密与解密

    package com.ego.util; import java.io.IOException; import java.security.SecureRandom; import javax.cr ...

  2. c# 加密转载 备忘

    public sealed class EncryptUtils { #region Base64加密解密 /// <summary> /// Base64加密 /// </summ ...

  3. Poj 3356 ACGT(LCS 或 带备忘的递归)

    题意:把一个字符串通过增.删.改三种操作变成另外一个字符串,求最少的操作数. 分析: 可以用LCS求出最大公共子序列,再把两个串中更长的那一串中不是公共子序列的部分删除. 分析可知两个字符串的距离肯定 ...

  4. sqlserver -- 学习笔记(一)自定义函数(学习总结,备忘)

    SQL Server自定义函数,以前只在书上看过,没有动手去敲一敲,今天刚好接触到,看了几篇博文学习了下.做好备忘很重要!! (@_@)Y Learn from:http://www.cnblogs. ...

  5. GIS部分理论知识备忘随笔

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...

  6. php 相关模块备忘

    在安装php的时候,不管是编译安装: ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc -- ...

  7. Go加密解密之DES

    一.DES简介 DES(Data Encryption Standard)是对称加密算法,也就是加密和解密用相同的密钥.其入口参数有三个:key.data.mode.key为加密解密使用的密钥,dat ...

  8. Cheat (tldr, bropages) - Unix命令用法备忘单

    cheat 是一个Unix命令行小工具,用来查询一些常用命令的惯用法(我们都知道,man page阅读起来太累了,常常是跳到最后去看 examples,但并不是所有man pages里面都有examp ...

  9. Nmap备忘单:从探索到漏洞利用(Part 5)

    这是备忘单的最后一部分,在这里主要讲述漏洞评估和渗透测试. 数据库审计 列出数据库名称 nmap -sV --script=mysql-databases 192.168.195.130 上图并没有显 ...

随机推荐

  1. Making the Newsfeed web part available outside of My Sites in SharePoint 2013 分类: Sharepoint 2015-07-07 19:29 4人阅读 评论(0) 收藏

    The Newsfeed is a key piece in SP2013's approach to social computing. It appears on the landing page ...

  2. eclipse + python dev

    错误:Project interpreter not specified解决方法 http://blog.csdn.net/magictong/article/details/7288732 安装Py ...

  3. 用Window Authentication的方式去连接SQLServer

    用Window Authentication的方式去连接SQLServer Connection String: jdbc:sqlserver://${serverName};databaseName ...

  4. 移动混合开发之文件管理Final之总结

    从昨天开始:2016年7月日,早晨用时1+2个小时左右,最开始还怀疑自己能否解决,但是最终还是自己解决, 所以下次遇到问题,最好还是尽量尝试自己解决. 1.css在设计的时候,一定要把父元素的长宽高指 ...

  5. mvc 传递匿名对象

    Controller代码: public ActionResult TupleTest() { LinqDBEntities db = new LinqDBEntities(); dynamic da ...

  6. Spark源码学习1.6——Executor.scala

    Executor.scala 一.Executor类 首先判断本地性,获取slaves的host name(不是IP或者host: port),匹配运行环境为集群或者本地.如果不是本地执行,需要启动一 ...

  7. 字节序相关问题简单总结,LSB与MSB

    细细碎碎的知识点还真是不少啊,今天总结下通信中的数据字节序的问题. 先来认识名词: MSB:Most Significant Bit.    “最高有效位” LSB:Least Significant ...

  8. 用Unity写一个12306验证器的恶搞图生成软件

    前言 前一阵子是买火车票的高峰期,然后12306的验证码就遭到各种吐槽.其实大部分验证码没有那么难,大家只是因为买不到票 发泄一下不满的情绪.于是各种恶搞的图就出现了,比如找二次元里人物的矮子,找好男 ...

  9. JSONObject

    JAR包简介: commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.j ...

  10. Stars_树状数组

    Problem Description Astronomers often examine star maps where stars are represented by points on a p ...