DES加密和解密代码

package com.bopomofo.common;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DES {

    private static final byte[] desKey = "INKOO.3DES.KEY".getBytes();

    public DES() {}

    public static String encrypt(String plainText) {
        // DES算法要求有一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        byte rawKeyData[] = desKey;/* 用某种方法获得密匙数据 */
        String rtnStr = "";
        try
        {
            // 从原始密匙数据创建DESKeySpec对象
            DESKeySpec dks;
            dks = new DESKeySpec(rawKeyData);
            // 创建一个密匙工厂,然后用它把DESKeySpec转换成
            // 一个SecretKey对象
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(dks);
            // Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES");
            // 用密匙初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, key, sr);
            // 现在,获取数据并加密
            byte data[] = plainText.getBytes();/* 用某种方法获取数据 */
            // 正式执行加密操作
            byte encryptedData[] = cipher.doFinal(data);
            rtnStr = new BASE64Encoder().encode(encryptedData);
        } catch (InvalidKeyException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeySpecException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalBlockSizeException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadPaddingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return rtnStr;
    }

    public static String decrypt(String encryptText) {
        // DES算法要求有一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        byte rawKeyData[] = desKey; /* 用某种方法获取原始密匙数据 */
        String rtnStr = "";

        try
        {
            // 从原始密匙数据创建一个DESKeySpec对象
            DESKeySpec dks;
            dks = new DESKeySpec(rawKeyData);
            // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
            // 一个SecretKey对象
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(dks);
            // Cipher对象实际完成解密操作
            Cipher cipher = Cipher.getInstance("DES");
            // 用密匙初始化Cipher对象
            cipher.init(Cipher.DECRYPT_MODE, key, sr);
            // 现在,获取数据并解密
            byte encryptedData[] = (new BASE64Decoder().decodeBuffer(encryptText));/* 获得经过加密的数据 */
            // 正式执行解密操作
            byte decryptedData[] = cipher.doFinal(encryptedData);
            rtnStr = new String(decryptedData);
        } catch (InvalidKeyException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeySpecException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalBlockSizeException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadPaddingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return rtnStr;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(encrypt("provider01"));
  

    }

}

你可能感兴趣的:(算法,Security,sun)