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的更多相关文章

  1. 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 ...

  2. 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/ ...

  3. Csharp and Vbscript: Encryption/Decryption Functional

      1 /// <summary>   2     /// 塗聚文   3     /// 20130621   4     /// 自定义字符串加密解密   5     /// < ...

  4. 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 ...

  5. Java Tomcat7性能监控与优化详解

    1.   目的 通过优化tomcat提高网站的并发能力. 2.   服务器资源 服务器所能提供CPU.内存.硬盘的性能对处理能力有决定性影响. 3.   优化配置 3.1. 配置tomcat管理员账户 ...

  6. 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 " ...

  7. AES advanced encryption standard 2

    /* * FIPS-197 compliant AES implementation * * Copyright (C) 2006-2007 Christophe Devine * * Redistr ...

  8. String decryption with de4dot

    Introduction de4dot is a wonderful tool for deobfuscating known and unknown .NET protections. Dealin ...

  9. Privacy-Preserving Deep Learning via Additively Homomorphic Encryption

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Full version of a paper at the 8-th International Conference on Appli ...

随机推荐

  1. HTTPclient 4.2.2 传参数和文件流

    package com.http; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodin ...

  2. [Day6]引用数据类型、ArrayList 集合

    1.类的定义与使用 (1)类的定义格式 创建java文件,与类名相同 public class 类名{ 数据类型  属性名称1: 数据类型  属性名称2: … } (2)使用格式 导包:我们将所有的类 ...

  3. python之以字符串形式导入模块

    示例 调用方法 class CorsMiddleware: def process(self): print('from auth.cors.CorsMiddleware.process') cors ...

  4. javascript常用的操作

    1.concat()  连接两个或更多的数组,并返回一个新的数组.注意:该方法不会改变原数组 var arry1=["李四",“王二”]:  var arry2=['赵柳',“李旺 ...

  5. JedisCluster简单使用

    项目中因为一些原因需要用到缓存,之前没有接触过,在此做一些简单的使用记录. 1.jedis在项目中依赖 <dependency> <groupId>redis.clients& ...

  6. Centos安装Oracle及问题处理

    安装Oracle前准备 创建运行oracle数据库的系统用户和用户组 [jonathan@localhost ~]$ su root #切换到root Password: [root@localhos ...

  7. 高并发架构系列:MQ消息队列的12点核心原理总结

    消息队列已经逐渐成为分布式应用场景.内部通信.以及秒杀等高并发业务场景的核心手段,它具有低耦合.可靠投递.广播.流量控制.最终一致性 等一系列功能. 无论是 RabbitMQ.RocketMQ.Act ...

  8. Kestrel:Net Core的跨平台服务器

    概述 Kestrel是一个基于libuv的跨平台ASP.NET Core web服务器,libuv是一个跨平台的异步I/O库.ASP.NET Core项目默认使用Kestrel作为web服务器. 用户 ...

  9. 12.C# 接口和抽象类的区别

    1.抽象类 声明方法的存在而不去实现它的类叫做抽象类,抽象类用abstract关键字声明.抽象类主要用来规定某些类的基本特征,继承它的子类必须实现抽象类的抽象成员,否则这个子类也为抽象类. publi ...

  10. 细说@Html.ActionLink()的用法

    一.@Html.ActionLink()概述 在MVC的Rasor视图引擎中,微软采用一种全新的方式来表示从前的超链接方式,它代替了从前的繁杂的超链接标签,让代码看起来更加简洁.通过浏览器依然会解析成 ...