提前关于加密的方式,我目前知道的有MD5,DES等等。今天写一下使用DES的代码,方便下次使用。

  1. package mocha.framework.hrbase.rest.utils;
  2.  
  3. import java.security.Key;
  4.  
  5. import javax.crypto.Cipher;
  6.  
  7. import sun.misc.BASE64Decoder;
  8. import sun.misc.BASE64Encoder;
  9.  
  10. /**
  11. *
  12. * 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.
  13. *
  14. * String getEncString(String strMing)对strMing进行加密,返回String密文 String
  15. * getDesString(String strMi)对strMin进行解密,返回String明文
  16. *
  17. * byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]
  18. * byteD)byte[]型的解密
  19. */
  20.  
  21. public final class DES {
  22.  
  23. //自定义密钥
  24. private static final String DES_KEY_STRING = "jingfen_SFTP";
  25. private static Key KEY;
  26.  
  27. /**
  28. * 初始化密钥
  29. */
  30. static {
  31. initKey(DES_KEY_STRING);
  32. }
  33.  
  34. private DES() {
  35. }
  36.  
  37. private static DES des = new DES();
  38.  
  39. public static DES getInstance() {
  40. return des;
  41. }
  42.  
  43. /**
  44. * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
  45. *
  46. * @param arrBTmp
  47. * 构成该字符串的字节数组
  48. *
  49. * @return 生成的密钥
  50. * @throws Exception
  51. */
  52. private static Key getKey(byte[] arrBTmp) throws Exception {
  53. // 创建一个空的8位字节数组(默认值为0)
  54. byte[] arrB = new byte[8];
  55. // 将原始字节数组转换为8位
  56. for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
  57. arrB[i] = arrBTmp[i];
  58. }
  59. // 生成密钥
  60. Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
  61. return key;
  62. }
  63.  
  64. /**
  65. * 根据参数生成KEY
  66. *
  67. * @param strKey
  68. */
  69. private static void initKey(String strKey) {
  70. try {
  71. // KeyGenerator _generator = KeyGenerator.getInstance("DES");
  72. // _generator.init(new SecureRandom(strKey.getBytes()));
  73. KEY = getKey(strKey.getBytes());
  74. // _generator.generateKey();
  75. // _generator = null;
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. }
  80.  
  81. /**
  82. * 加密String明文输入,String密文输出
  83. *
  84. * @param strMing
  85. * @returno
  86. */
  87. public static String getEncString(String strMing) {
  88. byte[] byteMi = null;
  89. byte[] byteMing = null;
  90. String strMi = "";
  91. BASE64Encoder base64en = new BASE64Encoder();
  92. try {
  93. byteMing = strMing.getBytes("UTF8");
  94. byteMi = getEncCode(byteMing);
  95. strMi = base64en.encode(byteMi);
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. } finally {
  99. base64en = null;
  100. byteMing = null;
  101. byteMi = null;
  102. }
  103. return strMi;
  104. }
  105.  
  106. /**
  107. * 解密 以String密文输入,String明文输出
  108. *
  109. * @param strMi
  110. * @return
  111. */
  112. public static String getDesString(String strMi) {
  113. BASE64Decoder base64De = new BASE64Decoder();
  114. byte[] byteMing = null;
  115. byte[] byteMi = null;
  116. String strMing = "";
  117. try {
  118. byteMi = base64De.decodeBuffer(strMi);
  119. byteMing =getDesCode(byteMi);
  120. strMing = new String(byteMing, "UTF8");
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. } finally {
  124. base64De = null;
  125. byteMing = null;
  126. byteMi = null;
  127. }
  128. return strMing;
  129. }
  130.  
  131. /**
  132. * 加密以byte[]明文输入,byte[]密文输出
  133. *
  134. * @param byteS
  135. * @return
  136. */
  137. private static byte[] getEncCode(byte[] byteS) {
  138. byte[] byteFina = null;
  139. Cipher cipher;
  140. try {
  141. cipher = Cipher.getInstance("DES");
  142. cipher.init(Cipher.ENCRYPT_MODE, KEY);
  143. byteFina = cipher.doFinal(byteS);
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. } finally {
  147. cipher = null;
  148. }
  149. return byteFina;
  150. }
  151.  
  152. /**
  153. * 解密以byte[]密文输入,以byte[]明文输出
  154. *
  155. * @param byteD
  156. * @return
  157. */
  158. private static byte[] getDesCode(byte[] byteD) {
  159. Cipher cipher;
  160. byte[] byteFina = null;
  161. try {
  162. cipher = Cipher.getInstance("DES");
  163. cipher.init(Cipher.DECRYPT_MODE, KEY);
  164. byteFina = cipher.doFinal(byteD);
  165. } catch (Exception e) {
  166. e.printStackTrace();
  167. } finally {
  168. cipher = null;
  169. }
  170. return byteFina;
  171.  
  172. }
  173.  
  174. public static void main(String[] args) {
  175. //加密字符串
  176. System.out.println(getEncString("22"));
  177. //解密密文
  178. System.out.println(getDesString("oEwwje/uXXo="));
  179. }
  180.  
  181. }

  

字符串加密DES的更多相关文章

  1. C# 字符串加密解密方法

    这个是加密的算法的命名空间,使用加密算法前要引用该程序集  System.Security.Cryptography using System;using System.Data;using Syst ...

  2. C# 字符串加密解密函数

    原文:C# 字符串加密解密函数 using System; using System.Text;using System.Security.Cryptography; using System.IO; ...

  3. ASP.NET 常用的字符串加密

    字符串常用的加密有三种 1.MD5加密,这个常用于密码,单向加密,不可解密,有些在线解密的可以解大部份,用代码不能实现,如果不想让人解密,加密后随便截取一段就好了: 2.Base64位加密,通常加密后 ...

  4. C#实现简单的字符串加密

    最近用到一些字符串加密,而.net中提供的加密算法中用起来比较复杂,便简单的封装了一下,方便日后使用.     public class Encrypt    {        static Enco ...

  5. 利用javascript对字符串加密

    没事利用js写个对字符串加密的方法,基本原理就是先把字符串转化成对应的unicode(用到的方法是charCodeAt()),再把unicode统一减去100(这里加减随便你取多少),把得到的unic ...

  6. iOS字符串加密至MD5&及获取文件MD5

    iOS 字符串加密至MD5 #import <CommonCrypto/CommonDigest.h> + (NSString *) md5:(NSString *)str { const ...

  7. Labview实现字符串加密

    Labview实现字符串加密 对字符串进行加密,规则是每个字母后移5 位 例如A 变为F,b 变为g,x 变为c,y 变为d- 实现效果 后端实现

  8. Dotfuscator可以实现混淆代码、变量名修改、字符串加密

    C#编写的代码如果不进行一定程度的混淆和加密,那么是非常容易被反编译进行破解的,特别是对于一些商业用途的C#软件来说,因为盯着的人多,更是极易被攻破.使用VS自带的Dotfuscator可以实现混淆代 ...

  9. 敏感字符串加密处理(PHP实现)

    /** * 敏感字符串加密处理 * @param $raw_str 原始字符串 * @param $before 前面保留的显示位数 * @param $after 后面保留的显示位数 * @para ...

  10. nefu 1116 字符串加密

    字符串加密 Problem : 1116 Time Limit : 1000ms Memory Limit : 65536K description 给你一段经过加密的字符串,我们称之为密文,现在请你 ...

随机推荐

  1. 异常:java.io.FileNotFoundException:e:\demo(拒绝访间。)

    禁止向目录中写数据,只能向文件写数据

  2. 从热爱到深耕,全国Top10开源软件出品人手把手教你如何做开源

    摘要:DTT直播邀请到管雷鸣与广大开发者分享"如何在开源领域找到适合自己的路". "想象一下,你写的代码被越来越多的人使用,并极大地帮助他们提高了开发效率和稳定性.&qu ...

  3. day05-优惠券秒杀01

    功能03-优惠券秒杀01 4.功能03-优惠券秒杀 4.1全局唯一ID 4.1.1全局ID生成器 每个店铺都可以发布优惠券: 当用户抢购时,就会生成订单,并保存到tb_voucher_order这张表 ...

  4. ElementPlus 组件全局配置

    友链:语雀,在线文档协同平台 官方提供的全局配置:Config Provider 本文只做简单的模板参考,具体的配置请根据自己的业务灵活设置,如果你使用的是其它的ui框架,原理应该都差不多 入口文件的 ...

  5. Django笔记三十五之admin后台界面介绍

    本文首发于公众号:Hunter后端 原文链接:Django笔记三十五之admin后台界面介绍 这一篇介绍一下 Django 的后台界面使用. Django 自带了一套后台管理界面,可用于我们直接操作数 ...

  6. Django-3:创建子项目APP

    django-admin startapp app01 或 python manage.py startapp app01 #app01 是app名称 PyCharm的样子:

  7. 记一次排查:接口返回值写入excel后,从单元格copy出来的数据会带有多重引号的问题

    在项目里刚好有3个服务,同一个网关内层的3个服务,两个php的,一个golang的,为了提高负载以及进行分流,部分客户的接口调用会被网关自动分配到go服务. 恰好为了测试,我写了一个全量用户的生产.测 ...

  8. phpstudy-sqlilabs-less-3

    题目:GET - Error based - Single quotes with twist 基于错误的单引号GET型变形注入 ?id=1 )and 1=2--+ ?id=1 "and 1 ...

  9. MySQL-DQL

    准备测试表,先跟着执行下面的SQL #1.登录MySQL后 #2.创建test_database数据库,不存在则创建 create database if not exists test_databa ...

  10. K8S in Action 读后感(概念简介)

    一.K8S的用武之地 今天,大型单体应用正被逐渐拆分成小的.可独立运行的组件,我们称之为微服务.微服务彼此之间解耦,所以它们可以被独立开发.部署.升级.伸缩.这使得我们可以对每一个微服务实现快速迭代, ...