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 ...
随机推荐
- 面试体验:Facebook 篇(转)
http://www.cnblogs.com/cathsfz/archive/2012/11/05/facebook-interview-experience.html 2012-11-05 08:2 ...
- 40个最好的Tumblr主题
如果安装了一款较好的Tumblr主题,你的Tumblr空间将焕然一新.然而找到一款合适的主题并不是一件容易的事,这正是本文中我整理那么多优质的Tumblr模板作为灵感的原因.其中有一些免费的Tumbl ...
- windows7__32位下安装python2.6.6
1.下载windows7__32位的python2.6.6.mis文件,直接运行.默认安装即可 2.设置系统环境变量,目的在cmd下能敲python后能够自动调用到安装目录程序 设计如下:(我的电脑- ...
- Spark及其应用场景初探
最近老大让用Spark做一个ETL项目,搭建了一套只有三个结点Standalone模式的Spark集群做测试,基础数据量大概8000W左右.看了官方文档,Spark确实在Map-Reduce上提升了很 ...
- iOS tableview 优化总结
根据网络上的优化方法进行了总括.并未仔细进行语言组织.正在这些优化方法进行学习,见另一篇文章 提高app流畅度 1.cell子控件创建写在 initWithStyle:reuseIdentifier ...
- ajax 模仿百度下拉
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 转】MyEclipse10安装Log4E插件
原博文出自于:http://www.cnblogs.com/xdp-gacl/p/4231812.html 感谢! 一. Log4E插件下载 下载地址:http://log4e.jayefem.de/ ...
- 【转】eclipse.ini内存设置
-vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M 这里有几个问题:1. 各个参数的含义什么?2. 为什么有的机器我将-Xm ...
- AutoCAD.NET二次开发错误集锦
1.CAD崩溃,错误“unhandled access violation reading”=“未将对象引用设置到对象的实例”. 2. 建模操作错误:指向给定边的指针为空. 建模操作错误:访问冲突. ...
- class bool
class bool(int): """ bool(x) -> bool Returns True when the argument x is true, Fal ...