网络上转载的代码,忘记出处了请原作者见谅!

des类

import java.security.*;
import javax.crypto.*; /**
* DES加解密算法
*/
public class DES { private static String strDefaultKey = "abcDEF123";
private Cipher encryptCipher = null;
private Cipher decryptCipher = null; /**
* 默认构造方法,使用默认密钥
* @throws Exception
*/
public DES() throws Exception {
this(strDefaultKey);
} /**
* 指定密钥构造方法
* @param strKey 指定的密钥
* @throws Exception
*/
public DES(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
} /**
* 加密字符串
* @param strIn 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
} /**
* 加密字节数组
* @param arrB 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
} /**
* 解密字符串
* @param strIn 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn)));
} /**
* 解密字节数组
* @param arrB 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
} /**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位
* 不足8位时后面补0,超出8位只取前8位
* @param arrBTmp 构成该字符串的字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
byte[] arrB = new byte[8];
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
} /**
* 将byte数组转换为表示16进制值的字符串,
* 如:byte[]{8,18}转换为:0813,
* 和public static byte[] hexStr2ByteArr(String strIn)
* 互为可逆的转换过程
* @param arrB 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
while (intTmp < 0) {
intTmp = intTmp + 256;
}
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
} /**
* 将表示16进制值的字符串转换为byte数组,
* 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的转换过程
* @param strIn 需要转换的字符串
* @return 转换后的byte数组
* @throws Exception 本方法不处理任何异常,所有异常全部抛出
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
} }

加密解密工具类

/**
* 加密解密工具类
*/
public class EncryUtil { /**
* 使用默认密钥进行DES加密
*/
public static String encrypt(String plainText) {
try {
return new DES().encrypt(plainText);
} catch (Exception e) {
return null;
}
} /**
* 使用指定密钥进行DES解密
*/
public static String encrypt(String plainText, String key) {
try {
return new DES(key).encrypt(plainText);
} catch (Exception e) {
return null;
}
} /**
* 使用默认密钥进行DES解密
*/
public static String decrypt(String plainText) {
try {
return new DES().decrypt(plainText);
} catch (Exception e) {
return null;
}
} /**
* 使用指定密钥进行DES解密
*/
public static String decrypt(String plainText, String key) {
try {
return new DES(key).decrypt(plainText);
} catch (Exception e) {
return null;
}
} public static void main(String[] args) {
String key="3123_";
String encrypt = encrypt("123456",key);
System.out.println("加密的:"+encrypt);
System.out.println("解密的:"+decrypt(encrypt,key));
} }

des,原理待续的更多相关文章

  1. DES原理及代码实现

    一.DES基础知识DES技术特点 DES是一种用56位密钥来加密64位数据的方法    DES采取了分组加密算法:明文和密文为64位分组长度    DES采取了对称算法:加密和解密除密钥编排不同外,使 ...

  2. 懂了!国际算法体系对称算法DES原理

    概念 加密领域主要有国际算法和国密算法两种体系.国密算法是国家密码局认定的国产密码算法.国际算法是由美国安全局发布的算法.由于国密算法安全性高等一系列原因.国内的银行和支付机构都推荐使用国密算法. 从 ...

  3. 浅析DES原理

    对称密码体制 对称密码体制:一种加密系统.其加密密钥和解密密钥是相同的,或者能够从其中之一推知另一个.对称密码体制根据对明文加密方式不同分为分组密码和流密码. 分组密码 分组密码按照一定长度(如64b ...

  4. DES原理

    1.DES的描述 为了建立适用于计算机系统的商用密码,美国商业部的国家标准局NBS于1973年5月和1974年8月两次发布通告,向社会征求密码算法.在征得的算法中,由IBM公司提出的算法lucifer ...

  5. DES原理与实现

    一 DES综述 DES是对称密码的一种,它使用56位秘钥对64位长分组进行加密.DES对每个分组的内容都会进行16轮迭代,每轮的操作相同但是对应不同的子秘钥.所有的子秘钥都是由主密钥推导而来. 64位 ...

  6. md5,原理待续

    以前项目中copy出来的 import java.security.MessageDigest; public class MD5Util { /** * @todo MD5加码 生成32位md5码 ...

  7. DES算法概述

    DES全称为Data Encryption Standard,即数据加密标准.1997年数据加密标准DES正式公布,其分组长度为64比特,密钥长度为64比特,其中8比特为奇偶校验位,所以实际长度为56 ...

  8. DES、AES和RSA加密算法

    DES加密算法简介 DES(Data Encryption Standard)是目前最为流行的加密算法之一(它是分组密码). 强加密使用的基本操作 -> 混淆与扩散 混淆:是一种使密钥与密文之间 ...

  9. DES算法的python3实现

    DES原理 DES原理 这里不予以复述, 有很多优秀的博客 原理可以参考这篇博客 https://www.cnblogs.com/songwenlong/p/5944139.html DES实现 1. ...

随机推荐

  1. jinja 2 filter 使用

    文档地址 http://jinja.pocoo.org/docs/templates/#builtin-filters indent indent(s, width=4, indentfirst=Fa ...

  2. npm 与 package.json 快速入门

    npm 是前端开发广泛使用的包管理工具,之前使用 Weex 时看了阮一峰前辈的文章了解了一些,这次结合官方文章总结一下,加深下理解吧! 读完本文你将了解: 什么是 npm 安装 npm 更新 npm ...

  3. java MessageFormat.format 用法

    FormatElement: { ArgumentIndex }:是从0开始的入参位置索引. { ArgumentIndex , FormatType } { ArgumentIndex , Form ...

  4. 算法初探:Tensorflow及PAI平台的使用

    前言 Tensorflow这个词由来已久,但是对它的理解一直就停留在“听过”的层面.之前做过一个无线图片适配问题智能识别的项目,基于Tensorflow实现了GoogLeNet - Inception ...

  5. [CF995F]Cowmpany Cowmpensation

    codeforces description 一棵\(n\)个节点的树,给每个节点标一个\([1,m]\)之间的编号,要求儿子的权值不大于父亲权值.求方案数.\(n\le3000,n\le10^9\) ...

  6. Oracle数据库导入导出(备份还原)

    一.数据库的导出 1 将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中(全库导出) exp system/manager@TEST file=d:\ ...

  7. FastAdmin 升级后出现 is already in use

    FastAdmin 升级后出现 is already in use 升级 FastAdmin 改进很多,但全新安装出现以下错误 Cannot use app\common\library\Menu a ...

  8. xargs命令学习

    1.xargs复制文件 目录下文件结构为: . ├── demo1 │ ├── test.lua │ ├── test.php │ └── test.txt └── demo2 执行命令: find ...

  9. cocos命令行生成项目

    cocos命令行生成项目: cocos new GoodDay(项目名称) -p com.boleban.www(包名字) -l cpp(项目类型) -d D:\DevProject\cocos2dx ...

  10. 马士兵Spring-声明式事务管理-XML

    1.com.cy.model中User.java  和 Log.java 实体 和上一节一样: 2.DAO的实现类com.cy.dao.impl中的UserDAOImpl.LogDAOImpl.jav ...