DES加密解密示例

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.security.auth.kerberos.KerberosKey;
import javax.security.auth.kerberos.KerberosPrincipal;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

public class EncryptData {
 // 用于产生指定key的常量定义---开始
 private static final String PRINCIPAL_NAME = "abcd@aaa.net";// 产生key的主体名称

 private static final String KEY_PASSWORD = "dfddsdfs333";// key的密码

 private static final String ALGORITHM_NAME = "DES";// 算法名称

 // 用于产生指定的key的常量定义---结束
 public static void main(String args[]) {
  // String password = "administrator";
  String password = "在实际工作中我们常常会遇到对数据进行加密解密的工作(如对密码数据的保护或者对网络传输的数据进行保护),因为数据加密解密需要非常复杂、高效的算法,所以通常对于一般";
  try {
   String str=encrypt(password);
   System.out.println(decrypt(str));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }


 /**
  * DES加密字符串,返回加密后的字符串
  *
  * @param encryptString
  *            要加密的字符串
  * @param strings
  *            不传此参数按默认生成密钥 参数数组,[0]=加密主体名称,[1]=key的密码
  * @return 返回base64转码后的字符串
  * @throws InvalidKeyException
  * @throws NoSuchAlgorithmException
  * @throws NoSuchPaddingException
  * @throws UnsupportedEncodingException
  * @throws IllegalBlockSizeException
  * @throws BadPaddingException
  */
 public static String encrypt(String encryptString, String... strings)
   throws InvalidKeyException, NoSuchAlgorithmException,
   NoSuchPaddingException, UnsupportedEncodingException,
   IllegalBlockSizeException, BadPaddingException {
  Key key = createKey(strings);
  Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
  byte[] by = encryptString.getBytes("utf8");
  cipher.init(Cipher.ENCRYPT_MODE, key);// 使用私鈅加密
  byte[] cipherText = cipher.doFinal(by);
  return Base64.encode(cipherText).replaceAll("\n", "");
 }

 /**
  * DES解密字符串
  *
  * @param decryptString要解密的字符串
  * @param strings生成密鑰的參數
  *            不传此参数按默认生成密钥 参数数组,[0]=加密主体名称,[1]=key的密码
  * @return
  * @throws NoSuchAlgorithmException
  * @throws NoSuchPaddingException
  * @throws UnsupportedEncodingException
  * @throws InvalidKeyException
  * @throws IllegalBlockSizeException
  * @throws BadPaddingException
  */
 public static String decrypt(String decryptString, String... strings)
   throws NoSuchAlgorithmException, NoSuchPaddingException,
   UnsupportedEncodingException, InvalidKeyException,
   IllegalBlockSizeException, BadPaddingException {
  Key key = createKey(strings);
  Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
  byte[] by = Base64.decode(decryptString);
  cipher.init(Cipher.DECRYPT_MODE, key);// 使用私鈅加密
  byte[] cipherText = cipher.doFinal(by);
  return new String(cipherText, "utf-8");
 }

 /**
  * 创建指定的key,参数数组含义: strings[0]=生成key的主体名称
  * strings[1]=key的密码(参考命名格式xxx@xx.xxx其他命名格式参考Java Cryptography Architecture
  * API Specification & Reference 中的附录 A) strings[2]=算法名称如果不传参数按默认值
  *
  * @param strings
  * @return
  */
 public static Key createKey(String... strings) {
  String algorithm = ALGORITHM_NAME;
  String name = PRINCIPAL_NAME;
  String password = KEY_PASSWORD;
  switch (strings.length) {
  case 1:
   name = strings[0];
   break;
  case 2:
   name = strings[0];
   password = strings[1];
   break;
  case 3:
   name = strings[0];
   password = strings[1];
   algorithm = strings[2];
   break;
  }

  KerberosPrincipal kerberosPrincipal = new KerberosPrincipal(name);
  Key key = new KerberosKey(kerberosPrincipal, password.toCharArray(),
    algorithm);
  return key;
 }

 /**
  * 产生一个随机key,并保存到文件中
  *
  * @param file
  */
 public static void setPriveKey(String file) {
  FileOutputStream fos;
  try {
   fos = new FileOutputStream(file);
   KeyGenerator keyGen = KeyGenerator.getInstance("DES");
   keyGen.init(56);
   Key key = keyGen.generateKey();// 生成私钥Key
   ObjectOutputStream oos = new ObjectOutputStream(fos);
   oos.writeObject(key);
   oos.close();
  } catch (Exception e1) {
   e1.printStackTrace();
  }
 }

 /**
  * 从文件中获取key
  *
  * @param file
  * @return
  * @throws Exception
  */
 public static Key getPriveKey(String file) throws Exception {
  FileInputStream fis = new FileInputStream(file);
  ObjectInputStream ois = new ObjectInputStream(fis);
  Key key = (Key) ois.readObject();
  return key;
 }
}

/**加密后的byte不能直接new string否则不能对形成的string解密,需要进行Base64编码*/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值