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

des类

  1. import java.security.*;
  2. import javax.crypto.*;
  3.  
  4. /**
  5. * DES加解密算法
  6. */
  7. public class DES {
  8.  
  9. private static String strDefaultKey = "abcDEF123";
  10. private Cipher encryptCipher = null;
  11. private Cipher decryptCipher = null;
  12.  
  13. /**
  14. * 默认构造方法,使用默认密钥
  15. * @throws Exception
  16. */
  17. public DES() throws Exception {
  18. this(strDefaultKey);
  19. }
  20.  
  21. /**
  22. * 指定密钥构造方法
  23. * @param strKey 指定的密钥
  24. * @throws Exception
  25. */
  26. public DES(String strKey) throws Exception {
  27. Security.addProvider(new com.sun.crypto.provider.SunJCE());
  28. Key key = getKey(strKey.getBytes());
  29. encryptCipher = Cipher.getInstance("DES");
  30. encryptCipher.init(Cipher.ENCRYPT_MODE, key);
  31. decryptCipher = Cipher.getInstance("DES");
  32. decryptCipher.init(Cipher.DECRYPT_MODE, key);
  33. }
  34.  
  35. /**
  36. * 加密字符串
  37. * @param strIn 需加密的字符串
  38. * @return 加密后的字符串
  39. * @throws Exception
  40. */
  41. public String encrypt(String strIn) throws Exception {
  42. return byteArr2HexStr(encrypt(strIn.getBytes()));
  43. }
  44.  
  45. /**
  46. * 加密字节数组
  47. * @param arrB 需加密的字节数组
  48. * @return 加密后的字节数组
  49. * @throws Exception
  50. */
  51. public byte[] encrypt(byte[] arrB) throws Exception {
  52. return encryptCipher.doFinal(arrB);
  53. }
  54.  
  55. /**
  56. * 解密字符串
  57. * @param strIn 需解密的字符串
  58. * @return 解密后的字符串
  59. * @throws Exception
  60. */
  61. public String decrypt(String strIn) throws Exception {
  62. return new String(decrypt(hexStr2ByteArr(strIn)));
  63. }
  64.  
  65. /**
  66. * 解密字节数组
  67. * @param arrB 需解密的字节数组
  68. * @return 解密后的字节数组
  69. * @throws Exception
  70. */
  71. public byte[] decrypt(byte[] arrB) throws Exception {
  72. return decryptCipher.doFinal(arrB);
  73. }
  74.  
  75. /**
  76. * 从指定字符串生成密钥,密钥所需的字节数组长度为8位
  77. * 不足8位时后面补0,超出8位只取前8位
  78. * @param arrBTmp 构成该字符串的字节数组
  79. * @return 生成的密钥
  80. * @throws java.lang.Exception
  81. */
  82. private Key getKey(byte[] arrBTmp) throws Exception {
  83. byte[] arrB = new byte[8];
  84. for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
  85. arrB[i] = arrBTmp[i];
  86. }
  87. Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
  88. return key;
  89. }
  90.  
  91. /**
  92. * 将byte数组转换为表示16进制值的字符串,
  93. * 如:byte[]{8,18}转换为:0813,
  94. * 和public static byte[] hexStr2ByteArr(String strIn)
  95. * 互为可逆的转换过程
  96. * @param arrB 需要转换的byte数组
  97. * @return 转换后的字符串
  98. * @throws Exception 本方法不处理任何异常,所有异常全部抛出
  99. */
  100. public static String byteArr2HexStr(byte[] arrB) throws Exception {
  101. int iLen = arrB.length;
  102. StringBuffer sb = new StringBuffer(iLen * 2);
  103. for (int i = 0; i < iLen; i++) {
  104. int intTmp = arrB[i];
  105. while (intTmp < 0) {
  106. intTmp = intTmp + 256;
  107. }
  108. if (intTmp < 16) {
  109. sb.append("0");
  110. }
  111. sb.append(Integer.toString(intTmp, 16));
  112. }
  113. return sb.toString();
  114. }
  115.  
  116. /**
  117. * 将表示16进制值的字符串转换为byte数组,
  118. * 和public static String byteArr2HexStr(byte[] arrB)
  119. * 互为可逆的转换过程
  120. * @param strIn 需要转换的字符串
  121. * @return 转换后的byte数组
  122. * @throws Exception 本方法不处理任何异常,所有异常全部抛出
  123. */
  124. public static byte[] hexStr2ByteArr(String strIn) throws Exception {
  125. byte[] arrB = strIn.getBytes();
  126. int iLen = arrB.length;
  127. byte[] arrOut = new byte[iLen / 2];
  128. for (int i = 0; i < iLen; i = i + 2) {
  129. String strTmp = new String(arrB, i, 2);
  130. arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
  131. }
  132. return arrOut;
  133. }
  134.  
  135. }

加密解密工具类

  1. /**
  2. * 加密解密工具类
  3. */
  4. public class EncryUtil {
  5.  
  6. /**
  7. * 使用默认密钥进行DES加密
  8. */
  9. public static String encrypt(String plainText) {
  10. try {
  11. return new DES().encrypt(plainText);
  12. } catch (Exception e) {
  13. return null;
  14. }
  15. }
  16.  
  17. /**
  18. * 使用指定密钥进行DES解密
  19. */
  20. public static String encrypt(String plainText, String key) {
  21. try {
  22. return new DES(key).encrypt(plainText);
  23. } catch (Exception e) {
  24. return null;
  25. }
  26. }
  27.  
  28. /**
  29. * 使用默认密钥进行DES解密
  30. */
  31. public static String decrypt(String plainText) {
  32. try {
  33. return new DES().decrypt(plainText);
  34. } catch (Exception e) {
  35. return null;
  36. }
  37. }
  38.  
  39. /**
  40. * 使用指定密钥进行DES解密
  41. */
  42. public static String decrypt(String plainText, String key) {
  43. try {
  44. return new DES(key).decrypt(plainText);
  45. } catch (Exception e) {
  46. return null;
  47. }
  48. }
  49.  
  50. public static void main(String[] args) {
  51. String key="3123_";
  52. String encrypt = encrypt("123456",key);
  53. System.out.println("加密的:"+encrypt);
  54. System.out.println("解密的:"+decrypt(encrypt,key));
  55. }
  56.  
  57. }

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. Mac下 cordova 安装随笔

    首先这是我自己第一篇博客,如果有什么不对的,大家指出,积极修改. cordova是大家做混合开发最经常使用的一款使用HTML, CSS & JS进行移动App开发多平台共用一套代码,中文官方网 ...

  2. eclipse+PyDev遇到字符UTF-8的问题

    今天配置eclipse+PyDev,在配置的时候出现了问题,如下: python and jpython require at least version 2.1 and iron python 2. ...

  3. Swift UIImageView和UISlider组合

    /***************火焰图片Demo************start*******/ var imgView: UIImageView? override func viewDidLoa ...

  4. 伴随我整十个年头的校内网,现名 人人网, 是不是要shut down 了

    如题: 每天我都习惯性的登录人人网,虽然现在基本上已经看不到曾经的同学上线了,不过我还是有事没事的往上面post 一些出行的照片,没事无聊的时候上这个网上看看自己曾经的照片,虽然已经无人在线,但是自己 ...

  5. [agc23E]Inversions

    Atcoder description 给你\(n\)和\(\{a_i\}\),你需要求所有满足\(p_i\le a_i\)的\(1-n\)排列的逆序对个数之和模\(10^9+7\). \(n \le ...

  6. python学习(一)—简明python教程

    2016-04-12 15:59:47 1. 介绍2. 安装Python3. 最初的步骤4. 基本概念5. 运算符与表达式6. 控制流7. 函数8. 模块9. 数据结构10. 解决问题——编写一个Py ...

  7. hadoop之 Hadoop1.x和Hadoop2.x构成对比

      Hadoop1.x构成: HDFS.MapReduce(资源管理和任务调度):运行时环境为JobTracker和TaskTracker: Hadoop2.0构成:HDFS.MapReduce/其他 ...

  8. C++ Primer第五版答案

    Downloads Download the source files for GCC 4.7.0. Download the source code files for MS Visual Stud ...

  9. log4net 极简配置

    log4net的配置详解 分类: C#2013-10-01 22:45 5335人阅读 评论(4) 收藏 举报 log4net日志框架         前言:没买到1号回家的票,所以在祖国64岁生日之 ...

  10. 洛谷八连测R6

    本次测试暴0!!!还有两周就要考试啦!!! 看题目时觉得难度不大,就直接写正解,没有参照数据,导致测出的结果和预想有较大差距. 不过得到经验,不管题目难易(除了D1T1)都要参照数据一部分一部分写,那 ...