字符串加密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 给你一段经过加密的字符串,我们称之为密文,现在请你 ...
随机推荐
- Centos7 开机时遇到initramfs-xxx.img not found错误导致虚拟机无法开启问题处理
1.背景 一台运行在Esxi上面的VM重启后报initramfs-xxx.img not found错误. 按任意键后出现以下错误. 之前在运维Centos7的时候解决过Kernel panic - ...
- 论文解析 -- A Survey of AIOps Methods for Failure Management
此篇Survey是A Systematic Mapping Study in AIOps的后续研究 对于AIOPS中占比较高的Failure Management进行进一步的研究 Compared t ...
- c# 异步进阶———— paralel [二]
前言 简单整理一下paralel,以上是并行的意思. 正文 我们在工作中常常使用task await 和 async,也就是将线程池进行了封装,那么还有一些更高级的应用. 是对task的封装,那么来看 ...
- [白嫖]阿里云服务器ECS以及无影云桌面
[白嫖]aliyun 阿里云ECS和无影云桌面 [云服务器 ECS]学生认证约7个月 适合入门级开发者,用于搭建网站.小程序.云博客.部署主流开发环境,如 lamp.node.js.wordpress ...
- 【故障公告】被放出的 Bing 爬虫,又被爬宕机的园子
这些巨头爬虫们现在怎么了?记忆中2022年之前的十几年,园子没有遇到过被巨头爬虫们爬宕机的情况,巨头们都懂得爱护,都懂得控制节奏,都懂得在爬网时控制并发连接数以免给目标网站造成过大压力. 从去年开始, ...
- 数据分析01-(numpy概述及使用)
数据分析-01 数据分析 numpy numpy概述 numpy`历史` numpy的核心:多维数组 numpy基础 ndarray数组 内存中的ndarray对象 ndarray数组对象的特点 nd ...
- Spring 之bean的生命周期
文章目录 IOC Bean的生命周期 运行结果 实例演示 实体类 实例化前后置代码 初始化的前后置代码 application.xml 总结 今天我们来聊一下Spring Bean的生命周期,这是一个 ...
- Android-图片压缩(二)-纯干货
Android - 图片压缩(一)- 项目中取图片转bitmap Android - 图片压缩(二)- 纯干货 前言:让我们手撸一个图片压缩库,对压缩工具鲁班进行升级改造. 在平常开发当中,我们一般是 ...
- vue项目使用rem布局刷新页面瞬间元素尺寸由小变大,页面闪现错乱样式
vue项目使用px2remLoader插件,在index.html自定义设置font-size的大小,尤其是在首屏加载的时候,会出现页面各个元素尺寸由小变大的一个过程,很难看 刚开始一直在想是不是因为 ...
- 2022-11-28:给定两个数组A和B,比如 A = { 0, 1, 1 } B = { 1, 2, 3 } A[0] = 0, B[0] = 1,表示0到1有双向道路 A[1] = 1, B[1]
2022-11-28:给定两个数组A和B,比如 A = { 0, 1, 1 } B = { 1, 2, 3 } A[0] = 0, B[0] = 1,表示0到1有双向道路 A[1] = 1, B[1] ...