java encode and decode by des

package com.tian.test16;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.*;

import javax.crypto.*;

import sun.misc.*;

public class Encrypt {
 private Key key;

 public void setKey(String strKey) {
  try {
   KeyGenerator generator = KeyGenerator.getInstance("DES");
   generator.init(new SecureRandom(strKey.getBytes()));
   this.key = generator.generateKey();
   generator = null;
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public String encodeString(String str) {
  BASE64Encoder base64en = new BASE64Encoder();
  byte[] fisrt = null;
  try {
   fisrt = str.getBytes("UTF8");
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  Cipher cipher = null;
  try {
   cipher = Cipher.getInstance("DES");
  } catch (NoSuchAlgorithmException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   cipher.init(Cipher.ENCRYPT_MODE, key);
  } catch (InvalidKeyException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  byte[] second = null;
  try {
   second = cipher.doFinal(fisrt);
  } catch (IllegalBlockSizeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (BadPaddingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  String result = base64en.encode(second);
  return result;
 }

 public String decodeString(String str) {
  BASE64Decoder base64De = new BASE64Decoder();
  byte[] first = null;
  try {
   first = base64De.decodeBuffer(str);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  Cipher cipher = null;
  try {
   cipher = Cipher.getInstance("DES");
  } catch (NoSuchAlgorithmException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   cipher.init(Cipher.DECRYPT_MODE, key);
  } catch (InvalidKeyException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  byte[] second = null;
  try {
   second = cipher.doFinal(first);
  } catch (IllegalBlockSizeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (BadPaddingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  String result = null;
  try {
   result = new String(second, "UTF8");
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return result;
 }

 public static void main(String[] args) {
  Encrypt et = new Encrypt();
  et.setKey("my test key");
  String encode = et.encodeString("mr tian this is the test account");
  System.out.println(encode);
  String decode = et.decodeString(encode);
  System.out.println(decode);
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值