码迷,mamicode.com
首页 > 编程语言 > 详细

java TripleDES加密

时间:2019-01-17 15:16:38      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:pre   生成密钥   length   eve   amp   sas   secret   初始化   binary   

package com.zhx.base.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.security.Key;

public class TripleDES {
    public static final String CIPHERMODE = "DESede/ECB/PKCS5Padding";
    public static final String ENCODE = "utf-8";

    /**
     * 初始化密钥
     * @return
     * @throws Exception
     */
    public static String getKeyStr() throws Exception{
        return Base64.encodeBase64String(getKey().getEncoded());
    }

    /**
     * 获取密钥
     * @return
     * @throws Exception
     */
    public static Key getKey() throws Exception{
        //实例化
        KeyGenerator kg = KeyGenerator.getInstance("DESede");
        kg.init(168);
        //生成密钥
        SecretKey secretKey = kg.generateKey();
        return secretKey;
    }



    public static String encrypt(String key, String src) {
        try {
            DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(ENCODE));
            SecretKeyFactory keyFactory = SecretKeyFactory
                    .getInstance("DESede");
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(CIPHERMODE);
            cipher.init(Cipher.ENCRYPT_MODE, securekey);
            byte[] b = cipher.doFinal(src.getBytes());
            return Base64.encodeBase64String(b).replaceAll("\r", "").replaceAll("\n", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String key, String src) {
        try {
            byte[] bytesrc = Base64.decodeBase64(src);
            DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(ENCODE));
            SecretKeyFactory keyFactory = SecretKeyFactory
                    .getInstance("DESede");
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(CIPHERMODE);
            cipher.init(Cipher.DECRYPT_MODE, securekey);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {

        String key = getKeyStr();
        System.out.println(key);
        System.out.println(key.getBytes().length);
        String test = TripleDES.encrypt(key, "1232135sdsadsasdshgjhkjhkadsadsdgdfhgdjhkbvnbcnbhgfjhgjhgkhghfdsgfdsgadsadadsad");
        System.out.println(test);
        test = test.replace("=","");
        System.out.println(test);
//        System.out.println(Base64Utils.getBase64(test));
        System.out.println(TripleDES.decrypt(key,test));
    }
}
package com.zhx.base.util;

public class TripleDESUtil {

    //    public static String key = PropertiesUtil.getValue("THREEDESKEY");
    public static String key = "asdsaffjhdfgdbv^&%^**hhjad";
    /**
     * 3DES加密
     *
     * @param value
     * @return
     */
    public static String getEncryptString(String value,String key) {
        return  TripleDES.encrypt(key, value).replace("=","");
    }
    /**
     * 3DES解密
     *
     * @param value
     * @return
     */
    public static String decryptString(String value,String key) {
        return  TripleDES.decrypt(key, value);
    }

    /**
     * 3DES加密 带Base64
     *
     * @param value
     * @return
     */
    public static String getEncryptStringIncludeBase64(String value,String key) {
        value = Base64Utils.getBase64(value);
        value = TripleDES.encrypt(key, value);
//        System.out.println("生成密文:" + value);
        return value.replace("=","");
    }

    /**
     * 3DES解密 带Base64
     *
     * @param str
     * @return
     */
    public static String decryptDataIncludeBase64(String str,String key) {
        if (str == null) {
            str = "";
        }
        str = str.replaceAll(" ", "+");
        return Base64Utils.getFromBase64(TripleDES.decrypt(key, str));
    }

    public static void main(String[] args) {
        String key = "5j3EAa7qp/R8xLquBKQskSaAtp224McC";
        System.out.println("*******************BASE64****************************");
        System.out.println(Base64Utils.getBase64("我的12312314"));
        System.out.println(Base64Utils.getFromBase64(Base64Utils.getBase64("我的12312314")));
        System.out.println("*******************3des****************************");
        System.out.println(TripleDESUtil.getEncryptString("我的12312314",key));
        System.out.println(TripleDESUtil.decryptString("+rEVEmiHZKcgIKVV2NcYNw",key));
        System.out.println(TripleDESUtil.decryptString(TripleDESUtil.getEncryptString("我的12312314",key), key));
        System.out.println("*******************3des+Base64****************************");
        System.out.println(TripleDESUtil.getEncryptStringIncludeBase64("我的12312314",key));
        System.out.println(TripleDESUtil.decryptDataIncludeBase64(TripleDESUtil.getEncryptStringIncludeBase64("我的12312314",key),key));
    }
}
package com.zhx.base.util;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;

public class Base64Utils {

    /**
     * 加密
     *
     * @param str
     * @return
     */
    public static String getBase64(String str) {
        byte[] b = null;
        String s = null;
        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b != null) {
            s = Base64.encodeBase64String(b);
        }
        return s;
    }

    /**
     * 解密
     *
     * @param s
     * @return
     */
    public static String getFromBase64(String s) {
        byte[] b = null;
        String result = null;
        if (s != null) {
            try {
                b = Base64.decodeBase64(s);
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}

 

java TripleDES加密

标签:pre   生成密钥   length   eve   amp   sas   secret   初始化   binary   

原文地址:https://www.cnblogs.com/SimonHu1993/p/10281887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!