DES加密解密(适用Windows和Linux系统)防止linux下解密失败
转自:http://blog.csdn.net/jerry_bj/article/details/8276552
package com.lasun.util; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom; import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; import com.lasun.core.services.ParameterManager; public class DESUtil {
Key key; public DESUtil() { } public DESUtil(String str) {
setKey(str); // 生成密匙
} public Key getKey() {
return key;
} public void setKey(Key key) {
this.key = key;
} <span style="color:#FF0000;"> /**
* 根据参数生成 KEY
*/
public void setKey (String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
//防止linux下 随机生成key
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(strKey.getBytes()); _generator.init(56,secureRandom);
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
}
}</span> <span style="color:#3366FF;">/**
* 根据参数生成 KEY
*/
/*public void setKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
}
}*/</span> /**
* 加密 String 明文输入 ,String 密文输出
*/
public String encryptStr(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.encryptByte(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
} /**
* 解密 以 String 密文输入 ,String 明文输出
*
* @param strMi
* @return
*/
public String decryptStr(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.decryptByte(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
} /**
* 加密以 byte[] 明文输入 ,byte[] 密文输出
*
* @param byteS
* @return
*/
private byte[] encryptByte(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
} /**
* 解密以 byte[] 密文输入 , 以 byte[] 明文输出
*
* @param byteD
* @return
*/
private byte[] decryptByte(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing SqlMap class. Cause: " + e);
} finally {
cipher = null;
}
return byteFina;
} public static void main(String[] args) throws Exception {
DESUtil des = new DESUtil("");
// DES 加密文件
// des.encryptFile("G:/test.doc", "G:/ 加密 test.doc");
// DES 解密文件
// des.decryptFile("G:/ 加密 test.doc", "G:/ 解密 test.doc");
String str1 = "jui8qayuh7yhqwsh";
// DES 加密字符串
String str2 = des.encryptStr(str1);
// DES 解密字符串
String deStr = des.decryptStr(str2);
System.out.println(" 加密前: " + str1);
System.out.println(" 加密后: " + str2);
System.out.println(" 加密后长度: " + str2.length());
System.out.println(" 解密后: " + deStr);
}
}
说明:蓝色部分(注释掉的)代码 在windows下面好使,但是在linux下面会导致每次生成的key不一样、也就是说加密之后再解密就会保存,网上说由于两个操作系统处理补充字节默认补齐的方式不同,所以出现这样的情况;
红色部分代码防止在linux下随机生成key,在两个操作系统下通用。
DES加密解密(适用Windows和Linux系统)防止linux下解密失败的更多相关文章
- linux系统Centos环境下搭建SVN服务器及权限配置
linux系统Centos环境下如何搭建SVN服务器以及svnserve.conf.authz.passwd配置文件详细介绍 至于svn的概念,这里就不做详细阐述了,可以自行百度.简单来讲就是一个 ...
- Linux中使用python测试主机存活 Linux系统CentOS Linux release 7.3.1611 (Core) py版本Python 2.7.5
下面是最初的情况 #/usr/bin/env python # -*- coding: utf-8 -*- import os import time import subprocess import ...
- 2021年3月-第01阶段-Linux基础-Linux系统概念-Linux命令
Linux系统基本概念 图形界面: Ctrl+Shift +号 //调整命令终端变大 Ctrl - 号 //调整命令终端变小 命令终端: ~ 家目录:用户的私有场所,其他用户不能随便访问 root超级 ...
- 【Linux 系统】Linux探秘之用户态与内核态
一. Unix/Linux的体系架构 如上图所示,从宏观上来看,Linux操作系统的体系架构分为用户态和内核态(或者用户空间和内核).内核从本质上看是一种软件——控制计算机的硬件资源,并提供上层应用程 ...
- linux 系统 vi编辑器下的删除
vi filename 进入vi模式 首先 最常用的 dd:删除 光标所在的整行: d1G: 删除光标所在到第一行的所有数据: dG: 删除光标到最后一行的所有数据 : d$:删除光标到 ...
- (win+linux)双系统,删除linux系统的条件下,删除grub引导记录,恢复windows引导
//(hdx,y) (显示查找到的分区号)第一个数字指第几个硬盘,第二个指第几个分区. 一般我们是(hd0,0) \n Linux的分区已经被你从Windows中删除,系统启动后停在“grub&g ...
- linux系统应用--Linux下用virtualBox安装win7(共享文件夹)
1. deepin终端: sudo apt-get install virtualbox 2. 下载win7 iso文件 3. deepin终端启动virtualbox : ./virtualbo ...
- Linux系统修改Home下的目录为英文
修改Home下的目录为英文 修改目录映射文件名: vim .config/user-dirs.dirs 修改如下:XDG_DESKTOP_DIR="$HOME/Desktop"XD ...
- linux 系统统计目录下文件夹的大小
du -ah --max-depth=1 这个是我想要的结果 a表示显示目录下所有的文件和文件夹(不含子目录),h表示以人类能看懂的方式,max-depth表示目录的深度. du命令用来查看 ...
- Linux系统:centos7下安装Jdk8、Tomcat8、MySQL5.7环境
一.JDK1.8 环境搭建 1.上传文件解压 [root@localhost mysoft]# tar -zxvf jdk-8u161-linux-x64.tar.gz [root@localhost ...
随机推荐
- 新霸哥带你进入java的世界
新霸哥从近期大家的留言中注意到了大家对基础知识比较重视,很多的朋友希望多讲一些入门的知识,为了满足广大开发爱好者的需求,新霸哥决定从最基础的做起,一点一点的帮助大家一起走进云计算的世界.下面新霸哥首先 ...
- TopFreeTheme精选免费模板【20130827】
今天我们整理了一些关于WordPress, Joomla, Drupal, Magento, OpenCart的最新免费模板主题,绝大多数都是来自ThemeForest的响应式风格模板主题.题材多样化 ...
- java多线程之synchronized(线程同步)
一.线程同步,主要应用synchronized关键字: public class TraditionalThreadSynchorinzed { public static void main(Str ...
- [iOS微博项目 - 3.2] - 发送微博
github: https://github.com/hellovoidworld/HVWWeibo A.使用微博API发送微博 1.需求 学习发送微博API 发送文字微博 发送带有图片的微博 ...
- HDU 3853LOOPS(简单概率DP)
HDU 3853 LOOPS 题目大意是说人现在在1,1,需要走到N,N,每次有p1的可能在元位置不变,p2的可能走到右边一格,有p3的可能走到下面一格,问从起点走到终点的期望值 这是弱菜做的第 ...
- C#中的DllImport
大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比如 Windows中的一些功能,C++中已经编写好的一些方法)要重新编写代码,C#有没有方法可以直接都用这些原本已经存在的功 ...
- ]用EnumChildWindows遍历窗口的方法
最近项目有需要,得到一个非自己实现的窗口控件对象.于是想起曾经做过类似功能.总结如下: 调用EnumChildWindows(this->m_hWnd, EnumChildProc, NULL) ...
- redis安装配置与测试
phpredis: https://github.com/nicolasff/phpredis http://www.cnblogs.com/ikodota/archive/2012/03/05/ph ...
- ELF学习--重定位文件
add.c int data = 1;int bss;const int rodata = 1;int add(int num1, int num2){ int sum = 0; sum = num1 ...
- SQL Server sql 操作
1.重命名表将表OLD重命名为NEW: EXEC sp_rename 'OLD','NEW' 2.重命名列将表table1中的列old重命名为new: EXEC sp_rename 'table1.o ...