java Encryption&Decryption
The encryption class:
package cn.com.smartcost.qy.util; import java.security.Key;
import java.security.Security; import javax.crypto.Cipher; /**
* encrypt and decryption
* @author wangfeng
* @since 2013-4-27 15:50:26
* @version 1.0
*
*/
public class EncryptionDecryption {
private static String strDefaultKey = "wfkey"; //encrypt
private Cipher encryptCipher = null; // decryption
private Cipher decryptCipher = null; /**
* byte array to hexadecimal
* @param arrB
* @return 16
* @throws Exception
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception{
int bLen = arrB.length;
//
StringBuffer strBuffer = new StringBuffer(bLen*2);
for(int i=0; i != bLen; ++i){
int intTmp = arrB[i];
//
while(intTmp < 0){
intTmp = intTmp + 256;//
}
//
if(intTmp < 16){
strBuffer.append("0");
}
strBuffer.append(Integer.toString(intTmp,16));
}
return strBuffer.toString();
} /**
* hex to byte array
* @param hexStr
* @return
* @throws Exception
*/
public static byte[] hexStr2ByteArr(String hexStr) throws Exception{
byte[] arrB = hexStr.getBytes();
int bLen = arrB.length;
byte[] arrOut = new byte[bLen/2];
for(int i=0; i<bLen; i = i+2){
String strTmp = new String(arrB,i,2);
arrOut[i/2] = (byte)Integer.parseInt(strTmp,16);
}
return arrOut;
} /**
* encrypt
* @throws Exception
*/
public EncryptionDecryption() throws Exception {
this(strDefaultKey);
} /**
* encryption
* @param strKey
* @throws Exception
*/
@SuppressWarnings("restriction")
public EncryptionDecryption(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes()); encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
} /**
* encrypt
* @param arrB
* @return
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception{
return encryptCipher.doFinal(arrB);
} /**
* encrypt
* @param strIn
* @return
* @throws Exception
*/
public String encrypt(String strIn) throws Exception{
return byteArr2HexStr(encrypt(strIn.getBytes()));
} /**
* decrypt
* @param arrB
* @return
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception{
return decryptCipher.doFinal(arrB);
} /**
* decrypt
* @param strIn
* @return
* @throws Exception
*/
public String decrypt(String strIn) throws Exception{
try{
return new String(decrypt(hexStr2ByteArr(strIn)));
}catch (Exception e) {
return "";
}
} /**
* get the key
* @param arrBTmp
* @return
* @throws Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception{
byte[] arrB = new byte[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;
} }
Attention:
This class nees a jar:sunjce_provider.jar. u can download it from here: http://pan.baidu.com/s/1ntqkU4h
To use:
private EncryptionDecryption des = new EncryptionDecryption("wang");
//encrypt
String password = "123456";
password = des.encrypt(password);
//decrypt
password =des.decrypt(password);
java Encryption&Decryption的更多相关文章
- Simple XOR Encryption/Decryption in C++ (And Several Other Languages)
For details on how to implement XOR encryption using Go, see this post. If you are looking for XOR e ...
- In ZeroDB, the client is responsible for the database logic. Data encryption, decryption, and compression also happen client side. Therefore, the server never has any knowledge about the data, its str
zerodb/index.rst at master · zerodb/zerodb https://github.com/zerodb/zerodb/blob/master/docs/source/ ...
- Csharp and Vbscript: Encryption/Decryption Functional
1 /// <summary> 2 /// 塗聚文 3 /// 20130621 4 /// 自定义字符串加密解密 5 /// < ...
- AES encryption of files (and strings) in java with randomization of IV (initialization vector)
http://siberean.livejournal.com/14788.html Java encryption-decryption examples, I've seen so far in ...
- Java Tomcat7性能监控与优化详解
1. 目的 通过优化tomcat提高网站的并发能力. 2. 服务器资源 服务器所能提供CPU.内存.硬盘的性能对处理能力有决定性影响. 3. 优化配置 3.1. 配置tomcat管理员账户 ...
- C#/PHP Compatible Encryption (AES256) ZZ
Finding a way to encrypt messages in C# and decrypting them in PHP or vice versa seems to be a " ...
- AES advanced encryption standard 2
/* * FIPS-197 compliant AES implementation * * Copyright (C) 2006-2007 Christophe Devine * * Redistr ...
- String decryption with de4dot
Introduction de4dot is a wonderful tool for deobfuscating known and unknown .NET protections. Dealin ...
- Privacy-Preserving Deep Learning via Additively Homomorphic Encryption
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Full version of a paper at the 8-th International Conference on Appli ...
随机推荐
- 半夜两点灵光一现想出来的一个demo
功能: 1.用户通过页面下载Excel模板,按照模板填写数据,上传Excel , 服务器解析 ,绘制成折线图.柱状图.雷达图 ....... 2.用户在线编辑数据,绘图 (没想好咋弄) 可定制需求,根 ...
- Navicat导入sql server数据库
1.新建连接 2. EXEC sp_attach_db @dbname = 'demo', (数据库的名字) @filename1 = 'G:\C#\超市管理系统\DB\CSGL.mdf', @f ...
- ThinkPHP缓存技术(S(),F(),查询缓存,静态缓存)
直接查看原网址 https://blog.csdn.net/u010081689/article/details/47976271
- mysql中主外键关系
一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性 外键:是另一表的主键, ...
- python re(正则表达式模块)学习
一.简介 正则表达式本身是一种小型的.高度专业化的编程语言,而在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配.正则表达式模式被编译成一系列的字节码,然后由用C编写的匹配引擎 ...
- php 延迟静态绑定: static关键字
abstract class DomainObject { public static function create() { return new self(); } } class User ex ...
- Web开发——HTML基础(图像、音频和视频内容)
参考: 参考:HTML中的图像 参考:视频和音频内容 目录: 1.HTML中的图像 1.1 我们如何在网页上放置图像? (1)替代文字(alt) (2)宽度和高度 (3)图片标题 1.2 用图形和图形 ...
- 在centos7下安装python3的步骤
环境搭建 准备工具: centos7:http://mirror.bit.edu.cn/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1611.iso virtus ...
- Windows平台分布式架构实践 - 负载均衡(转载)
Windows平台分布式架构实践 - 负载均衡 概述 最近.NET的世界开始闹腾了,微软官方终于加入到了对.NET跨平台的支持,并且在不久的将来,我们在VS里面写的代码可能就可以通过Mono直接在Li ...
- 020-并发编程-java.util.concurrent之-jdk6/7/8中ConcurrentHashMap、HashMap分析
一.概述 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表. 是根据关键码值(Key ...