欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java encode and decode by des  

程序员文章站 2024-03-22 21:09:10
...

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

}