java-可逆加密算法

package main;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base32;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public class CryptoUtil {

    public static Key DEFAULT_KEY = null;

    public static final String DEFAULT_SECRET_KEY1 = "?:P)(OL>
     * String明文输入,String密文输出
     */
    public static String encode(String str) {
        return encode64(null, str);
    }

    /**
     * 加密
* String明文输入,String密文输出 */ public static String encode64(String key, String str) { return Base64.encodeBase64URLSafeString(obtainEncode(key, str.getBytes())); } /** * 加密
* String明文输入,String密文输出 */ public static String encode32(String key, String str) { return base32.encodeAsString(obtainEncode(key, str.getBytes())).replaceAll("=", ""); } /** * 加密
* String明文输入,String密文输出 */ public static String encode16(String key, String str) { return Hex.encodeHexString(obtainEncode(key, str.getBytes())); } /** * 解密
* 以String密文输入,String明文输出 */ public static String decode(String str) { return decode64(null, str); } /** * 解密
* 以String密文输入,String明文输出 */ public static String decode64(String key, String str) { return new String(obtainDecode(key, Base64.decodeBase64(str))); } /** * 解密
* 以String密文输入,String明文输出 */ public static String decode32(String key, String str) { return new String(obtainDecode(key, base32.decode(str))); } /** * 解密
* 以String密文输入,String明文输出 */ public static String decode16(String key, String str) { try { return new String(obtainDecode(key, Hex.decodeHex(str.toCharArray()))); } catch (DecoderException e) { e.printStackTrace(); } return null; } /** * 加密
* 以byte[]明文输入,byte[]密文输出 */ private static byte[] obtainEncode(String key, byte[] str) { byte[] byteFina = null; Cipher cipher; try { Key key1 = obtainKey(key); cipher = Cipher.getInstance(DES); cipher.init(Cipher.ENCRYPT_MODE, key1); byteFina = cipher.doFinal(str); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } /** * 解密
* 以byte[]密文输入,以byte[]明文输出 */ private static byte[] obtainDecode(String key, byte[] str) { Cipher cipher; byte[] byteFina = null; try { Key key1 = obtainKey(key); cipher = Cipher.getInstance(DES); cipher.init(Cipher.DECRYPT_MODE, key1); byteFina = cipher.doFinal(str); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } /** * 加密 * @param val * @return */ public static String encrypt(String val){ String m = encode64(DEFAULT_SECRET_KEY2, val); String m1 = encode32(DEFAULT_SECRET_KEY3, m); return m1; } /** * 解密 * @param val * @return */ public static String decrypt(String val){ String n = decode32(DEFAULT_SECRET_KEY3, val); String n1 = decode64(DEFAULT_SECRET_KEY2, n); return n1; } public static void main(String[] args) { //加密 String m = encrypt("123456"); System.out.println("加密结果:"+m); //解密 String n = decrypt(m); System.out.println("解密结果:"+n); } }

加密结果:UG6BGBJNDRXSEEFJUANJN535EI
解密结果:123456

你可能感兴趣的:(java-可逆加密算法)