字符串加密DES
提前关于加密的方式,我目前知道的有MD5,DES等等。今天写一下使用DES的代码,方便下次使用。
- package mocha.framework.hrbase.rest.utils;
- import java.security.Key;
- import javax.crypto.Cipher;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- /**
- *
- * 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.
- *
- * String getEncString(String strMing)对strMing进行加密,返回String密文 String
- * getDesString(String strMi)对strMin进行解密,返回String明文
- *
- * byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]
- * byteD)byte[]型的解密
- */
- public final class DES {
- //自定义密钥
- private static final String DES_KEY_STRING = "jingfen_SFTP";
- private static Key KEY;
- /**
- * 初始化密钥
- */
- static {
- initKey(DES_KEY_STRING);
- }
- private DES() {
- }
- private static DES des = new DES();
- public static DES getInstance() {
- return des;
- }
- /**
- * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
- *
- * @param arrBTmp
- * 构成该字符串的字节数组
- *
- * @return 生成的密钥
- * @throws Exception
- */
- private static Key getKey(byte[] arrBTmp) throws Exception {
- // 创建一个空的8位字节数组(默认值为0)
- byte[] arrB = new byte[8];
- // 将原始字节数组转换为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;
- }
- /**
- * 根据参数生成KEY
- *
- * @param strKey
- */
- private static void initKey(String strKey) {
- try {
- // KeyGenerator _generator = KeyGenerator.getInstance("DES");
- // _generator.init(new SecureRandom(strKey.getBytes()));
- KEY = getKey(strKey.getBytes());
- // _generator.generateKey();
- // _generator = null;
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 加密String明文输入,String密文输出
- *
- * @param strMing
- * @returno
- */
- public static String getEncString(String strMing) {
- byte[] byteMi = null;
- byte[] byteMing = null;
- String strMi = "";
- BASE64Encoder base64en = new BASE64Encoder();
- try {
- byteMing = strMing.getBytes("UTF8");
- byteMi = getEncCode(byteMing);
- strMi = base64en.encode(byteMi);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- base64en = null;
- byteMing = null;
- byteMi = null;
- }
- return strMi;
- }
- /**
- * 解密 以String密文输入,String明文输出
- *
- * @param strMi
- * @return
- */
- public static String getDesString(String strMi) {
- BASE64Decoder base64De = new BASE64Decoder();
- byte[] byteMing = null;
- byte[] byteMi = null;
- String strMing = "";
- try {
- byteMi = base64De.decodeBuffer(strMi);
- byteMing =getDesCode(byteMi);
- strMing = new String(byteMing, "UTF8");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- base64De = null;
- byteMing = null;
- byteMi = null;
- }
- return strMing;
- }
- /**
- * 加密以byte[]明文输入,byte[]密文输出
- *
- * @param byteS
- * @return
- */
- private static byte[] getEncCode(byte[] byteS) {
- byte[] byteFina = null;
- Cipher cipher;
- try {
- cipher = Cipher.getInstance("DES");
- cipher.init(Cipher.ENCRYPT_MODE, KEY);
- byteFina = cipher.doFinal(byteS);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- cipher = null;
- }
- return byteFina;
- }
- /**
- * 解密以byte[]密文输入,以byte[]明文输出
- *
- * @param byteD
- * @return
- */
- private static byte[] getDesCode(byte[] byteD) {
- Cipher cipher;
- byte[] byteFina = null;
- try {
- cipher = Cipher.getInstance("DES");
- cipher.init(Cipher.DECRYPT_MODE, KEY);
- byteFina = cipher.doFinal(byteD);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- cipher = null;
- }
- return byteFina;
- }
- public static void main(String[] args) {
- //加密字符串
- System.out.println(getEncString("22"));
- //解密密文
- System.out.println(getDesString("oEwwje/uXXo="));
- }
- }
字符串加密DES的更多相关文章
- C# 字符串加密解密方法
这个是加密的算法的命名空间,使用加密算法前要引用该程序集 System.Security.Cryptography using System;using System.Data;using Syst ...
- C# 字符串加密解密函数
原文:C# 字符串加密解密函数 using System; using System.Text;using System.Security.Cryptography; using System.IO; ...
- ASP.NET 常用的字符串加密
字符串常用的加密有三种 1.MD5加密,这个常用于密码,单向加密,不可解密,有些在线解密的可以解大部份,用代码不能实现,如果不想让人解密,加密后随便截取一段就好了: 2.Base64位加密,通常加密后 ...
- C#实现简单的字符串加密
最近用到一些字符串加密,而.net中提供的加密算法中用起来比较复杂,便简单的封装了一下,方便日后使用. public class Encrypt { static Enco ...
- 利用javascript对字符串加密
没事利用js写个对字符串加密的方法,基本原理就是先把字符串转化成对应的unicode(用到的方法是charCodeAt()),再把unicode统一减去100(这里加减随便你取多少),把得到的unic ...
- iOS字符串加密至MD5&及获取文件MD5
iOS 字符串加密至MD5 #import <CommonCrypto/CommonDigest.h> + (NSString *) md5:(NSString *)str { const ...
- Labview实现字符串加密
Labview实现字符串加密 对字符串进行加密,规则是每个字母后移5 位 例如A 变为F,b 变为g,x 变为c,y 变为d- 实现效果 后端实现
- Dotfuscator可以实现混淆代码、变量名修改、字符串加密
C#编写的代码如果不进行一定程度的混淆和加密,那么是非常容易被反编译进行破解的,特别是对于一些商业用途的C#软件来说,因为盯着的人多,更是极易被攻破.使用VS自带的Dotfuscator可以实现混淆代 ...
- 敏感字符串加密处理(PHP实现)
/** * 敏感字符串加密处理 * @param $raw_str 原始字符串 * @param $before 前面保留的显示位数 * @param $after 后面保留的显示位数 * @para ...
- nefu 1116 字符串加密
字符串加密 Problem : 1116 Time Limit : 1000ms Memory Limit : 65536K description 给你一段经过加密的字符串,我们称之为密文,现在请你 ...
随机推荐
- 异常:java.io.FileNotFoundException:e:\demo(拒绝访间。)
禁止向目录中写数据,只能向文件写数据
- 从热爱到深耕,全国Top10开源软件出品人手把手教你如何做开源
摘要:DTT直播邀请到管雷鸣与广大开发者分享"如何在开源领域找到适合自己的路". "想象一下,你写的代码被越来越多的人使用,并极大地帮助他们提高了开发效率和稳定性.&qu ...
- day05-优惠券秒杀01
功能03-优惠券秒杀01 4.功能03-优惠券秒杀 4.1全局唯一ID 4.1.1全局ID生成器 每个店铺都可以发布优惠券: 当用户抢购时,就会生成订单,并保存到tb_voucher_order这张表 ...
- ElementPlus 组件全局配置
友链:语雀,在线文档协同平台 官方提供的全局配置:Config Provider 本文只做简单的模板参考,具体的配置请根据自己的业务灵活设置,如果你使用的是其它的ui框架,原理应该都差不多 入口文件的 ...
- Django笔记三十五之admin后台界面介绍
本文首发于公众号:Hunter后端 原文链接:Django笔记三十五之admin后台界面介绍 这一篇介绍一下 Django 的后台界面使用. Django 自带了一套后台管理界面,可用于我们直接操作数 ...
- Django-3:创建子项目APP
django-admin startapp app01 或 python manage.py startapp app01 #app01 是app名称 PyCharm的样子:
- 记一次排查:接口返回值写入excel后,从单元格copy出来的数据会带有多重引号的问题
在项目里刚好有3个服务,同一个网关内层的3个服务,两个php的,一个golang的,为了提高负载以及进行分流,部分客户的接口调用会被网关自动分配到go服务. 恰好为了测试,我写了一个全量用户的生产.测 ...
- phpstudy-sqlilabs-less-3
题目:GET - Error based - Single quotes with twist 基于错误的单引号GET型变形注入 ?id=1 )and 1=2--+ ?id=1 "and 1 ...
- MySQL-DQL
准备测试表,先跟着执行下面的SQL #1.登录MySQL后 #2.创建test_database数据库,不存在则创建 create database if not exists test_databa ...
- K8S in Action 读后感(概念简介)
一.K8S的用武之地 今天,大型单体应用正被逐渐拆分成小的.可独立运行的组件,我们称之为微服务.微服务彼此之间解耦,所以它们可以被独立开发.部署.升级.伸缩.这使得我们可以对每一个微服务实现快速迭代, ...