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 ...
随机推荐
- python使用matplotlib绘制折线图教程
Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.下面这篇文章主要介绍了python使用matplot ...
- java web应用连接mysql会突然connection连接失败
tomcat6.0 mysql5.1 项目:java web项目 问题:原本项目运行了好几天了,一直没发现问题,突然今天报数据库连接异常,进入看日志发现 ### Error querying data ...
- Linux命令 umask
umask: 文件预设权限 指定当前用户在创建文件或目录时的权限默认值. $ umask0002$ umask -Su=rwx,g=rwx,o=rx 创建文件时,预设没有x 权限,即只有rw 权限,最 ...
- Golang中mac地址+时间戳加入rand.Seed()产生随机数
记录一下用mac地址+local时间作为seed来产生随机数 // 首先记录一下rand.Seed()怎么用 // 官方说明,传入int64数据为Seed func (r *Rand) Seed(se ...
- Delphi 中的 XMLDocument 类详解(10) - 判断节点类型: 支节点、叶节点、文本节点、空节点
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...
- AnyConnect无法与指定的安全网关建立连接(转)
原文:https://www.cnblogs.com/dumuqiao/p/4020547.html Cisco的VPN客户端最近报“AnyConnect was not able to establ ...
- 一个少女心满满的例子带你入门canvas
https://blog.csdn.net/sunshine940326/article/details/76572850 本文首发于我的个人博客:http://cherryblog.site/ gi ...
- Nginx、Tomcat配置https
一.Nginx.Tomcat配置https 前提就是已经得到了CA机构颁发的证书 一.合并证书 1.假设证书文件如下 秘钥文件server.key,证书CACertificate-INTERMEDIA ...
- python进阶(三) 内建函数getattr工厂模式
getattr()这个方法最主要的作用是实现反射机制.也就是说可以通过字符串获取方法实例. 传入不同的字符串,调用的方法不一样. 原型:getattr(对象,方法名) 举个栗子: pyMethod类 ...
- 利用spring实现服务启动就自动执行某些操作的2种方式
第一种方式,用bean的init-method属性 <bean class="com.emax.paycenter.log.LogBridge" init-method=&q ...