J2EE培训 百分网手机站

java证书的加密与解密代码(2)

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

java证书的加密与解密代码

  //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 ));