- 相關推薦
java證書的加密與解密代碼
java很多時候要對秘要進行持久化加密,此時的加密采用md5。采用對稱加密的時候就采用DES方法了,那么java證書的加密與解密代碼是什么呢?下面跟yjbys小編一起來學習一下吧!
以下兩個類可以很方便的完成字符串的加密和解密:
加密:CryptHelper.encrypt(password)
解密:CrypHelper.decrypt(password)
代碼如下:
CryptUtils.java
[java]
package com.gdie.lab.crypt;
import java.io.IOException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class CryptUtils {
private static String Algorithm = "DES";
private static byte[] DEFAULT_KEY=new byte[] {-53, 122, -42, -88, -110, -123, -60, -74};
private static String VALUE_ENCODING="UTF-8";
/**
* 生成密鑰
*
* @return byte[] 返回生成的密鑰
* @throws exception
* 扔出異常.
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
// if (debug ) System.out.println ("生成密鑰:"+byte2hex (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 ("加密前的二進串:"+byte2hex (input ));
// System.out.println ("加密前的字符串:"+new String (input ));
//
// }
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] cipherByte = c1.doFinal(input);
// if (debug ) System.out.println ("加密后的二進串:"+byte2hex (cipherByte ));
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input, DEFAULT_KEY);
}
/**
* 將給定的已加密的數據通過指定的密鑰進行解密
*
* @param input
* 待解密的數據
* @param key
* 密鑰
* @return byte[] 解密后的數據
* @throws Exception
*/
public static byte[] decryptData(byte[] input, byte[] key) throws Exception {
SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, Algorithm);
// if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
byte[] clearByte = c1.doFinal(input);
// if (debug )
// {
// System.out.println ("解密后的二進串:"+byte2hex (clearByte ));
// System.out.println ("解密后的字符串:"+(new String (clearByte )));
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input, DEFAULT_KEY);
}
/**
* 字節碼轉換成16進制字符串
*
* @param byte[] b 輸入要轉換的字節碼
* @return String 返回轉換后的16進制字符串
*/
public static String byte2hex(byte[] bytes) {
StringBuilder hs = new StringBuilder();
for(byte b : bytes)
hs.append(String.format("%1$02X", b));
return hs.toString();
}
public static byte[] hex2byte(String content) {
int l=content.length()>>1;
byte[] result=new byte[l];
for(int i=0;i
int j=i<<1;
String s=content.substring(j, j+2);
result[i]=Integer.valueOf(s, 16).byteValue();
}
return result;
}
/**
* 將字節數組轉換為base64編碼字符串
* @param buffer
* @return
*/
public static String bytesToBase64(byte[] buffer) {
//BASE64Encoder en=new BASE64Encoder();
return Base64.encode(buffer);
// return encoder.encode(buffer);
}
/**
* 將base64編碼的字符串解碼為字節數組
* @param value
* @return
* @throws IOException
*/
public static byte[] base64ToBytes(String value) throws IOException {
//return Base64.decodeToByteArray(value);
// System.out.println(decoder.decodeBuffer(value));
// return decoder.decodeBuffer(value);
return Base64.decode(value);
}
/**
* 加密給定的字符串
* @param value
* @return 加密后的base64字符串
*/
public static String encryptString(String value) {
return encryptString(value, DEFAULT_KEY);
}
/**
* 根據給定的密鑰加密字符串
* @param value 待加密的字符串
* @param key 以BASE64形式存在的密鑰
* @return 加密后的base64字符串
* @throws IOException
*/
public static String encryptString(String value, String key) throws IOException {
return encryptString(value, base64ToBytes(key));
}
/**
* 根據給定的密鑰加密字符串
* @param value 待加密的字符串
* @param key 字節數組形式的密鑰
* @return 加密后的base64字符串
*/
public static String encryptString(String value, byte[] key) {
try {
byte[] data=value.getBytes(VALUE_ENCODING);
data=CryptUtils.encryptData(data, key);
return bytesToBase64(data);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* 解密字符串
* @param value base64形式存在的密文
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value, DEFAULT_KEY);
}
/**
* 解密字符串
* @param value base64形式存在的密文
* @param key base64形式存在的密鑰
* @return 明文
* @throws IOException
*/
public static String decryptString(String value, String key) throws IOException {
String s=decryptString(value, base64ToBytes(key));
return s;
}
/**
* 解密字符串
* @param value base64形式存在的密文
* @param key 字節數據形式存在的密鑰
* @return 明文
*/
public static String decryptString(String value, byte[] key) {
try {
byte[] data=base64ToBytes(value);
data=CryptUtils.decryptData(data, key);
return new String(data, VALUE_ENCODING);
}catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
package com.gdie.lab.crypt;
import java.io.IOException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class CryptUtils {
private static String Algorithm = "DES";
private static byte[] DEFAULT_KEY=new byte[] {-53, 122, -42, -88, -110, -123, -60, -74};
private static String VALUE_ENCODING="UTF-8";
/**
* 生成密鑰
*
* @return byte[] 返回生成的密鑰
* @throws exception
* 扔出異常.
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
SecretKey deskey = keygen.generateKey();
// if (debug ) System.out.println ("生成密鑰:"+byte2hex (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 ("加密前的二進串:"+byte2hex (input ));
// System.out.println ("加密前的字符串:"+new String (input ));
//
// }
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.ENCRYPT_MODE, deskey);
byte[] cipherByte = c1.doFinal(input);
// if (debug ) System.out.println ("加密后的二進串:"+byte2hex (cipherByte ));
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input, DEFAULT_KEY);
}
/**
* 將給定的已加密的數據通過指定的密鑰進行解密
*
* @param input
* 待解密的數據
* @param key
* 密鑰
* @return byte[] 解密后的數據
* @throws Exception
*/
public static byte[] decryptData(byte[] input, byte[] key) throws Exception {
SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, Algorithm);
// if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));
Cipher c1 = Cipher.getInstance(Algorithm);
c1.init(Cipher.DECRYPT_MODE, deskey);
byte[] clearByte = c1.doFinal(input);
// if (debug )
// {
// System.out.println ("解密后的二進串:"+byte2hex (clearByte ));
// System.out.println ("解密后的字符串:"+(new String (clearByte )));
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input, DEFAULT_KEY);
}
/**
* 字節碼轉換成16進制字符串
*
* @param byte[] b 輸入要轉換的字節碼
* @return String 返回轉換后的16進制字符串
*/
public static String byte2hex(byte[] bytes) {
StringBuilder hs = new StringBuilder();
for(byte b : bytes)
hs.append(String.format("%1$02X", b));
return hs.toString();
}
public static byte[] hex2byte(String content) {
int l=content.length()>>1;
byte[] result=new byte[l];
for(int i=0;i
int j=i<<1;
String s=content.substring(j, j+2);
result[i]=Integer.valueOf(s, 16).byteValue();
}
return result;
}
/**
* 將字節數組轉換為base64編碼字符串
* @param buffer
* @return
*/
public static String bytesToBase64(byte[] buffer) {
//BASE64Encoder en=new BASE64Encoder();
return Base64.encode(buffer);
// return encoder.encode(buffer);
}
/**
* 將base64編碼的字符串解碼為字節數組
* @param value
* @return
* @throws IOException
*/
public static byte[] base64ToBytes(String value) throws IOException {
//return Base64.decodeToByteArray(value);
// System.out.println(decoder.decodeBuffer(value));
// return decoder.decodeBuffer(value);
return Base64.decode(value);
}
/**
* 加密給定的字符串
* @param value
* @return 加密后的base64字符串
*/
public static String encryptString(String value) {
return encryptString(value, DEFAULT_KEY);
}
/**
* 根據給定的密鑰加密字符串
* @param value 待加密的字符串
* @param key 以BASE64形式存在的密鑰
* @return 加密后的base64字符串
* @throws IOException
*/
public static String encryptString(String value, String key) throws IOException {
return encryptString(value, base64ToBytes(key));
}
/**
* 根據給定的密鑰加密字符串
* @param value 待加密的字符串
* @param key 字節數組形式的密鑰
* @return 加密后的base64字符串
*/
public static String encryptString(String value, byte[] key) {
try {
byte[] data=value.getBytes(VALUE_ENCODING);
data=CryptUtils.encryptData(data, key);
return bytesToBase64(data);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* 解密字符串
* @param value base64形式存在的密文
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value, DEFAULT_KEY);
}
/**
* 解密字符串
* @param value base64形式存在的密文
* @param key base64形式存在的密鑰
* @return 明文
* @throws IOException
*/
public static String decryptString(String value, String key) throws IOException {
String s=decryptString(value, base64ToBytes(key));
return s;
}
/**
* 解密字符串
* @param value base64形式存在的密文
* @param key 字節數據形式存在的密鑰
* @return 明文
*/
public static String decryptString(String value, byte[] key) {
try {
byte[] data=base64ToBytes(value);
data=CryptUtils.decryptData(data, key);
return new String(data, VALUE_ENCODING);
}catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
CryptHelper.java
[java]
package com.gdie.lab.crypt;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.springframework.util.DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = "zhongqian";
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils.md5DigestAsHex(CRYPT_KEY.getBytes()).toUpperCase();
KEY = KEY.substring(0, 8);
byte[] bytes = KEY.getBytes();
DESKeySpec ks = new DESKeySpec(bytes);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(ks);
IvParameterSpec iv2 = new IvParameterSpec(bytes);
ecip = Cipher.getInstance("DES/CBC/PKCS5Padding");
ecip.init(Cipher.ENCRYPT_MODE, sk, iv2);
dcip = Cipher.getInstance("DES/CBC/PKCS5Padding");
dcip.init(Cipher.DECRYPT_MODE, sk, iv2);
}catch(Exception ex) {
ex.printStackTrace();
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip.doFinal(content.getBytes("ascii"));
return CryptUtils.byte2hex(bytes);
}
public static String decrypt(String content) throws Exception {
byte[] bytes = CryptUtils.hex2byte(content);
bytes = dcip.doFinal(bytes);
return new String(bytes, "ascii");
}
//test
public static void main(String[] args) throws Exception {
String password = "gly";
String en = encrypt(password);
System.out.println(en);
System.out.println(decrypt(en));
}
}
package com.gdie.lab.crypt;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import org.springframework.util.DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = "zhongqian";
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils.md5DigestAsHex(CRYPT_KEY.getBytes()).toUpperCase();
KEY = KEY.substring(0, 8);
byte[] bytes = KEY.getBytes();
DESKeySpec ks = new DESKeySpec(bytes);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(ks);
IvParameterSpec iv2 = new IvParameterSpec(bytes);
ecip = Cipher.getInstance("DES/CBC/PKCS5Padding");
ecip.init(Cipher.ENCRYPT_MODE, sk, iv2);
dcip = Cipher.getInstance("DES/CBC/PKCS5Padding");
dcip.init(Cipher.DECRYPT_MODE, sk, iv2);
}catch(Exception ex) {
ex.printStackTrace();
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip.doFinal(content.getBytes("ascii"));
return CryptUtils.byte2hex(bytes);
}
public static String decrypt(String content) throws Exception {
byte[] bytes = CryptUtils.hex2byte(content);
bytes = dcip.doFinal(bytes);
return new String(bytes, "ascii");
}
//test
public static void main(String[] args) throws Exception {
String password = "gly";
String en = encrypt(password);
System.out.println(en);
System.out.println(decrypt(en));
}
}
【java證書的加密與解密代碼】相關文章:
PHP url 加密解密函數代碼方法03-31
java非對稱加密的源代碼(rsa)03-30
如何讓JAVA代碼更高效03-20
Java代碼的基本知識02-27
Java中的動態代碼編程03-05
在Java中執行JavaScript代碼04-01
關于Java源代碼折行的規則03-20
如何給word文檔加密03-09
過濾HTML代碼08-29