java TripleDES加密

Posted SimonHu1993

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java TripleDES加密相关的知识,希望对你有一定的参考价值。

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("
", "").replaceAll("
", "");
        } 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加密的主要内容,如果未能解决你的问题,请参考以下文章

多语言(Java&C#&Ruby&C++&Objective-C&Android)互通的TripleDES加解密算法实现

TripleDES 加密在 c# 和 PHP 中没有得到相同的结果

js 加密解密 TripleDES

加密算法:AES DES RC4 Rabbit TripleDes 那个更好??它们分

php和java中的加密和解密

C# TripleDES NoPadding 时对待加密内容进行补字节(8个字节为一个Block)