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. 计算属性和监听,computed,watch

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. SpringBoot:四种读取properties文件的方式

    前言 在项目开发中经常会用到配置文件,配置文件的存在解决了很大一份重复的工作.今天就分享四种在Springboot中获取配置文件的方式. 注:前三种测试配置文件为springboot默认的applic ...

  3. 泡泡一分钟:Semantic Labeling of Indoor Environments from 3D RGB Maps

    张宁 Semantic Labeling of Indoor Environments from 3D RGB Maps Manuel Brucker,  Maximilian Durner,  Ra ...

  4. ECMAScript基础

    概念: 1):区分大小写 2):变量是弱类型的. 3):每行结尾的分号可有可无 4):注释与Java,C和PHP语言的注释相同 5):括号表明代码块 原始值:是存储在栈中的简单数据段,也就是说他们的值 ...

  5. 探究Java中的锁

    一.锁的作用和比较 1.Lock接口及其类图 Lock接口:是Java提供的用来控制多个线程访问共享资源的方式. ReentrantLock:Lock的实现类,提供了可重入的加锁语义 ReadWrit ...

  6. nlp底层技术列举

    其实目前除了之前博客写到的一些关于自然语言处理用到的知识点之外,很多其他nlp技术只是会用但是不了解原理,先整体分个类,之后再仔细分析吧. 上图是https://www.sohu.com/a/1386 ...

  7. 小型网站使用高德地图开发定位模块需要的php代码

    项目要求: 1.使用定位,获取gps信息 2.获取当前所在城市 3.从该城市中取数据,按照距离我的当前位置远近排序 方案 1.使用js获取当前位置信息,然后使用cookie或者session存储 fu ...

  8. JVM(一):方法区

    方法区(Method Area) 在JVM中,类型信息和类静态变量都保存在方法区中,需要注意的一点是,常量池也存放于方法区中. 类型信息包括: 1.类型的全名(The fully qualified ...

  9. here文档 here doc EOF重定向

    here文档  here doc EOF重定向 http://www.cnblogs.com/xiangzi888/archive/2012/03/24/2415077.html 在shell脚本程序 ...

  10. python基础(11)-常用模块

    re(正则)模块 常用方法 findall() 以列表返回所有满足条件的结果 import re print(re.findall('\d','a1b2c2abc123'))#['1', '2', ' ...