提前关于加密的方式,我目前知道的有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的更多相关文章

  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. CentOS7---部署Tomcat和安装Jpress

    总览需求 1. 简述静态网页和动态网页的区别. 2. 简述 Webl.0 和 Web2.0 的区别. 3. 安装tomcat8,配置服务启动脚本,部署jpress应用. 1.简述静态网页和动态网页的区 ...

  2. Redis分布式锁这样用,有坑?

    背景 在微服务项目中,大家都会去使用到分布式锁,一般也是使用Redis去实现,使用RedisTemplate.Redisson.RedisLockRegistry都行,公司的项目中,使用的是Redis ...

  3. 企业应用可观测性利器!华为云CodeArts APM发布

    摘要:近日,华为云全链路应用性能管理服务CodeArts APM全新上线,提供端到端的全链路性能管理服务,涵盖前端监控.应用性能监控,全面拥抱开源生态. 本文分享自华为云社区<企业应用可观测性利 ...

  4. [C++核心编程] 4.3、类和对象-C++对象模型和this指针

    文章目录 4.3 C++对象模型和this指针 4.3.1 成员变量和成员函数分开存储 4.3.2 this指针概念 4.3.3 空指针访问成员函数 4.3.4 const修饰成员函数 4.3 C++ ...

  5. Java的构造方法和标准JavaBean

    构造方法 一.构造方法概述: 构造方法也叫做构造器,构造函数,平时叫做构造方法 二.构造方法的作用: 创建对象的时候,由虚拟机自动调用,给成员变量进行初始化(赋值) 三.构造方法的格式: public ...

  6. 2020-12-26:mysql中,表person有字段id、name、age、sex,id是主键,name是普通索引,age和sex没有索引。select * from person where id=1 and name='james' and age=1 and sex=0。请问这条语句有几次回表?

    2020-12-26:mysql中,表person有字段id.name.age.sex,id是主键,name是普通索引,age和sex没有索引.select * from person where i ...

  7. Play to Earn Games

    什么是P2E游戏 P2E 游戏(Play to Earn Games)指的是在区块链游戏中,玩家可以通过完成任务.收获资源.挖矿或游戏中的其他活动以获得成就来赚取游戏内的资产(NFT)或代币(Toke ...

  8. 【GiraKoo】Git工具使用指南

    Git工具使用指南 Git是一个分布式版本控制工具,可以用于管理代码.本文介绍了如何使用git工具. 1. SVN和Git的区别 1.1 SVN SVN是集中式版本控制工具,所有的代码都存储在一个中央 ...

  9. svn is already locked 最终解决方案

    今日执行项目更新时,手贱点击了cancel 中断了操作,最后导致项目被锁,杯具了. 首先想到了Clean up 直接提示 看来不行呀 -// 省略 n 多种尝试 最后使用删除db 中的 lock 表来 ...

  10. git从配置到使用

    一 .安装git 1.1 官方地址为:https://git-scm.com/download/win 1.2 双击下载的.exe文件 1.3 直接下一步 1.4 自定义安装目录 1.5 勾选命令行和 ...