java明文解密源代码 java 加解密开源工具类( 二 )


StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; ibArray.length; i++) {
sTemp = Integer.toHexString(0xFFbArray[i]);
if (sTemp.length()2)
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=https://www.04ip.com/post/null;
//CommDao dao=new CommDao();
// List list=dao.getRecordList("from Key k where k.deptid="+deptid);
//if(list.size()0){
//value=https://www.04ip.com/post/((com.csc.sale.bean.Key)list.get(0)).getKey();
valuehttps://www.04ip.com/post/= "https://www.04ip.com/post/CB7A92E3D3491964";
key=hexStringToByte(value);
//}
if (debug)
System.out.println("密钥:" + value);
return key;
}
public String encryptData2(String data) {
String en = null;
try {
byte[] key=hexStringToByte(this.key);
en = bytesToHexString(encryptData(data.getBytes(),key));
} catch (Exception e) {
e.printStackTrace();
}
return en;
}
public String decryptData2(String data) {
String de = null;
try {
byte[] key=hexStringToByte(this.key);
de = new String(decryptData(hexStringToByte(data),key));
} catch (Exception e) {
e.printStackTrace();
}
return de;
}
} 加密使用:byte[] key=Eryptogram.getSecretKey(deptid);//获得钥匙(字节数组)
byte[] tmp=Eryptogram.encryptData(password.getBytes(), key);//传入密码和钥匙,获得加密后的字节数组的密码
password=Eryptogram.bytesToHexString(tmp);//将字节数组转化为字符串,获得加密后的字符串密码解密与之差不多
java密码加密与解密 以下两个类可以很方便java明文解密源代码的完成字符串java明文解密源代码的加密和解密
加密 CryptHelper encrypt(password)
解密 CrypHelper decrypt(password)
代码如下
CryptUtils java
[java]
package gdie lab crypt;
import java io IOException;
import javax crypto Cipher;
import javax crypto KeyGenerator;
import javax crypto SecretKey;
import apache xerces internal impl dv util Base ;
public class CryptUtils {
private static String Algorithm = DES ;
private static byte[] DEFAULT_KEY=new byte[] { };
private static String VALUE_ENCODING= UTF ;
/**
* 生成密钥
*
* @return byte[] 返回生成java明文解密源代码的密钥
* @throws exception
*扔出异常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator getInstance(Algorithm)
SecretKey deskey = keygen generateKey()
// if (debug ) System out println ( 生成密钥 +byte hex (deskey getEncoded
// ()))
return deskey getEncoded()
}
/**
* 将指定java明文解密源代码的数据根据提供java明文解密源代码的密钥进行加密
*
* @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 ( 加密前的二进串 +byte hex (input ))
// System out println ( 加密前的字符串 +new String (input ))
//
// }
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher ENCRYPT_MODE deskey)

推荐阅读