J2EE培训 百分网手机站

java证书的加密与解密代码

时间:2017-05-24 20:04:42 J2EE培训 我要投稿

java证书的加密与解密代码

  java很多时候要对秘要进行持久化加密,此时的加密采用md5。采用对称加密的时候就采用DES方法了,那么java证书的加密与解密代码是什么呢?下面跟yjbys小编一起来学习一下吧!

java证书的加密与解密代码

  以下两个类可以很方便的完成字符串的加密和解密:

  加密: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) {