des,原理待续
网络上转载的代码,忘记出处了请原作者见谅!
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,原理待续的更多相关文章
- DES原理及代码实现
一.DES基础知识DES技术特点 DES是一种用56位密钥来加密64位数据的方法 DES采取了分组加密算法:明文和密文为64位分组长度 DES采取了对称算法:加密和解密除密钥编排不同外,使 ...
- 懂了!国际算法体系对称算法DES原理
概念 加密领域主要有国际算法和国密算法两种体系.国密算法是国家密码局认定的国产密码算法.国际算法是由美国安全局发布的算法.由于国密算法安全性高等一系列原因.国内的银行和支付机构都推荐使用国密算法. 从 ...
- 浅析DES原理
对称密码体制 对称密码体制:一种加密系统.其加密密钥和解密密钥是相同的,或者能够从其中之一推知另一个.对称密码体制根据对明文加密方式不同分为分组密码和流密码. 分组密码 分组密码按照一定长度(如64b ...
- DES原理
1.DES的描述 为了建立适用于计算机系统的商用密码,美国商业部的国家标准局NBS于1973年5月和1974年8月两次发布通告,向社会征求密码算法.在征得的算法中,由IBM公司提出的算法lucifer ...
- DES原理与实现
一 DES综述 DES是对称密码的一种,它使用56位秘钥对64位长分组进行加密.DES对每个分组的内容都会进行16轮迭代,每轮的操作相同但是对应不同的子秘钥.所有的子秘钥都是由主密钥推导而来. 64位 ...
- md5,原理待续
以前项目中copy出来的 import java.security.MessageDigest; public class MD5Util { /** * @todo MD5加码 生成32位md5码 ...
- DES算法概述
DES全称为Data Encryption Standard,即数据加密标准.1997年数据加密标准DES正式公布,其分组长度为64比特,密钥长度为64比特,其中8比特为奇偶校验位,所以实际长度为56 ...
- DES、AES和RSA加密算法
DES加密算法简介 DES(Data Encryption Standard)是目前最为流行的加密算法之一(它是分组密码). 强加密使用的基本操作 -> 混淆与扩散 混淆:是一种使密钥与密文之间 ...
- DES算法的python3实现
DES原理 DES原理 这里不予以复述, 有很多优秀的博客 原理可以参考这篇博客 https://www.cnblogs.com/songwenlong/p/5944139.html DES实现 1. ...
随机推荐
- Mac下 cordova 安装随笔
首先这是我自己第一篇博客,如果有什么不对的,大家指出,积极修改. cordova是大家做混合开发最经常使用的一款使用HTML, CSS & JS进行移动App开发多平台共用一套代码,中文官方网 ...
- eclipse+PyDev遇到字符UTF-8的问题
今天配置eclipse+PyDev,在配置的时候出现了问题,如下: python and jpython require at least version 2.1 and iron python 2. ...
- Swift UIImageView和UISlider组合
/***************火焰图片Demo************start*******/ var imgView: UIImageView? override func viewDidLoa ...
- 伴随我整十个年头的校内网,现名 人人网, 是不是要shut down 了
如题: 每天我都习惯性的登录人人网,虽然现在基本上已经看不到曾经的同学上线了,不过我还是有事没事的往上面post 一些出行的照片,没事无聊的时候上这个网上看看自己曾经的照片,虽然已经无人在线,但是自己 ...
- [agc23E]Inversions
Atcoder description 给你\(n\)和\(\{a_i\}\),你需要求所有满足\(p_i\le a_i\)的\(1-n\)排列的逆序对个数之和模\(10^9+7\). \(n \le ...
- python学习(一)—简明python教程
2016-04-12 15:59:47 1. 介绍2. 安装Python3. 最初的步骤4. 基本概念5. 运算符与表达式6. 控制流7. 函数8. 模块9. 数据结构10. 解决问题——编写一个Py ...
- hadoop之 Hadoop1.x和Hadoop2.x构成对比
Hadoop1.x构成: HDFS.MapReduce(资源管理和任务调度):运行时环境为JobTracker和TaskTracker: Hadoop2.0构成:HDFS.MapReduce/其他 ...
- C++ Primer第五版答案
Downloads Download the source files for GCC 4.7.0. Download the source code files for MS Visual Stud ...
- log4net 极简配置
log4net的配置详解 分类: C#2013-10-01 22:45 5335人阅读 评论(4) 收藏 举报 log4net日志框架 前言:没买到1号回家的票,所以在祖国64岁生日之 ...
- 洛谷八连测R6
本次测试暴0!!!还有两周就要考试啦!!! 看题目时觉得难度不大,就直接写正解,没有参照数据,导致测出的结果和预想有较大差距. 不过得到经验,不管题目难易(除了D1T1)都要参照数据一部分一部分写,那 ...