Java端sqlserver数据库des加密 东师理想

  1. 在这里以一个Java程序举例说明,分为一个加密、解密类,一个测试类,废话少说,上代码
  2. 加密、解密类
    import java.security.Key;
    import java.security.spec.AlgorithmParameterSpec;

    import javax.crypto.Cipher;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    import javax.crypto.spec.IvParameterSpec;

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


    /**
    *
    *
    @author Administrator
    */

    public class CryptoTools {
    // DES加密的私钥,必须是8位长的字符串
    private static final byte[] DESkey = "12345678".getBytes();// 设置密钥

    private static final byte[] DESIV = "zhoufeng".getBytes();// 设置向量

    static AlgorithmParameterSpec iv = null;// 加密算法的参数接口,IvParameterSpec是它的一个实现
    private static Key key = null;

    public CryptoTools() throws Exception {
    DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
    iv = new IvParameterSpec(DESIV);// 设置向量
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
    key = keyFactory.generateSecret(keySpec);// 得到密钥对象

    }

    /**
    * <p>功能:加密</p>
    *
    @author 周枫
    * @date 2012-4-5
    *
    @param
    *
    @return String
    */
    public String encode(String data) throws Exception {
    Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
    enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量
    byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
    BASE64Encoder base64Encoder = new BASE64Encoder();
    return base64Encoder.encode(pasByte);
    }

    /**
    * <p>功能:解密</p>
    *
    @author 周枫
    * @date 2012-4-5
    *
    @param
    *
    @return String
    */
    public String decode(String data) throws Exception {
    Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    deCipher.init(Cipher.DECRYPT_MODE, key, iv);
    BASE64Decoder base64Decoder = new BASE64Decoder();
    byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
    return new String(pasByte, "UTF-8");
    }
    }
  3. 测试类
    //测试
    public static void main(String[] args) throws Exception {

    CryptoTools tools = new CryptoTools();
    System.out.println("加密:" + tools.encode("123456"));
    System.out.println("解密:" + tools.decode(tools.encode("123456")));
    }
  4. 成功了,哈哈

原文地址:https://www.cnblogs.com/cczhoufeng/p/2433515.html