package com.ncs.pki.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
private static MessageDigest digest = null;
public synchronized static final String hash(String data) {
if (digest == null) {
try {
} catch (NoSuchAlgorithmException nsae) {
System.err.println(
+ "Jive will be unable to function normally.");
nsae.printStackTrace();
}
// Now, compute hash.
digest.update(data.getBytes());
return encodeHex(digest.digest());
public static final String encodeHex(byte[] bytes) {
int i;
for (i = 0; i bytes.length; i++) {
if (((int) bytes[i] 0xff) 0x10) {
buf.append("0");
return buf.toString();
public static String test(){
return null;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class DesEncrypt {
*
* 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.
* 方法: void getKey(String strKey)从strKey的字条生成一个Key
* String getEncString(String strMing)对strMing进行加密,返回String密文 String
* getDesString(String strMi)对strMin进行解密,返回String明文
*byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]
* byteD)byte[]型的解密
Key key;
* 根据参数生成KEY
* @param strKey
public void getKey(String strKey) {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
e.printStackTrace();
* 加密String明文输入,String密文输出
* @param strMing
* @return
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
byteMi = this.getEncCode(byteMing);
} finally {
byteMing = null;
byteMi = null;
return strMi;
* 解密 以String密文输入,String明文输出
* @param strMi
public String getDesString(String strMi) {
String strMing = "";
byteMing = this.getDesCode(byteMi);
return strMing;
* 加密以byte[]明文输入,byte[]密文输出
* @param byteS
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
cipher = null;
return byteFina;
* 解密以byte[]密文输入,以byte[]明文输出
* @param byteD
private byte[] getDesCode(byte[] byteD) {
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
System.out.println("des demo");
DesEncrypt des = new DesEncrypt();// 实例化一个对像
des.getKey("MYKEY");// 生成密匙
System.out.println("key=MYKEY");
String strEnc = des.getEncString("111111");// 加密字符串,返回String的密文
System.out.println("密文=" + strEnc);
String strDes = des.getDesString(strEnc);// 把String 类型的密文解密
System.out.println("明文=" + strDes);
package com.cube.limail.util;
import javax.crypto.SecretKey;/**
* 加密解密类
public class Eryptogram
{
private static String Algorithm ="DES";
//定义 加密算法,可用 DES,DESede,Blowfish
static boolean debug = false ;
* 构造子注解.
public Eryptogram ()
} /**
* 生成密钥
* @return byte[] 返回生成的密钥
* @throws exception 扔出异常.
public static byte [] getSecretKey () throws Exception
KeyGenerator keygen = KeyGenerator.getInstance (Algorithm );
SecretKey deskey = keygen.generateKey ();
System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
if (debug ) System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
return deskey.getEncoded ();
* 将指定的数据根据提供的密钥进行加密
* @param input 需要加密的数据
* @param key 密钥
* @return byte[] 加密后的数据
* @throws Exception
public static byte [] encryptData (byte [] input ,byte [] key ) throws Exception
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug )
System.out.println ("加密前的字符串:"+new String (input ));
} Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.ENCRYPT_MODE ,deskey );
byte [] cipherByte =c1.doFinal (input );
return cipherByte ;
* 将给定的已加密的数据通过指定的密钥进行解密
* @param input 待解密的数据
* @return byte[] 解密后的数据
public static byte [] decryptData (byte [] input ,byte [] key ) throws Exception
Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.DECRYPT_MODE ,deskey );
byte [] clearByte =c1.doFinal (input );
System.out.println ("解密后的字符串:"+(new String (clearByte )));
} return clearByte ;
* @param byte[] b 输入要转换的字节码
String hs ="";
String stmp ="";
for (int n =0 ;n b.length ;n ++)
stmp =(java.lang.Integer.toHexString (b [n ] 0XFF ));
if (stmp.length ()==1 ) hs =hs +"0"+stmp ;
else hs =hs +stmp ;
if (n b.length -1 ) hs =hs +":";
} return hs.toUpperCase ();
* 字符串转成字节数组.
* @param hex 要转化的字符串.
* @return byte[] 返回转化后的字符串.
public static byte[] hexStringToByte(String hex) {
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i len; i++) {
return result;
private static byte toByte(char c) {
return b;
* 字节数组转成字符串.
* @param String 要转化的字符串.
* @return 返回转化后的字节数组.
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i bArray.length; i++) {
sTemp = Integer.toHexString(0xFF bArray[i]);
sb.append(0);
sb.append(sTemp.toUpperCase());
return sb.toString();
* 从数据库中获取密钥.
* @param deptid 企业id.
* @return 要返回的字节数组.
* @throws Exception 可能抛出的异常.
public static byte[] getSecretKey(long deptid) throws Exception {
byte[] key=null;
String value=null;
//CommDao dao=new CommDao();
// List list=dao.getRecordList("from Key k where k.deptid="+deptid);
//if(list.size()0){
//value=((com.csc.sale.bean.Key)list.get(0)).getKey();
key=hexStringToByte(value);
//}
if (debug)
System.out.println("密钥:" + value);
return key;
String en = null;
byte[] key=hexStringToByte(this.key);
en = bytesToHexString(encryptData(data.getBytes(),key));
return en;
String de = null;
de = new String(decryptData(hexStringToByte(data),key));
return de;
} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //获得钥匙(字节数组)
byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //传入密码和钥匙,获得加密后的字节数组的密码
password=Eryptogram.bytesToHexString(tmp); //将字节数组转化为字符串,获得加密后的字符串密码解密与之差不多
Java中的IO流使用的是Decorator设计模式
所以只要写两个装饰者类
覆盖write和read方法
在write前和read后对原数据进行一些处理(比如异或操作)就可以了
我吃过饭写个贴上来......
--------------------------------------------------------
// EncryptStream.java
import java.io.IOException;
import java.io.OutputStream;
* 类型描述 加密流
* @author 何智刚
public class EncryptStream extends OutputStream {
private byte key;
private OutputStream out;
* @param in 需要加密的流
public EncryptStream(byte key, OutputStream out) {
this.key = key;
this.out = out;
@Override
public void write(int b) throws IOException {
out.write(b ^ key);
// DecryptStream.java
import java.io.InputStream;
* 类型描述 解密流
public class DecryptStream extends InputStream {
private InputStream in;
* @param in 需要解密的流
public DecryptStream(byte key, InputStream in) {
this.in = in;
public int read() throws IOException {
return in.read() ^ key;
public int read(byte[] b, int off, int len) throws IOException {
byte[] temp = new byte[b.length];
int c = in.read(temp, off, len);
for (int i = 0; i b.length; i++) {
b[i] = (byte) (temp[i] ^ key);
return c;
// Client.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Client {
public static void main(String[] args) throws Exception {
encryptFile("要加密的文件.dat", "加密过的文件.dat", key);
decryptFile("加密过的文件.dat", "解密出来的文件.dat", key);
* 方法描述 加密文件
* @param src 要加密的文件的路径
* @param des 加密过后的文件的存放路径
public static void encryptFile(String src, String des, byte key)
throws Exception {
InputStream in = new FileInputStream(src);
OutputStream out = new EncryptStream(key, new FileOutputStream(des));
int c;
while ((c = in.read(buf)) 0) {
out.write(buf, 0, c);
in.close();
out.flush();
out.close();
* 方法描述 解密文件
* @param src 要解密的文件的路径
* @param des 解密过后的文件的存放路径
public static void decryptFile(String src, String des, byte key)
InputStream in = new DecryptStream(key, new FileInputStream(src));
OutputStream out = new FileOutputStream(des);
-----------------------------------------------
我在例子里没有用BufferedStream,而是自己创建了个byte[]做缓冲区,因为这样性能更好.用BufferedStream的话代码会更简单.
以上就是土嘎嘎小编为大家整理的java密码流代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!